Extend hoster to get paths from environment.

This commit is contained in:
Adrian Elsener 2022-08-12 11:12:50 +02:00 committed by GitHub
parent 581f5c1e66
commit 7942a16cd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 9 deletions

View File

@ -1,11 +1,8 @@
FROM frolvlad/alpine-python3
FROM python:3.10.6-alpine3.16
RUN pip3 install docker
RUN mkdir /hoster
WORKDIR /hoster
ADD hoster.py /hoster/
CMD ["python3", "-u", "hoster.py"]
CMD ["python3", "-u", "hoster.py"]

View File

@ -1,14 +1,25 @@
# Hoster
## Mount hosts file directyly
A simple "etc/hosts" file injection tool to resolve names of local Docker containers on the host.
hoster is intended to run in a Docker container:
docker run -d \
-v /var/run/docker.sock:/tmp/docker.sock \
-v /etc/hosts:/tmp/hosts \
--mount type=bind,source=/var/run/docker.sock,target=/tmp/docker.sock \
--mount type=bind,source=/etc/hosts,target=/tmp/hosts \
dvdarias/docker-hoster
## Mount directory containing hosts file
In some cases there is a problem while rewriting hosts file. This can be prevent with mapping/mounting complete directory and point to the hosts file with environment variable.
docker run -d \
-e HOST_PATH=/tmp/etc/hosts -e SOCK_PATH=tmp/docker.sock \
--mount type=bind,source=/etc,target=/tmp/etc \
--mount type=bind,source=/var/run/docker.sock,target=/tmp/docker.sock \
dvdarias/docker-hoster
The `docker.sock` is mounted to allow hoster to listen for Docker events and automatically register containers IP.
Hoster inserts into the host's `/etc/hosts` file an entry per running container and keeps the file updated with any started/stoped container.

View File

@ -129,9 +129,11 @@ def update_hosts_file():
def parse_args():
hostPath = os.getenv('HOST_PATH', "/tmp/hosts")
sockPath = os.getenv('SOCK_PATH', "tmp/docker.sock")
parser = argparse.ArgumentParser(description='Synchronize running docker container IPs with host /etc/hosts file.')
parser.add_argument('socket', type=str, nargs="?", default="tmp/docker.sock", help='The docker socket to listen for docker events.')
parser.add_argument('file', type=str, nargs="?", default="/tmp/hosts", help='The /etc/hosts file to sync the containers with.')
parser.add_argument('socket', type=str, nargs="?", default=sockPath, help='The docker socket to listen for docker events.')
parser.add_argument('file', type=str, nargs="?", default=hostPath, help='The /etc/hosts file to sync the containers with.')
return parser.parse_args()
if __name__ == '__main__':