Docker internal communication between multiple containers

Docker is very convenient for us to use containers to solve the problem of service isolation operation. At the same time, it also creates a need for internal network communication between multiple containers. In fact, it can be achieved relatively simply. The method is: create a network group, and then require internal network The communicating containers are all added to this network group, and then the added containers are all on the same network segment, and they can communicate.

1. Create a network group

[root@ecs-b3bf-0225793 ~]# 
[root@ecs-b3bf-0225793 ~]# docker network create --driver bridge mq-bridge
5e9af94588646f049559ba049facc50c1c8b3c8a3d06f7cd8c44df5908bf0125
[root@ecs-b3bf-0225793 ~]# 
[root@ecs-b3bf-0225793 ~]# 
[root@ecs-b3bf-0225793 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
6f66df95e8ce        app_net             bridge              local
95746a181dfb        bridge              bridge              local
9c82ac52a997        docker_default      bridge              local
d86404029f06        docker_work         bridge              local
8d350f2a2209        host                host                local
5e9af9458864        mq-bridge           bridge              local
2f1631ba396f        none                null                local
[root@ecs-b3bf-0225793 ~]#

As above, our network group is ready to use: mq-bridge

2. Just specify this container when starting the container.

[root@ecs-b3bf-0225795 conf]# 
[root@ecs-b3bf-0225795 conf]# docker run -d -p 10911:10911 -p 10909:10909 \
-v /home/docker/rocketmq/logs:/root/logs \
-v /home/docker/rocketmq/store:/root/store \
-v /home/docker/rocketmq/conf/broker.conf:/opt/rocketmq-latest/conf/broker.conf \
--name rmqbroker --link rmqnamesrv:namesrv --net=mq-bridge \
-e "NAMESRV_ADDR=namesrv:9876" \
-e "MAX_POSSIBLE_HEAP=200000000" \
rocketmqinc/rocketmq:latest sh mqbroker 
3046c85168b8ee4e6f61c68412c7495c596b4c366eb9091d62205ad5f97eef34
[root@ecs-b3bf-0225795 rocketmq]# 

The most important information above: --net=mq-bridge

3. View the IP of the container in our network group

[root@ecs-b3bf-0225795 conf]# 
[root@ecs-b3bf-0225795 conf]# docker inspect mq-bridge

 ok, the IPs between multiple containers are there. If you change the corresponding IPs, you can communicate on the intranet.

Guess you like

Origin blog.csdn.net/yexiaomodemo/article/details/127111710