Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 4 additions & 2 deletions hoster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down