Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Communication between Docker containers

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to realize the communication between Docker containers? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

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

Use docker to create a new network (the-d bridge network driver is bridge), and connect both containers to the network for data exchange.

The methods are as follows:

Create your own network

[root@docker] # docker network create-d bridge my-bridge01f3d92281d7dd6eaeabd77c95c62f0d33f8d45bd0e1bc7c7e1c3208526dfd2a

Create a network, the-d bridge network drive is bridge and the name is mybridge

View the network that has been created:

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 3600th done" 44d549f38e2a38d528d26193c5585f8c68008411d764ad39d23dc57ee48ab28c

-network my-bridge: specifies that the network used by the container is a self-built my-bridge.

Use the container launched by the self-built network with an ip of 172.18.0.2

Start another container test4. Do not use-network to specify the network, but 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 the container test3 and test4 are not associated, no matter you use the container name or the container IP, you cannot communicate with ping:

Add test4 to the self-built my-bridge

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

Usage: Usage: docker network connect [OPTIONS] NETWORK CONTAINER

If you view test4, you will have one more virtual network card and be in the same network segment as test3.

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

Check the details 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 Attachable 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 IPv6Address 16", "IPv6Address": "}," c64c2993e94a3dc0359a0e38ed5f5d262c0b01be45bacdf1788c7b0c2223777b ": {" Name ":" test4 "," EndpointID ":" 9b41a9ef9b3e6dabdaad52167642a74aa7e05f93ceec65768b311945c8f2c036 "," MacAddress ":" 02:42:ac:12:00:03 " "IPv4Address": "172.18.0.3plus 16", "IPv6Address": ""}}, "Options": {}, "Labels": {}}]

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

This is the answer to the question about how to communicate between Docker containers. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report