Cấu hình Docker Network trên window server 2019

Tóm tắt nội dung

Đây là cách sử dụng cơ bản để định cấu hình Docker Network.
Khi chạy các vùng chứa mà không chỉ định mạng, mạng [nat] mặc định sẽ được chỉ định.

				
					# display network list
PS C:\Users\Administrator> docker network ls
NETWORK ID          NAME                    DRIVER              SCOPE
835e5265281e        administrator_default   nat                 local
84fb0716e654        nat                     nat                 local
4a4ab2bf8de9        none                    null                local

# display details of [nat]
PS C:\Users\Administrator> docker network inspect nat
[
    {
        "Name": "nat",
        "Id": "84fb0716e654da933857ea23cf9b7bb45c615583af0df2586281dc7099786326",
        "Created": "2020-12-15T22:03:50.6680838-08:00",
        "Scope": "local",
        "Driver": "nat",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "windows",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.224.0/20",
                    "Gateway": "172.18.224.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.windowsshim.hnsid": "848A89F5-CFB3-4D77-818A-3DC49C4C6DFB",
            "com.docker.network.windowsshim.networkname": "nat"
        },
        "Labels": {}
    }
]

# [nat] is assigned as container network by default
PS C:\Users\Administrator> docker run mcr.microsoft.com/windows/servercore:1809 powershell -c "ipconfig"

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::f017:c45a:9576:257a%21
   IPv4 Address. . . . . . . . . . . : 172.18.232.152
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.18.224.1
				
			

Nếu bạn muốn chỉ định một mạng khác, hãy đặt như sau.

				
					# create network [network01] with [192.168.100.0/24] subnet
PS C:\Users\Administrator> docker network create -d "nat" --subnet "192.168.100.0/24" network01
b6ccadd1101d02118f69b1649e33e9b56f88152fd0ed56da647ba5feeb7d18e3
PS C:\Users\Administrator> docker network ls
NETWORK ID          NAME                    DRIVER              SCOPE
835e5265281e        administrator_default   nat                 local
84fb0716e654        nat                     nat                 local
b6ccadd1101d        network01               nat                 local
4a4ab2bf8de9        none                    null                local

# run a container with specifying [network01]
PS C:\Users\Administrator> docker run --net network01 mcr.microsoft.com/windows/servercore:1809 powershell -c "ipconfig"

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::6973:b329:3b79:68df%25
   IPv4 Address. . . . . . . . . . . : 192.168.100.206
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.100.1

# to attach the network to existing running container, set like follows
PS C:\Users\Administrator> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
43f4fd5c7833        srv.world/iis       "cmd"               10 seconds ago      Up 8 seconds        0.0.0.0:8081->80/tcp   quirky_hermann

PS C:\Users\Administrator> docker exec 43f4fd5c7833 powershell -c "ipconfig"

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::480c:fc3b:6b06:a363%25
   IPv4 Address. . . . . . . . . . . : 172.18.228.192
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.18.224.1

# attach network to specify an IP address in the subnet
PS C:\Users\Administrator> docker network connect --ip 192.168.100.10 network01 43f4fd5c7833
PS C:\Users\Administrator> docker exec 43f4fd5c7833 powershell -c "ipconfig"

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::480c:fc3b:6b06:a363%25
   IPv4 Address. . . . . . . . . . . : 172.18.228.192
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.18.224.1

Ethernet adapter vEthernet (Ethernet) 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::d9ca:8ddc:a838:cdc%29
   IPv4 Address. . . . . . . . . . . : 192.168.100.10
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.100.1

# to disconnect the network, set like follows
PS C:\Users\Administrator> docker network disconnect network01 43f4fd5c7833
PS C:\Users\Administrator> docker exec 43f4fd5c7833 powershell -c "ipconfig"

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::480c:fc3b:6b06:a363%25
   IPv4 Address. . . . . . . . . . . : 172.18.228.192
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.18.224.1
				
			

Để xóa mạng docker, hãy đặt như sau.

				
					PS C:\Users\Administrator> docker network ls
NETWORK ID          NAME                    DRIVER              SCOPE
835e5265281e        administrator_default   nat                 local
84fb0716e654        nat                     nat                 local
b6ccadd1101d        network01               nat                 local
4a4ab2bf8de9        none                    null                local

# remove [network01]
PS C:\Users\Administrator> docker network rm network01
network01
# remove networks which containers don't use at all
PS C:\Users\Administrator> docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
administrator_default
				
			

Để kết nối với mạng Máy chủ, không phải nat, hãy đặt như sau.

				
					# confirm network adapter name
PS C:\Users\Administrator> Get-NetAdapter

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
vEthernet (nat)           Hyper-V Virtual Ethernet Adapter              9 Up           00-15-5D-9F-81-EC        10 Gbps
Ethernet                  Intel(R) 82574L Gigabit Network Conn...       5 Up           52-54-00-05-80-B8         1 Gbps

# create [transparent01] network with [transparent] driver
# for [com.docker.network.windowsshim.interface="***"], specify adapter name confirmed above
PS C:\Users\Administrator> docker network create -d transparent -o com.docker.network.windowsshim.interface="Ethernet" transparent01
PS C:\Users\Administrator> docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
84fb0716e654        nat                 nat                 local
4a4ab2bf8de9        none                null                local
83d4393b8873        transparent01       transparent         local

PS C:\Users\Administrator> docker network inspect transparent01
[
    {
        "Name": "transparent01",
        "Id": "83d4393b8873869ea6965af480b4d45ddc61995615eb96e4b4e83ce6f4c1d0e9",
        "Created": "2020-12-15T23:57:06.3332974-08:00",
        "Scope": "local",
        "Driver": "transparent",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "windows",
            "Options": {},
            "Config": [
                {
                    "Subnet": "0.0.0.0/0"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "f655e188d62d5c595a83b51b18ff6c57519072d8fa4bd6e384760543bef979f1": {
                "Name": "hungry_swirles",
                "EndpointID": "42397562c0296c3763cdd704be2a761e4759b408b415a001714e839db327850e",
                "MacAddress": "00:15:5d:63:80:eb",
                "IPv4Address": "",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.windowsshim.hnsid": "21B4D02D-4FD6-40B9-A2A6-1E07CC138842",
            "com.docker.network.windowsshim.interface": "Ethernet"
        },
        "Labels": {}
    }
]

PS C:\Users\Administrator> docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED                  SIZE
srv.world/iis-server                   latest              d0b43de1e6a4        4 hours ago              5.32GB
mcr.microsoft.com/windows/servercore   1809                5c1f582f60a9        12 days ago              5.12GB

# run a container with [transparent01] network
PS C:\Users\Administrator> docker run -t -d --net transparent01 srv.world/iis-server
f655e188d62d5c595a83b51b18ff6c57519072d8fa4bd6e384760543bef979f1

PS C:\Users\Administrator> docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
f655e188d62d        srv.world/iis-server   "cmd"               27 seconds ago      Up 25 seconds       80/tcp              hungry_swirles

# display IP address (it is assigned by DHCP server in your local network)
PS C:\Users\Administrator> docker exec f655e188d62d powershell -c "ipconfig"

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet) 2:

   Connection-specific DNS Suffix  . : srv.world
   Link-local IPv6 Address . . . . . : fe80::fc34:3e2d:f775:f192%21
   IPv4 Address. . . . . . . . . . . : 10.0.0.204
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.0.0.1

# verify accesses
PS C:\Users\Administrator> curl.exe 10.0.0.204
Dockerfile test example
				
			
Logo Cloud Việt

Thông Tin Và Hoạt Động Của Chúng Tôi

Cloud Việt là đơn vị cung cấp dịch vụ TTDL uy tín tại Việt Nam. Với tiêu chí là đặt quyền lợi và trải nghiệm của khách hàng lên hàng đầu để  đem đến dịch vụ tốt nhất.

Trụ sở chính

110/20/41 đường số 30, phường 6, quận Gò Vấp, TPHCM

Ngày làm việc

Thứ 2 - Thứ 6, Hàng tuần

Giờ làm việc

08 : 00 AM - 18 : 00 PM

Email

Support@cloudviet.com.vn

Hotline

0972 710 812