Docker的网络

利用linux的网络命名空间能够让docker之间进行网络访问,具体可以参考一下这篇文章:
https://blog.csdn.net/sld880311/article/details/77650937

Docker的网络_第1张图片

当 Docker 启动时,会自动在主机上创建一个 docker0 虚拟网桥,实际上是 Linux 的一个 bridge,可以理解为一个软件交换机。它会在挂载到它的网口之间进行转发。

1.容器之间的link

[root@VM_0_5_centos ~]# docker run -d --name test1 busybox /bin/sh -c "while true;do sleep 3600;done"
661f4c840e173abea5cb2c43ba47c9472ab2cfc6e1bbc7fa9ef38721197f6864
[root@VM_0_5_centos ~]# docker run -d --name test2 --link test1  busybox /bin/sh -c "while true;do sleep 3600;done"
3208cf96c2ec4b66a21a60471cde2340494dd2ac34f3bb188ac3254b41181d74

进入到test2这个docker中可以ping test1

[root@VM_0_5_centos ~]# docker exec -it test2 /bin/sh
/ # ping test1
PING test1 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.091 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.062 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.064 ms

2.创建一个自定义的bridge,让container连接这个bridge

2.1创建bridge

[root@VM_0_5_centos ~]# docker network create -d bridge my-bridge
b8f0160eaa5869838736535ba8461f41aee974236bab369b9d1c9bf863860231

2.2连接这个bridge

[root@VM_0_5_centos ~]# docker run -d --name test3 --network my-bridge busybox /bin/sh -c "while true;do sleep 3600;done"
c0a10dc189817469c87576022ce61840bc0101a700b083647451ca63548415d3

2.3查看连接情况

[root@VM_0_5_centos ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
57e3ff55d2a5        bridge              bridge              local
a1452e108ecf        host                host                local
b8f0160eaa58        my-bridge           bridge              local
a85a055e33a2        net_basic           bridge              local
f80f89b1a446        none                null                local
[root@VM_0_5_centos ~]# docker network inspect b8f0160eaa58
[
    {
        "Name": "my-bridge",
        "Id": "b8f0160eaa5869838736535ba8461f41aee974236bab369b9d1c9bf863860231",
        "Created": "2019-11-17T10:35:20.025898124+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "c0a10dc189817469c87576022ce61840bc0101a700b083647451ca63548415d3": {
                "Name": "test3",
                "EndpointID": "e52bce4bed58f73971e6c7a3b89e7949d739e9f5ab5d709c4c6af4119c01752e",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

2.4让已经启动的容器去连接我们自定义的bridge

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

如果容器连接自定义的bridge,那么容器之间可以通过名称来ping。但是bridge0做不到这样。

3.容器的端口映射

其实就是希望把docker的端口映射到本地中来。

[root@VM_0_5_centos ~]# docker run --name web -d -p 80:80 nginx
0e75fec1bb1b50be02cd43c6e62f887e0907352fe97373d071ca219dde664b63

第一个80是本地端口,第二个80是docker的端口


Docker的网络_第2张图片

4.多容器复杂应用部署

4.1启动redis

[root@VM_0_5_centos ~]# docker run -d --name redisdocker redis
362d8efb991af997174bbe804e8d55188d370a9673e6b7907cd37586bc69cd2d

4.2启动python应用对应的container

docker build -t mikezzmeric/zzmredis .
docker run -d --link redisdocker --name flask-redis -e REDIS_HOST=redisdocker mikezzmeric/zzmredis
docker exec -it flask-redis

4.3进入容器中

docker exec -it flask-redis /bin/bash

4.4访问应用

root@c699a0940703:/app# curl 127.0.0.1:5000
Hello Container World! I have been seen 1 times and my hostname is c699a0940703.

4.5停掉应用,让外层的宿主机访问此应用

docker run -d -p 5000:5000 --link redisdocker --name flask-redis -e REDIS_HOST=redisdocker mikezzmeric/zzmredis

其中的-e表示设置环境变量

你可能感兴趣的:(Docker的网络)