This question already has an answer here:
I have a couple of docker Containers and one special case that two of them have to talk to each other, so they must know each other at best via link. I can link one Container to the other but the problem is, that i can't tell them that the second one can talk back to the first.
I tried to create and run the first container and stopped it, then i created started the second container and stopped it as well. Next up i started the first container again with the link to the second and started the second one linked to the first. After this my machine went crazy the docker process took all CPU and Memory and neither of the containers was accessible. When you killed the process a new one poped with the same. Even when i deinstalled docker restarted the machine and installed docker again it get back to to the crazy state without even starting one of the containers.
Got anyone a solution how to link two containers to each other or let them talk to each other in both directions?
Containers in the same network are linked to each other.
You have to create a network for those containers.
When running the containers, you have to specify the network.
after you created the containers you can ping other containers in the same network using just the name of the containers
The bridge or host network works when 2 containers run on the same host. If two containers runs on different hosts, they will not be able to talk with each other.
For two containers on different hosts to talk with each other, the docker overlay network should be used.
The docker --link is a deprecated feature, as mentioned on docker links doc.
The recommended approach is to create a user-defined network where you can connect multiple containers.
The network can easily be created using the following command:
When the network has been created you can start containers like this.
First container:
Second container:
More info can be found in the Docker docs about user-defined networks.
Another possible approach that does not use linking is to map the containers by exposing a port on each container and binding the containers to the host interface.
This way container1 can access container2 via localhost:6666 and container2 can access container1 via localhost:5555.
This is not linking but there is no way to do bidirectional linking.
The documentation for docker networking explains this further.
Another approach is to connect the containers by binding the ports to the docker0 interface. All docker containers are connected to this bridge per default (which usually has the ip address
172.17.42.1
).The containers can access eachother via
172.17.42.1
and the specific port.A similar solution like @wassgreen provided, but has the advantage that the containers do not have access to the hosts interfaces.
See Advanced networking section
--net=host
:For more information about this connection via
docker0
interface, see Unorthodox docker connection without links.Check the --link option in docker docs. You can link 2 containers with the following command: