Docker Networking: Auto-discovering host names in

2019-01-19 01:19发布

I am trying to get the following pretty basic (or so I thought) networking setup to work using Docker 1.9:

  • I have multiple containers that run services, e.g. a postgres container and a python container (there might be more than two).
  • Those containers are connected to each other through a bridge network.
  • I want them to be addressable using unique host names (i.e. the Python container should be able to do ping postgres to ping the Postgres container).

Following the tutorial (https://docs.docker.com/engine/userguide/networking/dockernetworks/), I can use the following sequence of commands to achieve this:

#create the containers
docker run -itd --name container1 busybox
docker run -itd --name container2 busybox
#create the network
docker network create test
docker network connect test container1
docker network connect test container2

This works quite well and Docker correctly sets the entries in etc/hosts to point to the correct IP addresses. However, I also want to be able to run several instances of that setup (i.e. containers + network) simultaneously. This does not work because the entry for each container in the /etc/hosts file is equal to its name, which needs to be unique. Specifying the hostname parameter does not solve this problem since it only changes the local hostname of the container (i.e. the one it sees itself).

I would be very interested in a way to do this without resorting to having a DNS service running on a container. It seems to be a simple problem but unfortunately I was not able to find any configuration options to change the name of a container in the /etc/hosts file.

BTW, I want the hostname to be the same in every instance of my network+container setup so that I do not need to dynamically pass the hostnames into the container (e.g. to tell the Python container the address of the Postgres container)

EDIT: I did some research on Docker's issue tracker and there seems to be a feature for this in the pipeline: https://github.com/docker/libnetwork/issues/737

标签: docker
1条回答
贪生不怕死
2楼-- · 2019-01-19 01:58

docker 1.10, and PR 19242 can help:

docker create --net-alias=[]: Add network-scoped alias for the container

docker 1.10 has a new section Network-scoped alias:

While links provide private name resolution that is localized within a container, the network-scoped alias provides a way for a container to be discovered by an alternate name by any other container within the scope of a particular network.
Unlike the link alias, which is defined by the consumer of a service, the network-scoped alias is defined by the container that is offering the service to the network.

Continuing with the above example, create another container in isolated_nw with a network alias.

$ docker run --net=isolated_nw -itd --name=container6 --net-alias app busybox
8ebe6767c1e0361f27433090060b33200aac054a68476c3be87ef4005eb1df17

Now let us connect container6 to the local_alias network with a different network-scoped alias.

$ docker network connect --alias scoped-app local_alias container6

container6 in this example now is aliased as app in network isolated_nw and as scoped-app in network local_alias.

查看更多
登录 后发表回答