Original article, welcome to reprint. Reprint please specify: reprint from IT people story, thank you! Link between containers (27)

The default network Bridge was introduced last time. After connecting to Docker0, you can also communicate with the outside world. Check the relationship between dockers link. Test1 and test2 were generated in the previous section. Test1 and test2 used to communicate with each other via IP addresses.

scenario

If you create two containers, one mysql container and one Tomcat container, the background application in the Tomcat container needs to access the mysql database container, according to the principle of the previous section, I need to enter the mysql container to check the IP address of mysql. Then modify the database connection address in the Tomcat container application to complete the application connection. Is that too much trouble. In fact, there is a link way to make application communication become simple

  • Start test1 and test2
sudo docker ps
Copy the code



  • Stop test2 and delete test2
sudo docker stop test2
sudo docker rm test2
sudo docker ps
Copy the code



image.png

  • Add link to create test2
sudo docker run -d --name test2 --link test1 busybox /bin/sh -c "while true; do sleep 3600; done"
sudo docker ps
sudo docker exec -it test2 /bin/sh
#ping test1Ping 172.17.0.2 pingtest1
Copy the code

Test1 link test1 link test1 link test1







  • If I ping test2 from test1, let’s try it
sudo docker exec -it test1 /bin/sh
ping test2
Copy the code



Docker network ls bridge Host None

We create our own bridge and have the container bind the new bridge directly




sudo docker network create -d bridge mybridge 
sudo docker network ls
brctl show
Copy the code






image.png

Create a new myBridge from test3
sudo docker run -d --name test3 --network mybridge busybox /bin/sh -c "while true; do sleep 3600; done"
There is a new interface on myBridge
brctl show
Copy the code







# Check out the new MyBridage network
sudo docker network inspect mybridge
Copy the code

It used to be 172.17 and this is 172.18




So the question is, if you have a 17, an 18 network segment how do you get a 17 network segment container to connect to the 18 network segment

sudo docker network 
sudo docker network connect
Copy the code



Let's connect to the mybridage network
 sudo docker network connect mybridge test2
 sudo docker network inspect mybridge
Copy the code



image.png

Let’s ping test2 from test3 to see if it works

sudo docker exec -it test3 /bin/sh
Ping IP address of test2Ping 172.18.0.3#ping test2
ping test2
Copy the code



Test2 can be pinged from link to test2 by name. Test2 and test3 are connected to the bridge created by the user, so they are connected by default. Let’s try ping test3 from test2.

sudo docker exec -it test2 /bin/sh
ip a
ping test3
#ping test1 can be pinged because we have already linked it. If the ping fails, you can use sudo Docker network connect myBridge test1 to ping
ping test1
Copy the code






PS: user defined bridge and docker0 this bridge before their difference, docker0 if through the name to find the need to link, the actual project rarely use link, in fact, let everyone know link this command.