Public | Automated Build

Last pushed: 12 hours ago
Short Description
Simple Docker service discovery.
Full Description

Simple Docker service discovery

docker-hosts maintains a file in the format of /etc/hosts that contains IP addresses and hostnames of all Docker
containers on the host. When the generated file is mounted at /etc/hosts within your Docker container it
provides simple hostname resolution. This allows you to set up redis and web containers where the web container
is able to connect to redis via its hostname, without a need for linking of containers together. This allows for
independent life-cycles of containers. You can optionally provide a domain like dev.docker, so redis.dev.docker
is a usable alias, as well (by default is set to docker).

Running

The easiest way to run it is inside a Docker container using the provided tozd/docker-hosts Docker image. You have
to provide host's Docker socket file to it. For example, create an empty file /srv/var/hosts on the host and mount
it as a volume into the container so that docker-hosts will keep it populated with entries.

docker run -d -v /var/run/docker.sock:/var/run/docker.sock -v /srv/var/hosts:/hosts -e DOMAIN_NAME=dev.docker tozd/docker-hosts

Optionally, you can also mount a volume onto /var/log/hosts to store logs.

Then read-only mount /srv/var/hosts into all containers where you want hostname resolution to happen. For example:

docker run -i -t -v /srv/var/hosts:/etc/hosts:ro centos /bin/bash

Within the centos container, you'll see /etc/hosts has an entry for the container you just started, as well as
any other containers already running. /etc/hosts will continue to reflect all of the containers currently running
on this Docker host.

The only container that should have write access to the generated hosts file is the container running this
application.

Running on the host

You can also run docker-hosts directly on the host. First, you have to build it. This project uses
gpm and gvp. Both must be available on your
path. Run:

make

-- or --

gvp init
source gvp in
gpm install
go build -v -o stage/docker-host ./...

Start the docker-host process and give it the path to a file that will be mounted as /etc/hosts in your containers:

docker-host --domain-name=dev.docker /srv/var/hosts

Optionally specify DOCKER_HOST environment variable.

History

This is a fork of the original docker-hosts project.

This utility was inspired by Michael Crosby's skydock project.
docker-hosts and skydock (paired with skydns) work in much the same way: the container lifecycle
is monitored via the Docker daemon's events, and resolvable hostnames are made available to appropriately-configured
containers. The end result is that you have a simple way of connecting containers together on the same Docker host,
without having to resort to links or manual configuration. This does not provide a solution to container connectivity
across Docker hosts. For that you should look at something like Jeff Lindsay's
registrator.

Docker Pull Command
Owner
tozd
Source Repository

Comments (1)
premma
2 years ago

Hello,
your project is great, but I wonder if you could add one feature to find hosts in docker-compose created networks. The docker inspect command output differs slightly and all that would be needed is to add a parameter to your script, that would specify a network name, where /etc/hosts should find the IP addresses.
example of docker inspect with separate network:

...
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "f695e341d4c79e2823c9d3fa73c88eee6059208ba88e5ec6583c03f207835962",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "50000/tcp": null,
                "8080/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/f695e341d4c7",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "testing": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "jenkins-with-docker",
                        "0a1406b6d5"
                    ],
                    "NetworkID": "9dd5fc6ef187cfee64de1b580760a3f7bfff96b00ad586f66c029ea6ffeef5c9",
                    "EndpointID": "dc3a0247a7e787c014b38a42e92aa7ca87d80d9deb17cbccb227eb3318232859",
                    "Gateway": "172.21.0.1",
                    "IPAddress": "172.21.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:15:00:03"
                }
            }
        }
...