Published 5月 31, 2019 by with 0 comment

4 - 02 - User-defined Network




We can create network by requirement.

1. Creating a network called Net_192_168_1, subnet (192.168.1.0/24) and gateway 192.168.1.1
peter@peter-KVM:~$  docker network create --driver bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 NET_192_168_1
fe87b5c03fe4579682b2c2d3441107a6a0a66ec74a5dbbbde044b6240598cbae

2. Verify
peter@peter-KVM:~$  brctl show
bridge name     bridge id               STP enabled     interfaces
br-fe87b5c03fe4         8000.0242850aa602       no
docker0         8000.024219252d6f       no

peter@peter-KVM:~$  docker network inspect NET_192_168_1 
[
    {
        "Name": "NET_192_168_1",
        "Id": "fe87b5c03fe4579682b2c2d3441107a6a0a66ec74a5dbbbde044b6240598cbae",
        "Created": "2019-05-30T23:47:30.927385608+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.1.0/24",
                    "Gateway": "192.168.1.1"
                }
            ]
        },

peter@peter-KVM:~$  ifconfig
br-fe87b5c03fe4 Link encap:Ethernet  HWaddr 02:42:85:0a:a6:02  
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

docker0   Link encap:Ethernet  HWaddr 02:42:19:25:2d:6f  
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:19ff:fe25:2d6f/64 Scope:Link
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:136565 errors:0 dropped:0 overruns:0 frame:0
          TX packets:189858 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:9298486 (9.2 MB)  TX bytes:295225784 (295.2 MB)

3. Create a container named Busybox1 and uses this NET_192_168_1, and get a IP from DHCP.
peter@peter-KVM:~$  docker run -itd --name=Busybox1 --network=NET_192_168_1 busybox sleep 3000
639735b021f27bf01df5dfbdcc9b170973bf276274f21626c041b56508412554 

4. Verify the Busybox1 information
peter@peter-KVM:~$  docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
639735b021f2        busybox             "sleep 3000"        18 seconds ago      Up 17 seconds                           Busybox1

peter@peter-KVM:~$  docker exec -it Busybox1 sh
/ # 
/ # ip a
1: lo:  mtu 65536 qdisc noqueue 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
123: eth0@if124:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:01:02 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 
/ # ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: seq=0 ttl=64 time=0.157 ms
64 bytes from 192.168.1.1: seq=1 ttl=64 time=0.132 ms

5. Create a container named Busybox2 and uses this NET_192_168_1, and IP address (192.168.1.10/24)
peter@peter-KVM:~$ docker run -itd --name "Busybox2" --network NET_192_168_1  --ip 192.168.1.10 busybox sleep 3000
168b6c19dd04ea2672ee64b5653896fefcdbc51952b1b5c63bf7312df7b7a7a2

6. Verify the Busybox2 information
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
168b6c19dd04        busybox             "sleep 3000"        4 seconds ago       Up 2 seconds                            Busybox2
639735b021f2        busybox             "sleep 3000"        4 minutes ago       Up 4 minutes                            Busybox1

$ docker exec -it Busybox2 sh
/ # 
/ # ip a
1: lo:  mtu 65536 qdisc noqueue 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
125: eth0@if126:  mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:01:0a brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 
/ # ping 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: seq=0 ttl=64 time=0.155 ms
64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.118 ms
^C
--- 192.168.1.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.118/0.136/0.155 ms
/ # 
/ # ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: seq=0 ttl=64 time=0.140 ms
64 bytes from 192.168.1.1: seq=1 ttl=64 time=0.146 ms
^C
--- 192.168.1.1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.140/0.143/0.146 ms
/ # 
7. Ping from host to Busybox1 and Busybox2
peter@peter-KVM:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
br-fe87b5c03fe4         8000.0242850aa602       no              veth2f4208f
                                                        vethba7521f
docker0         8000.024219252d6f       no
peter@peter-KVM:~$ 
peter@peter-KVM:~$ ping -c 2 192.168.1.2
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.077 ms

--- 192.168.1.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1014ms
rtt min/avg/max/mdev = 0.077/0.077/0.078/0.008 ms
peter@peter-KVM:~$ 
peter@peter-KVM:~$ ping -c 2 192.168.1.10
PING 192.168.1.10 (192.168.1.10) 56(84) bytes of data.
64 bytes from 192.168.1.10: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 192.168.1.10: icmp_seq=2 ttl=64 time=0.107 ms

--- 192.168.1.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1020ms
rtt min/avg/max/mdev = 0.079/0.093/0.107/0.014 ms
peter@peter-KVM:~$ 
peter@peter-KVM:~$ docker network  inspect NET_192_168_1 
[
    {
        "Name": "NET_192_168_1",
        "Id": "fe87b5c03fe4579682b2c2d3441107a6a0a66ec74a5dbbbde044b6240598cbae",
        "Created": "2019-05-30T23:47:30.927385608+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.1.0/24",
                    "Gateway": "192.168.1.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "168b6c19dd04ea2672ee64b5653896fefcdbc51952b1b5c63bf7312df7b7a7a2": {
                "Name": "Busybox2",
                "EndpointID": "f004cc97f7c5f66d4ba878d93059e7974d2129c495141feeb093dc550b4a19e7",
                "MacAddress": "02:42:c0:a8:01:0a",
                "IPv4Address": "192.168.1.10/24",
                "IPv6Address": ""
            },
            "639735b021f27bf01df5dfbdcc9b170973bf276274f21626c041b56508412554": {
                "Name": "Busybox1",
                "EndpointID": "1177ef70b58772164371aee3a5afa295b68244604e05cba7d61308376b666eed",
                "MacAddress": "02:42:c0:a8:01:02",
                "IPv4Address": "192.168.1.2/24",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
peter@peter-KVM:~$ 



最初發表 / 最後更新: 2019.05.31 / 2019.05.31

0 comments:

張貼留言