Docker link realizes container interconnection

Docker link realizes container interconnection

1.1. Network access between containers via IP

Create two new containers tomcat01 and tomcat02

docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat02 tomcat

insert image description here

Use the ifconfig command to view the network card information of toncat01:

insert image description here

You can see that the IP address of tomcat01 is 172.17.0.2

Then check the network card information of toncat02:

insert image description here

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:

insert image description here

tomcat02 pings tomcat01:

insert image description here

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 ID

If 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 format

--link <name or id>:alias

--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.

Examples of using –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

ping tomcat01

insert image description here

ping t1

insert image description here

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.

--link principle

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

insert image description here

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:
  • A brief discussion on the preliminary practice of Docker container interconnection
  • Detailed explanation of Docker port mapping and container interconnection
  • Docker learning notes: Weave realizes cross-host container interconnection
  • Detailed explanation of Docker container interconnection methods
  • Implementation of docker --link container interconnection

<<:  Founder font library Chinese and English file name comparison table

>>:  A brief discussion on the specific use of viewport in mobile terminals

Recommend

Tomcat configuration and how to start it in Eclipse

Table of contents How to install and configure To...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

Mysql queries the transactions being executed and how to wait for locks

Use navicat to test and learn: First use set auto...

Use semantic tags to write your HTML compatible with IE6,7,8

HTML5 adds more semantic tags, such as header, fo...

React realizes secondary linkage effect (staircase effect)

This article shares the specific code of React to...

Vue.set() and this.$set() usage and difference

When we use Vue for development, we may encounter...

Detailed explanation of the use of base tag in HTML

In requireJS, there is a property called baseURL....

Sharing some details about MySQL indexes

A few days ago, a colleague asked me a question a...

Detailed example code of mysql batch insert loop

background A few days ago, when I was doing pagin...

Docker data volume container creation and usage analysis

A data volume container is a container specifical...

MySQL Series Database Design Three Paradigm Tutorial Examples

Table of contents 1. Knowledge description of the...

Solution to no Chinese input method in Ubuntu

There is no solution for Chinese input method und...