Let two containers be connected to each other

I have a couple of docker containers and one special case when two of them should talk to each other, so they should know each other at best through a link. I can associate one Container with another, but the problem is that I cannot tell them that the second can return to the first.

I tried to create and start the first container and stop it, and then created a second container and stop it. Then I started the first container again with reference to the second and started the second, connected with the first. After that, my car went crazy, the docker process took up the entire processor and memory, and not one of the containers was available. When you killed the process, the new one was filled with the same. Even when I uninstalled the docker, restarted the machine and installed the docker again to return to a crazy state without even launching one of the containers.

Does anyone have a decision to bind two containers to each other or allow them to talk to each other in both directions?

+8
docker
source share
5 answers

One possible approach that does not use binding is to map containers by displaying a port in each container and binding 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 

Thus, container1 can access container2 through localhost: 6666 and container2 can access container1 through localhost: 5555.

This link is not , but bidirectional connectivity does not exist .

The documentation for docker networks explains this further.

+15
source share

Containers on the same network are connected to each other.

You need to create a network for these containers.

 docker network create --driver bridge isolated_network 

When starting containers, you must 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 have created containers, you can ping other containers on the same network using only the container name.

 root@container1 ping container2 root@container2 ping container1 
+11
source share

Another approach is to connect containers by binding ports to the docker0 interface. All docker containers are connected to this bridge by default (which usually has an IP address of 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] 

Containers can access each other through 172.17.42.1 and a specific port.

A similar solution, for example, @wassgreen, but has the advantage that containers do not have access to host interfaces.

See Extended Network --net=host section :

[...] but it allows containers to open ports with a low number, like any other root process. It also allows the container to access local network services such as D-bus. This can cause processes in the container to perform unexpected actions, such as restarting the computer. You should use this option with caution.

For more information about this connection through the docker0 interface docker0 see Unortodox docker connection without links .

+9
source share

Check the -link option in docker docs. You can associate 2 containers with the following command:

 $ docker run -i -t --name container_web --link container_db image_name 
+1
source share

The bridge or host network works when 2 containers run on the same host. If two containers work on different hosts, they will not be able to talk to each other.

For two containers on different hosts, to talk to each other, you should use a docker overlay network .

Docker -link is an obsolete function, as stated in the docker links doc .

+1
source share

All Articles