Docker Network

1、创建 apline1 alpine 2  两个容器

docker run -it --name alpine1 -d alpine
docker run -it --name alpine2 -d alpine

2、创建 一个 mynet 网络

docker network create -d bridge --subnet 172.19.0.0/16 --gateway 172.19.0.1 mynet

3、把 alpine2 容器加入 mynet 网络

docker network connect mynet alpine2

 4、创建 alpine3 容器 并加入 mynet 网络

docker run -it --name alpine3 --network mynet --ip 172.19.2.1 -d alpine

5、查看容器

[root@ ~docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                 NAMES
96e6839315e7        alpine              "/bin/sh"               alpine3
264784076b5d        alpine              "/bin/sh"               alpine2
28dfb21ea47b        alpine              "/bin/sh"               alpine1

6、查看网络

[root@ ~docker]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f494d9e871fc        bridge              bridge              local
0049c2e52fe2        host                host                local
7a9f67575661        mynet               bridge              local
d1e9a42f8a20        none                null                local

 7、查看 mynet 网络 明细

可以看到 apline 2,apline3 在 mynet 网络中

[root@~docker]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "7a9f675756611910f059c8f9ab928bd76f3855fa1840794d9c1569f36cbbf913",
        "Created": "2020-10-21T21:10:02.09035415+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
            "264784076b5d5360cccff022e4ab7b4d5ad79c0562cce824f5291b00a290822b": {
                "Name": "alpine2",
                "EndpointID": "d20d282b72180a47e53d30737586c6601362129ec217eacadb96dffa37c83cb1",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            },
            "96e6839315e7ea13b39afa483999ae7a6a8d9916eebebb416a5ceb1a93b26d08": {
                "Name": "alpine3",
                "EndpointID": "5c12559d5a6d9420400f76027bccce613316fa83e01c8744e98d4fa98d741c41",
                "MacAddress": "02:42:ac:13:02:01",
                "IPv4Address": "172.19.2.1/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

 8、查看 bridge 默认网络

可以看到 apline1,apline2 在 bridge 网络中

[root@ ~docker]# docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "f494d9e871fc411c959e89ec8b4208c5763203a30e83eee8c5f1c9766be06e1a",
        "Created": "2020-10-20T09:11:29.030016967+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
            "264784076b5d5360cccff022e4ab7b4d5ad79c0562cce824f5291b00a290822b": {
                "Name": "alpine2",
                "EndpointID": "dde38b0fb5e2f9d77a21ba28b6f29a0a3e365694a9d40c3d339cbb569beb6b2d",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "28dfb21ea47b8875a697ed5d4e8ade2e2e956c683cb32954185fe678593da93a": {
                "Name": "alpine1",
                "EndpointID": "2115e5adf7e50829932bcec4af8c1198474a7e40ca12e55c3cdeaf4a3dd9281a",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

9、登陆 alpine2

(1)查看 IP,看到有 2 个IP

/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:03  
          inet addr:172.17.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:25 errors:0 dropped:0 overruns:0 frame:0
          TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2194 (2.1 KiB)  TX bytes:1744 (1.7 KiB)

eth1      Link encap:Ethernet  HWaddr 02:42:AC:13:00:02  
          inet addr:172.19.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe13:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:40 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:3256 (3.1 KiB)  TX bytes:1902 (1.8 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:36 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2698 (2.6 KiB)  TX bytes:2698 (2.6 KiB)

(2)测试 

docker exec -it alpine2 /bin/sh

# ping alpine3 容器,可以通
ping -w 3 alpine3

PING alpine3 (172.19.2.1): 56 data bytes
64 bytes from 172.19.2.1: seq=0 ttl=64 time=0.056 ms
64 bytes from 172.19.2.1: seq=1 ttl=64 time=0.060 ms
64 bytes from 172.19.2.1: seq=2 ttl=64 time=0.059 ms

--- alpine3 ping statistics ---
4 packets transmitted, 3 packets received, 25% packet loss
round-trip min/avg/max = 0.056/0.058/0.060 ms

# ping alpine1 容器,默认网络不能使用容器名访问
ping -w 3 alpine1
ping: bad address 'alpine1'


# 但是可以通过 IP 访问
/ # ping -w3 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.090 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.064 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.086 ms

--- 172.17.0.2 ping statistics ---
4 packets transmitted, 3 packets received, 25% packet loss
round-trip min/avg/max = 0.064/0.080/0.090 ms

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页