Three ways to communicate between Docker containers

Three ways to communicate between Docker containers

We all know that Docker containers are isolated from each other and cannot access each other, but what if there are some dependent services? The following three methods are introduced to solve the container mutual access problem.

Method 1: Virtual IP access

When installing Docker, Docker will create an internal bridge network docker0 by default. Each container created will be assigned a virtual network card, and containers can access each other based on IP.

[root@33fcf82ab4dd /]# [root@CentOS ~]# ifconfig
......
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
    inet6 fe80::42:35ff:feac:66d8 prefixlen 64 scopeid 0x20<link>
    ether 02:42:35:ac:66:d8 txqueuelen 0 (Ethernet)
    RX packets 4018 bytes 266467 (260.2 KiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 4226 bytes 33935667 (32.3 MiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
......

Run a centos image and check the IP address: 172.17.0.7

[root@CentOS ~]# docker run -it --name centos-1 docker.io/centos:latest
[root@6d214ff8d70a /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 172.17.0.7 netmask 255.255.0.0 broadcast 0.0.0.0
    inet6 fe80::42:acff:fe11:7 prefixlen 64 scopeid 0x20<link>
    ether 02:42:ac:11:00:07 txqueuelen 0 (Ethernet)
    RX packets 16 bytes 1296 (1.2 KiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 8 bytes 648 (648.0 B)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Run the same command to start another container and check the IP address: 172.17.0.8

[root@CentOS ~]# docker run -it --name centos-2 docker.io/centos:latest
[root@33fcf82ab4dd /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 172.17.0.8 netmask 255.255.0.0 broadcast 0.0.0.0
    inet6 fe80::42:acff:fe11:8 prefixlen 64 scopeid 0x20<link>
    ether 02:42:ac:11:00:08 txqueuelen 0 (Ethernet)
    RX packets 8 bytes 648 (648.0 B)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 8 bytes 648 (648.0 B)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

The results of the ping test inside the container are as follows:

[root@33fcf82ab4dd /]# ping 172.17.0.7
PING 172.17.0.7 (172.17.0.7) 56(84) bytes of data.
64 bytes from 172.17.0.7: icmp_seq=1 ttl=64 time=0.205 ms
64 bytes from 172.17.0.7: icmp_seq=2 ttl=64 time=0.119 ms
64 bytes from 172.17.0.7: icmp_seq=3 ttl=64 time=0.118 ms
64 bytes from 172.17.0.7: icmp_seq=4 ttl=64 time=0.101 ms

This method requires knowing the IP address of each container, which is not practical in actual use.

Method 2: link

Add parameters when running the containerlink

Run the first container

docker run -it --name centos-1 docker.io/centos:latest

Run the second container

[root@CentOS ~]# docker run -it --name centos-2 --link centos-1:centos-1 docker.io/centos:latest

--link: The first centos-1 in the parameter is the container name, and the second centos-1 is the defined container alias (use the alias to access the container). For ease of use, the alias generally defaults to the container name.

The test results are as follows:

[root@e0841aa13c5b /]# ping centos-1
PING centos-1 (172.17.0.7) 56(84) bytes of data.
64 bytes from centos-1 (172.17.0.7): icmp_seq=1 ttl=64 time=0.210 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=2 ttl=64 time=0.116 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=3 ttl=64 time=0.112 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=4 ttl=64 time=0.114 ms

This method has requirements on the order in which containers are created. If multiple containers in the cluster need to access each other, it is not very convenient to use.

Method 3: Create a bridge network

1. After installing Docker, run the following command to create a bridge network: docker network create testnet

The newly created bridge testnet is queried.

2. Run the container to connect to the testnet network.

Usage: docker run -it --name <container name> ---network <bridge> --network-alias <network alias> <image name>

[root@CentOS ~]# docker run -it --name centos-1 --network testnet --network-alias centos-1 docker.io/centos:latest
[root@CentOS ~]# docker run -it --name centos-2 --network testnet --network-alias centos-2 docker.io/centos:latest

3. Ping from one container to another. The test results are as follows:

[root@fafe2622f2af /]# ping centos-1
PING centos-1 (172.20.0.2) 56(84) bytes of data.
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=1 ttl=64 time=0.158 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=2 ttl=64 time=0.108 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=3 ttl=64 time=0.112 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=4 ttl=64 time=0.113 ms

4. If you want to access the service in the container, you can use this method to access <network alias>: <service port number>

It is recommended to use this method to customize the network. Because the network alias is used, you don’t have to worry about whether the IP changes. You can access each other as long as you connect to the bright network inside Docker. Multiple bridges can be established to isolate them in different network segments.

The above are the details of the three methods of Docker container mutual access. For more information about Docker container mutual access, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed graphic tutorial on how to enable remote secure access with Docker
  • Solution to the problem of not being able to access the home page when adding a tomcat container to Docker
  • How to directly access the docker for windows container intranet through an independent IP
  • A troubleshooting experience of centos Docker bridge mode unable to access the host Redis service
  • Detailed explanation of using Docker to build externally accessible MySQL
  • Detailed explanation of how to solve the problem that the docker container cannot access the host machine through IP
  • How to access the local machine (host machine) in Docker
  • How to use Docker container to access host network
  • Solution to the problem that docker nginx cannot be accessed after running
  • Method for accessing independent IP in Docker container

<<:  SQL merge operation of query results of tables with different columns

>>:  Vue project realizes login and registration effect

Recommend

Detailed explanation of Nginx regular expressions

Nginx (engine x) is a high-performance HTTP and r...

18 Web Usability Principles You Need to Know

You can have the best visual design skills in the...

Example code for implementing image adaptive container with CSS

There is often a scenario where the image needs t...

How to use module fs file system in Nodejs

Table of contents Overview File Descriptors Synch...

Detailed explanation of docker network bidirectional connection

View Docker Network docker network ls [root@maste...

Zabbix implements monitoring of multiple mysql processes

Three MySQL instance processes are started on one...

What are Web Slices?

IE8 new feature Web Slices (Web Slices) Microsoft...

Markup language - for

Click here to return to the 123WORDPRESS.COM HTML ...

JavaScript to achieve elastic navigation effect

This article shares the specific code for JavaScr...

Five things a good user experience designer should do well (picture and text)

This article is translated from the blog Usability...

Element UI table realizes drop-down filtering function

This article example shares the specific code for...

Markup language - CSS layout

Click here to return to the 123WORDPRESS.COM HTML ...

js returns to the previous page and refreshes the code

1. Javascript returns to the previous page history...