1.1. Network access between containers via IPCreate two new containers tomcat01 and tomcat02 docker run -d -P --name tomcat01 tomcat docker run -d -P --name tomcat02 tomcat Use the ifconfig command to view the network card information of toncat01: You can see that the IP address of tomcat01 is 172.17.0.2 Then check the network card information of toncat02: As you can see, the IP address of tomcat02 is 172.17.03 Test whether containers tomcat01 and tomcat02 can ping each other: tomcat01 pings tomcat02: tomcat02 pings tomcat01: As shown in the two figures above, whether tomcat01 pings tomcat02 or tomcat02 pings tomcat01, both can be pinged successfully. Note: If there is no ifconfig command and ping command in the container, execute the following commands in sequence: apt-get update apt install iputils-ping apt install net-tools 1.2. Network access between containers through container name or container IDIf you want to establish a network connection between containers through the container name, you need to use docker run --link to link the two containers. –link can be used to link two containers so that the source container (the linked container) and the receiving container (the container that actively unlinks) can communicate with each other, and the receiving container can obtain some data of the source container, such as the environment variables of the source container.
--link Add a link to another container name and id are the name and id of the source container, and alias is the alias of the source container under the link.
Create a container tomcat03, let tomcat03 be the receiving container (the container that actively links), and the above tomcat01 (alias t1) be the source container (the linked container), and link the two containers: docker run -d -P --name tomcat03 --link tomcat01:t1 tomcat tomcat01 is the name of the 7b94f50c43ea container started above. It is used as the source container here, and t1 is the alias of the container under the link. In layman's terms, from the perspective of the tomcat03 container, tomcat01 and t1 are both the names of the 7b94f50c43ea container, and as the hostname of the container, tomcat03 can access and communicate with the 7b94f50c43ea container using either of these two names (docker automatically resolves through DNS). Perform a link test: tomcat03 ping tomcat01
Both can be pinged, which shows that tomcat01 and t1 are pointing to 172.17.0.2. However, the above link is only one-way, that is, only the receiving container can link to the source container, and the source container cannot link to the receiving container. That is, tomcat03 is linked to tomcat01, tomcat03 can ping tomcat01, but tomcat01 is not linked to tomcat03, and tomcat01 cannot ping tomcat03. However, it does not affect tomcat01 pinging tomcat03 via IP or tomcat03 pinging tomcat01.
Check the hosts file of tomcat03. The operating system stipulates that before making a DNS request, check whether there is a mapping relationship between the domain name and IP in the system's own hosts file. If yes, then the network location specified by the IP address is accessed directly. If no, then a domain name resolution request is made to a known DNS server. docker exec -it tomcat03 cat /etc/hosts In the hosts configuration file of tomcat03, you can see that the IP, container name, alias, and container ID of tomcat01 are mapped. Therefore, tomcat03 can communicate with tomcat01 through the specified container name. –link adds a name resolution for the tomcat01 container to the receiving container (here, the container named tomcat003). With this name resolution, you don't need to use IP to communicate with the source container. In addition, when the source container is restarted, Docker will be responsible for updating the /etc/hosts file, so you don't have to worry about the IP address changing after the container is restarted and the resolution not taking effect. This is the end of this article about how to use Docker link to interconnect containers. For more information about Docker container interconnection, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Founder font library Chinese and English file name comparison table
>>: A brief discussion on the specific use of viewport in mobile terminals
Table of contents How to install and configure To...
1. Docker imports local images Sometimes we copy ...
Recently, two accounts on the server were hacked ...
Table of contents The significance of standard co...
Use navicat to test and learn: First use set auto...
HTML5 adds more semantic tags, such as header, fo...
This article shares the specific code of React to...
Table of contents 1. Vue life cycle 2. Hook funct...
When we use Vue for development, we may encounter...
In requireJS, there is a property called baseURL....
A few days ago, a colleague asked me a question a...
background A few days ago, when I was doing pagin...
A data volume container is a container specifical...
Table of contents 1. Knowledge description of the...
There is no solution for Chinese input method und...