Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Docker learning | container interconnection

This paper studies how to communicate between two Docker containers

–link

The preparatory work

  • Start two Tomcats using Docker
docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat02 tomcat
Copy the code

Ping Tomcat02 using the service name of Tomcat01

docker exec -it tomcat01 ping tomcat02
Copy the code

It turned out that the ping didn’t work

How can it be solved?

We need these two container services to connect to the network

Consider the following scenario: we write a microservice, the URL of the database is IP, but the IP of the database is changed, we need to modify the code to restart the project to solve the problem. So, can we use the service name to access the database, even if the IP address is changed, without affecting our project?

–link

Container connection via --link
docker run -d -P --name tomcat03 --link tomcat02 tomcat
Copy the code

Test the

$ docker exec -it tomcat03 ping tomcat02

Pinging is successful
Copy the code

However, doing so will have a lot of pits, and it is very inconvenient, and can not reverse Ping

Tomcat02 – Link Tomcat03

Check out the Docker network

docker network ls

Check everything on the card

docker network inspect NETWORK_ID

Essential principle

— Link adds tomcat02’s address to hosts in Tomcat03

Is this it? It is not recommended for development

Docker0 does not support container name connection access

Custom network

The container of interconnected

View the current docker network

docker network ls

docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
51e24adb9482   bridge    bridge    local
3db21d51de9c   host      host      local
8d20e8fd6793   none      null      local
Copy the code

Remove the network

docker network rm <NAME>

Network mode

  • Bridge: Bridge mode (default)
  • None: The network is not configured
  • Host: Host mode: Shares the network with the host
  • Container: The network inside the container is connected

docker network --help

Commands: connect Connect a container to a network create Create a network disconnect Disconnect a container from a network inspect Display detailed information on one or more networks ls List networks prune Remove all unused networks rm Remove  one or more networksCopy the code

Empty container docker rm -f $before test (docker ps -aq)

IP addr Displays the current network adapter of the host

$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host LO valid_lft forever preferred_lft forever Inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 52:54:00:4c:83:06 brd Ff :ff:ff:ff:ff:ff :ff inet 10.0.8.10/22 BRD 10.0.11.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe4c:8306/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:fc:84:98:fb brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:fcff:fe84:98fb/64 scope link 
       valid_lft forever preferred_lft forever
Copy the code

test

Docker0: net bridge: net bridge: net bridge
docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat01 --net bridge tomcat

# Docker0 features: 1. Default 2. Domain names cannot be accessed
Copy the code

We can customize a network

docker create

docker network create --help

# We customize a network
# docker network create 
# --driver bridge 
# - subnet configures 192.168.0.0/16
# --gateway 192.168.0.1
# mynet$docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynetMynet, subnet [192.168.0.0/16] supports up to 65535 networks, default routing gateway [192.168.0.1]
de55be69be90cc51324b0dfcd6b1a25bf4d5cc2f91569208025a9f068565a383
# created successfully

# check
[root@VM-8-10-centos tomcat]$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
acaeca217cc5   bridge    bridge    local
3edbb9cd9ab0   host      host      local
de55be69be90   mynet     bridge    local  # mynet network created successfully
dad0ff6720e0   none      null      local
Copy the code

docker inspect mynet

[root@VM-8-10-centos tomcat]# docker inspect mynet[{"Name": "mynet"."Id": "de55be69be90cc51324b0dfcd6b1a25bf4d5cc2f91569208025a9f068565a383"."Created": "The 2021-09-29 T23:54:40. 88159831 + 08:00." "."Scope": "local"."Driver": "bridge"."EnableIPv6": false."IPAM": {
            "Driver": "default"."Options": {},
            "Config": [{"Subnet": "192.168.0.0/16"."Gateway": "192.168.0.1"}},"Internal": false."Attachable": false."Ingress": false."ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false."Containers": {},
        "Options": {},
        "Labels": {}}]At this point, our own network has been created
Copy the code

Test: Create a Tomcat container and use the network we created

$ docker run -d -P --name tomcat-net-01 --net mynet mytomcat
$ docker run -d -P --name tomcat-net-02 --net mynet mytomcat
Copy the code

Check the mynet

$ docker inspect mynet

[
    {
        "Name": "mynet"."Id": "de55be69be90cc51324b0dfcd6b1a25bf4d5cc2f91569208025a9f068565a383"."Created": "The 2021-09-29 T23:54:40. 88159831 + 08:00." "."Scope": "local"."Driver": "bridge"."EnableIPv6": false."IPAM": {
            "Driver": "default"."Options": {},
            "Config": [{"Subnet": "192.168.0.0/16"."Gateway": "192.168.0.1"}},"Internal": false."Attachable": false."Ingress": false."ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false.Tomcat01 and Tomcat02 containers exist on myNet
        "Containers": {
            "52514945040be96967dbc982f4cbe86a07c6303b51931595c645fc8f0deb9570": {
                "Name": "tomcat-net-02"."EndpointID": "d2bcd6a479f20e20575c03d6309a0d2790d9867a47325f11c140db25e792569f"."MacAddress": "02:42:c0:a8:00:03"."IPv4Address": "192.168.0.3/16"."IPv6Address": ""
            },
            "e629bb8bcd66a2f43dbb617d9decc35d40bd1678c81a217d159057a4443aed22": {
                "Name": "tomcat-net-01"."EndpointID": "e2e501168303a5e33464ba0fb4d8838bd6a6a9f09545026b7a900b4abb8833a4"."MacAddress": "02:42:c0:a8:00:02"."IPv4Address": "192.168.0.2/16"."IPv6Address": ""}},"Options": {},
        "Labels": {}}]Copy the code

Can tomcat-net-01 ping tomcat-net-02 directly

$ docker exec-it tomcat-net-01 ping 192.168.0.3 ping 192.168.0.3 (192.168.0.3) 56(84) bytes of data. 64 bytes from 192.168.0.3: Icmp_seq =1 TTL =64 time=0.117 ms 64 bytes from 192.168.0.3: ICmp_seq =2 TTL =64 time=0.054 ms 64 bytes from 192.168.0.3: Icmp_seq =3 TTL =64 time=0.063 ms 64 bytes from 192.168.0.3: ICmp_seq =4 TTL =64 time=0.056 ms 64 bytes from 192.168.0.3: icmp_seq=4 TTL =64 time=0.056 ms 64 bytes from 192.168.0.3: Icmp_seq =5 TTL =64 time=0.054 ms 64 bytes from 192.168.0.3: ICmp_seq =6 TTL =64 time=0.058 ms 64 bytes from 192.168.0.3: icmp_seq=6 TTL =64 time=0.058 ms 64 bytes from 192.168.0.3: Icmp_seq =7 TTL =64 time=0.054 ms 64 bytes from 192.168.0.3: ICmp_seq =8 TTL =64 time=0.047 ms 64 bytes from 192.168.0.3: icmp_seq=8 TTL =64 time=0.047 ms 64 bytes from 192.168.0.3: Icmp_seq =9 TTL =64 time=0.052 ms ^C -- 192.168.0.3 ping statistics --Copy the code

Success!

The custom network Docker has helped us maintain the corresponding relationship, and it is recommended to use the network in this way