This question already has an answer here:
-
How to create a bidirectional link between containers?
5 answers
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?
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:
docker network create -d bridge my-net
When the network has been created you can start containers like this.
First container:
docker run -it --name b1 --network=my-net --rm bash:4.4
Second container:
docker run -it --name b2 --network=my-net --rm bash:4.4
bash-4.4# ping b1
PING b2 (172.26.0.3): 56 data bytes
64 bytes from 172.26.0.3: seq=0 ttl=64 time=0.111 ms
64 bytes from 172.26.0.3: seq=1 ttl=64 time=0.336 ms
...
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.
docker run --net=host -p 127.0.0.1:5555:5555 --name container1 my/dockerimage1
docker run --net=host -p 127.0.0.1:6666:6666 --name container2 my/dockerimage2
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.
Containers in the same network are linked to each other.
You have to create a network for those containers.
docker network create --driver bridge isolated_network
When running the containers, you have to specify the network.
docker run -it --net=isolated_network --name container1 container1-image bash
docker run -it --net=isolated_network --name container2 container2-image bash
after you created the containers you can ping other containers in the same network using just the name of the containers
root@container1 ping container2
root@container2 ping container1
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
).
docker run -p 172.17.42.1:8001:8080 --name container1 [image]
docker run -p 172.17.42.1:8002:8080 --name container2 [image]
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
:
[...] but it does let container processes open low-numbered ports like any other root process. It also allows the container to access local network services like D-bus. This can lead to processes in the container being able to do unexpected things like restart your computer. You should use this option with caution.
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:
$ docker run -i -t --name container_web --link container_db image_name
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.