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?
If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.
For an easy solution you could use Docker-compose . in you compose file (docker-compose. yml) use the option links Link to containers in another service. Either specify both the service name and a link alias (SERVICE:ALIAS), or just the service name.
You can use sockets, the containers will communicate through ports just like any other server would. Otherwise you can link them together which allows separate containers to act as one.
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
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