How to implement communication between Docker containers

How to implement communication between Docker containers

Scenario: A laradock development environment (php7.3+mysql5.7) has been built locally. Now I want to use the laradock environment to run the same existing project, but the project data is in the mysql5.6 docker container. Now I need to connect the two containers to achieve data interaction.

I read a lot of information on the Internet related to "Communication between docker containers_How to achieve communication between containers in docker", and finally decided to use docker to create a new network (-d bridge network driver is bridge), connect both containers to the network, and achieve data interaction.

Here’s how:

Create a network yourself

[root@docker ~]# docker network create -d bridge my-bridge
01f3d92281d7dd6eaeabd77c95c62f0d33f8d45bd0e1bc7c7e1c3208526dfd2a

Create a network, -d bridge network driver is bridge, named mybridge

View the created network:

insert image description here

Start a container and use the self-built network my-bridge

[root@docker ~]# docker run -d --name test3 --network my-bridge cgy/mycentos:v2 /bin/bash -c "while true;do sleep 3600;done"

44d549f38e2a38d528d26193c5585f8c68008411d764ad39d23dc57ee48ab28c

–network my-bridge: Specifies that the network used by the container is the self-built my-bridge.

The container started using a self-built network, the IP is 172.18.0.2

insert image description here

Start another container test4, do not use –network to specify the network, and use the docker0 network by default:

[root@docker ~]# docker run -d --name test4 cgy/mycentos:v2 /bin/bash -c "while true;do sleep 3600;done"
c64c2993e94a3dc0359a0e38ed5f5d262c0b01be45bacdf1788c7b0c2223777b

When containers test3 and test4 are not associated, pinging is unsuccessful regardless of whether the container name or the container IP is used:

insert image description here

Add test4 to the self-built my-bridge

[root@docker ~]# docker network connect my-bridge test4

Usage: docker network connect [OPTIONS] NETWORK CONTAINER

Check test4 and you will see that there is one more virtual network card, and it is in the same network segment as test3.

insert image description here

At this time, you can directly use the container name to communicate

insert image description here

Check the detailed information of my-bridge again

[root@docker ~]# docker network inspect my-bridge
[
    {
        "Name": "my-bridge",
        "Id": "01f3d92281d7dd6eaeabd77c95c62f0d33f8d45bd0e1bc7c7e1c3208526dfd2a",
        "Created": "2018-05-01T01:08:04.864178764+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "44d549f38e2a38d528d26193c5585f8c68008411d764ad39d23dc57ee48ab28c": {
                "Name": "test3",
                "EndpointID": "95f13c730e08f7c82e5f3ec555f3c3afa79a50eceb7a1f540463ba181c81d05c",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "c64c2993e94a3dc0359a0e38ed5f5d262c0b01be45bacdf1788c7b0c2223777b": {
                "Name": "test4",
                "EndpointID": "9b41a9ef9b3e6dabdaad52167642a74aa7e05f93ceec65768b311945c8f2c036",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

As you can see, my-bridge has connected two containers, test3 and test4.

Reference: https://www.jianshu.com/p/a9dce5179e31

This is the end of this article about how to implement communication between Docker containers. For more information about Docker container communication, 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:
  • Operations of communication between Docker containers and external network communication
  • Implementation of Docker container connection and communication
  • Detailed explanation of Docker container cross-host multi-network segment communication solution
  • Detailed explanation of direct routing in cross-host communication of Docker containers
  • Detailed explanation of how Docker containers communicate across hosts
  • Detailed explanation of a method of communication between docker containers

<<:  CSS3 diamond puzzle realizes the function of rotating only div and not rotating the background image

>>:  Details on using bimface in vue

Recommend

Web design tips on form input boxes

This article lists some tips and codes about form...

MySQL Optimization Summary - Total Number of Query Entries

1. COUNT(*) and COUNT(COL) COUNT(*) usually perfo...

Summary of data interaction between Docker container and host

Preface When using Docker in a production environ...

Detailed Tutorial on Installing VirtualBox 6.0 on CentOS 8 / RHEL 8

VirtualBox is a free and open source virtualizati...

VMware Workstation Pro installs Win10 pure version operating system

This article describes the steps to install the p...

Share the pitfalls of MySQL's current_timestamp and their solutions

Table of contents MySQL's current_timestamp p...

The difference between shtml and html

Shtml and asp are similar. In files named shtml, s...

The latest 36 high-quality free English fonts shared

01. Infinity Font Download 02. Banda Font Download...

Application and implementation of data cache mechanism for small programs

Mini Program Data Cache Related Knowledge Data ca...

MySQL sequence AUTO_INCREMENT detailed explanation and example code

MySQL sequence AUTO_INCREMENT detailed explanatio...

How to use Linux commands in IDEA

Compared with Windows system, Linux system provid...

Use of Linux passwd command

1. Command Introduction The passwd command is use...