Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Networking: Auto-discovering host names in a bridge network

Tags:

docker

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

like image 543
ThePhysicist Avatar asked Nov 20 '15 20:11

ThePhysicist


People also ask

What is the difference between bridge and host network in Docker?

User-defined bridge networks are best when you need multiple containers to communicate on the same Docker host. Host networks are best when the network stack should not be isolated from the Docker host, but you want other aspects of the container to be isolated.

How does Docker bridge network work?

In terms of Docker, a bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.

What network does a container attached to a Docker bridge network have?

Network internal mode By default, when you connect a container to an overlay network, Docker also connects a bridge network to it to provide external connectivity. If you want to create an externally isolated overlay network, you can specify the --internal option.

How does overlay network work in Docker?

The overlay network driver creates a distributed network among multiple Docker daemon hosts. This network sits on top of (overlays) the host-specific networks, allowing containers connected to it (including swarm service containers) to communicate securely when encryption is enabled.


1 Answers

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.

like image 183
VonC Avatar answered Nov 16 '22 04:11

VonC