In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve Docker link container interconnection, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
1.1.Network access between containers through IP
Create two new containers, tomcat01 and tomcat02
Docker run-d-P-- name tomcat01 tomcatdocker run-d-P-- name tomcat02 tomcat
Use the ifconfig command to view the Nic information of toncat01:
As you can see, 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 the container tomcat01 and tomcat02 can ping:
Tomcat01 ping tomcat02:
Tomcat02 ping tomcat01:
According to the above two figures, both tomcat01 ping tomcat02 and tomcat02 ping tomcat01 can be ping connected.
Note: if there are no ifconfig commands and ping commands in the container, execute the following commands in turn:
Apt-get updateapt install iputils-pingapt install net-tools1.2, network access between containers through container name or container id
If you want to make 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 (linked container) and the receiving container (active unlinked container) 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.
-format of link
-- link: alias
-link adds a link to another container
Name and id are the source container's name and id,alias are aliases for the source container under link.
-examples of using link
Create a container tomcat03, and use tomcat03 as the receiving container (the container that is actively linked), and the above tomcat01 (alias T1) as the source container (the linked container). The two containers are linked:
Docker run-d-P-- name tomcat03-- link tomcat01:t1 tomcat
Tomcat01 is the name of the 7b94f50c43ea container launched above. Here as the source container, T1 is the alias (alias) of the container under link. In an easy-to-understand way, from the point of view of the tomcat03 container, tomcat01 and T1 are the names of the 7b94f50c43ea container, and hostname,tomcat03 as a container can access and communicate with the 7b94f50c43ea container with either of these two names (docker is automatically parsed through DNS).
Conduct link testing: tomcat03 ping tomcat01
Ping tomcat01
Ping t1
Both can ping, so you can see that both tomcat01 and T1 point to 172.17.0.2.
However, the above link is only one-way, that is, the receiving container can only be linked to the source container, and the source container cannot be linked to the receiving container, that is, if the tomcat03 link is tomcat01,tomcat03, it can ping the tomcat01,tomcat01 and not the tomcat03,tomcat01ping different tomcat03. But it does not affect tomcat01 through IP ping tomcat03 or tomcat03 ping tomcat01.
-link principle
Check the hosts file of tomcat03. The operating system stipulates that before making a DNS request, check whether there is a mapping between this domain name and IP in your own hosts file. If so, directly access the network location specified by the IP address, and if not, make a domain name resolution request to the known DNS server.
Docker exec-it tomcat03 cat / etc/hosts
In tomcat03's hosts configuration file, you can see the IP, container name, alias, and container id mapped to tomcat01, and all tomcat03 can communicate with tomcat01 through the specified container name.
-link adds a name resolution about the tomcat01 container to the receiving container (in this case, the container named tomcat003). With this name resolution, you can communicate with the source container without using ip. In addition, when the source container is restarted, docker is responsible for updating the / etc/hosts file, so you don't have to worry about the IP address changing after the container restart, and the resolution cannot take effect.
The above is all the contents of the article "how to interconnect containers in Docker link". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.