I am trying to get the following pretty basic (or so I thought) networking setup to work using Docker 1.9:
postgres
container and a python
container (there might be more than two). bridge
network.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
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.
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.
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.
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.
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 asapp
in networkisolated_nw
and asscoped-app
in networklocal_alias
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With