docker专项(五)容器之间相互通讯

本文我们简单说一下,容器之间是怎么相互他通讯的,假设我们一个容器只有php,一个容器只有nginx,一个容器只有mysql或者redis,我们想要php容器去链接mysql或者redis应该怎么做呢

首先,如果我们在没有指定网络的情况一下,一般容器是会使用默认的网络,也就是bridge ,
我们看下docker网络列表

[root@work home]# docker network list
NETWORK ID     NAME      DRIVER    SCOPE
5927b735123e   bridge    bridge    local
c71ab2fd1bcf   host      host      local
707911944ab8   none      null      local

然后启动3个docker

[root@work home]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS      NAMES
4e18e2880b5e   centos:7.6.1810   "/bin/bash"              27 minutes ago   Up 27 minutes              centostest
fbfd416582a0   nginx:latest      "/docker-entrypoint.…"   37 minutes ago   Up 37 minutes   80/tcp     nginxtest
c3e05f45b3a1   php:7.4.27-fpm    "docker-php-entrypoi…"   48 minutes ago   Up 48 minutes   9000/tcp   test

我们随意进入一个容器里面

[root@work home]# docker exec -it centostest /bin/bash
[root@4e18e2880b5e /]# ping nginxtest
ping: nginxtest: Name or service not known

发现是无法识别的,我们可以通过一下操作来创建一个网络(因为默认网络不带dns)

#创建网络
docker network create jinan
#将容器nginxtest加入到网络中
docker network connect jinan nginxtest
#将容器centostest加入到网络中
docker network connect jinan centostest
#将容器test加入到网络中
docker network connect jinan test

现在可以先看下网络列表

[root@work home]# docker network list
NETWORK ID     NAME      DRIVER    SCOPE
5927b735123e   bridge    bridge    local
c71ab2fd1bcf   host      host      local
b48d23bb5492   jinan     bridge    local
707911944ab8   none      null      local

可以查看下使用该网络的有哪些容器

[root@work home]# docker network inspect jinan
[
    {
        "Name": "jinan",
        "Id": "b48d23bb5492d6fa2b02042dabbca54715d4e0070b8104bff6ece718f8b5a29d",
        "Created": "2022-01-14T17:57:39.983008369+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": {
            "4e18e2880b5e31b6f1cc4f1fbcf837a52f311e8d5b71d516f964701ae0fba7ad": {
                "Name": "centostest",
                "EndpointID": "9ce4e741a8ac48f5d58d85819da2b68dc8cce7820a714ba39f0daa7e555e198f",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "c3e05f45b3a16d77d98c57feaff9e5a9fd7e3ca6e4a9d4af0853fc2538cab82f": {
                "Name": "test",
                "EndpointID": "6594cd35fa6594027ea229ee93f32e39c1e0bd02d8050c65af8a0c98e319671a",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            },
            "fbfd416582a011079813442553b3b9e5bfdc97aeda4d1da5100adaa0813c681e": {
                "Name": "nginxtest",
                "EndpointID": "701387ca6762be60cef0702ff0dc0e4c51230f7d80c2dae3870b64224eae8ec6",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

从Containers可以看到刚加入的三个容器都在这个网络里
进入任意一个容器中

[root@work home]# docker exec -it centostest /bin/bash
[root@4e18e2880b5e /]# ping nginxtest
PING nginxtest (172.18.0.2) 56(84) bytes of data.
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=4 ttl=64 time=0.061 ms
64 bytes from nginxtest.jinan (172.18.0.2): icmp_seq=5 ttl=64 time=0.052 ms

OK 已经实现了容器之间的通讯了,已经创建了网络的,以后启动容器可以通过 --network=jinan来直接加入到jinan网络中

你可能感兴趣的:(docker专项(五)容器之间相互通讯)