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

Summary of methods to prevent users from submitting forms repeatedly

Duplicate form submission is the most common and ...

Ubuntu 18.04 installs mysql 5.7.23

I installed MySQL smoothly in Ubuntu 16.04 before...

Vue makes a simple random roll call

Table of contents Layout part: <div id="a...

VMwarea virtual machine installation win7 operating system tutorial diagram

The installation process of VMwarea will not be d...

Nginx external network access intranet site configuration operation

background: The site is separated from the front ...

Is mysql a relational database?

MySQL is a relational database management system....

Mysql uses stored procedures to quickly add millions of data sample code

Preface In order to reflect the difference betwee...

HTML Nine-grid Layout Implementation Method

Diversifying website layouts is our front-end spe...

Docker installs Elasticsearch7.6 cluster and sets password

Starting from Elasticsearch 6.8, free users are a...

js native carousel plug-in production

This article shares the specific code for the js ...

Detailed explanation of JavaScript timers

Table of contents Brief Introduction setInterval ...

In-depth explanation of various binary object relationships in JavaScript

Table of contents Preface Relationships between v...

Implementation of CSS Fantastic Border Animation Effect

Today I was browsing the blog site - shoptalkshow...

Solution to Element-ui upload file upload restriction

question Adding the type of uploaded file in acce...