Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker linked containers, Docker Networks, Compose Networks - how should we now 'link' containers

I have an existing app that comprises of 4 docker containers running on the same host. They have been linked together using the link command.

However, after some upgrades of docker, the link behaviour has been deprecated, and changed it seems. We are having issues where containers are loosing the link to each other now.

So, docker says to use the new Network feature over linked containers. But I can't see how this works.

If 2 containers are in the same network, are the same ENV vars automatically exposed on the containers as if they were linked?

Or is the hosts file updated with the correct container name / ip addresses ? Even after a docker restart ?

I can't see in the docs how a container can find the location of another in its network?

Also, compose looks to have a simple set up for linking containers, and may automate some of this - would compose be the way to go for defining multi container apps? Or is it too soon to run it in production?

Does compose support multiple host configuration as well?

at some point in the future we will probably need to move one of the containers to a different host....

like image 958
Matt Bryson Avatar asked Jan 11 '16 17:01

Matt Bryson


People also ask

Can we link Docker containers?

Docker also has a linking system that allows you to link multiple containers together and send connection information from one to another. When containers are linked, information about a source container can be sent to a recipient container.

Can a Docker container connect to multiple networks?

You can create multiple networks with Docker and add containers to one or more networks. Containers can communicate within networks but not across networks. A container with attachments to multiple networks can connect with all of the containers on all of those networks.

Can we share container network between Docker hosts?

If you use the host network mode for a container, that container's network stack is not isolated from the Docker host (the container shares the host's networking namespace), and the container does not get its own IP-address allocated.


1 Answers

If 2 containers are in the same network, are the same ENV vars automatically exposed on the containers as if they were linked?

no, you would now have to use the container names as their hostnames. The new network feature has no idea which ports will be used. Think of this as 2 computers plugged on the same network hub. Both can address the other one by its hostname.

is the hosts file updated with the correct container name / ip addresses ? Even after a docker restart ?

yes, /etc/hosts files for all containers which are part of a network will be updated live by the docker engine.

I can't see in the docs how a container can find the location of another in its network?

Using the container name. See the Connect containers section of the Work with network commands doc: Once connected, the containers can communicate using another container’s IP address or name.

Also, compose looks to have a simple set up for linking containers, and may automate some of this - would compose be the way to go for defining multi container apps? Or is it too soon to run it in production?

Compose supports the new network feature as beta by offering the --x-networking option. You should not use it in production yet (current Compose version is 1.5).

Furthermore, the current implementation is a bit inconvenient as we must use the full container name which is composed of the project name + _ + container name + _1. The documentation says the next version (current one is 1.5) will improve this so that we should not have to worry about the project name to address containers.

Does compose support multiple host configuration as well?

Yes, in conjonction with Swarm as detailed in the overlay network documentation

like image 106
Thomasleveil Avatar answered Oct 10 '22 04:10

Thomasleveil