Windows 2022
Sponsored Link

Docker : Docker Network2022/01/25

 
This is the basic usage to configure Docker Network.
[1] When running containers without specifying network, default [nat] network is assigned.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

# display network list
PS C:\Users\Administrator> docker network ls 
NETWORK ID     NAME      DRIVER    SCOPE
3681aae29377   nat       nat       local
3edcf1153d19   none      null      local

# display details of [nat]
PS C:\Users\Administrator> docker network inspect nat 
[
    {
        "Name": "nat",
        "Id": "3681aae293778d14a42b0c97aa6650851a721b46abb9aa67e7796d3968cd8c53",
        "Created": "2022-01-24T18:16:36.5492238-08:00",
        "Scope": "local",
        "Driver": "nat",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "windows",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.24.160.0/20",
                    "Gateway": "172.24.160.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.windowsshim.hnsid": "0729EC4F-404D-48F2-B325-A2375FD245A3",
            "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:ltsc2022 powershell -c "ipconfig" 

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::753e:221a:27b3:7444%14
   IPv4 Address. . . . . . . . . . . : 172.24.167.99
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.24.160.1
[2] If you'd like to assign another network, set like follows.
# 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 
be888587579795f50f1177dff57ec5d18dac72a83423ae53649e7cb5e05de2ca

PS C:\Users\Administrator> docker network ls 
NETWORK ID     NAME        DRIVER    SCOPE
3681aae29377   nat         nat       local
be8885875797   network01   nat       local
3edcf1153d19   none        null      local

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

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::b8a3:6b0b:1cf2:cf48%18
   IPv4 Address. . . . . . . . . . . : 192.168.100.20
   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
0065495abb3d   srv.world/iis   "cmd"     4 seconds ago   Up 2 seconds   0.0.0.0:8081->80/tcp   vigorous_tereshkova

PS C:\Users\Administrator> docker exec 0065495abb3d powershell -c "ipconfig" 

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::5406:409f:298:790c%18
   IPv4 Address. . . . . . . . . . . : 172.24.166.130
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.24.160.1

# attach network to specify an IP address in the subnet
PS C:\Users\Administrator> docker network connect --ip 192.168.100.10 network01 0065495abb3d 

PS C:\Users\Administrator> docker exec 0065495abb3d powershell -c "ipconfig" 

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::5406:409f:298:790c%18
   IPv4 Address. . . . . . . . . . . : 172.24.166.130
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.24.160.1

Ethernet adapter vEthernet (Ethernet) 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::998a:d856:7a1c:d1e1%22
   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 0065495abb3d 
PS C:\Users\Administrator> docker exec 0065495abb3d powershell -c "ipconfig" 

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::5406:409f:298:790c%18
   IPv4 Address. . . . . . . . . . . : 172.24.166.130
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.24.160.1
[3] To remove docker networks, run like follows.
PS C:\Users\Administrator> docker network ls 
NETWORK ID     NAME        DRIVER    SCOPE
3681aae29377   nat         nat       local
be8885875797   network01   nat       local
3edcf1153d19   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
[4] To connect to Host network, not nat, set like follows.
# confirm network adapter name
PS C:\Users\Administrator> Get-NetAdapter 

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
vEthernet (Ethernet)      Hyper-V Virtual Ethernet Container A...      18 Up           00-15-5D-CF-F9-0F        10 Gbps
vEthernet (nat)           Hyper-V Virtual Ethernet Adapter              7 Up           00-15-5D-CF-FA-B0        10 Gbps
Ethernet0                 Intel(R) 82574L Gigabit Network Conn...       9 Up           00-0C-29-E7-79-55         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="Ethernet0" transparent01 

PS C:\Users\Administrator> docker network ls 
NETWORK ID     NAME            DRIVER        SCOPE
3681aae29377   nat             nat           local
3edcf1153d19   none            null          local
a028ce2611e4   transparent01   transparent   local

PS C:\Users\Administrator> docker network inspect transparent01 
[
    {
        "Name": "transparent01",
        "Id": "a028ce2611e46a3ad42dadce25f72f3448a888aacb0aa50d453ea98f8a99c354",
        "Created": "2022-01-24T19:54:07.0028749-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": {},
        "Options": {
            "com.docker.network.windowsshim.hnsid": "E9277179-2B8D-490E-80B8-B48A3B0051A6",
            "com.docker.network.windowsshim.interface": "Ethernet0"
        },
        "Labels": {}
    }
]

PS C:\Users\Administrator> docker images 
REPOSITORY                                 TAG        IMAGE ID       CREATED      SIZE
srv.world/iis-server                       latest     0eaa2396d003   4 days ago   5.07GB
mcr.microsoft.com/windows/servercore/iis   latest     09adff3e2845   5 days ago   5.07GB
mcr.microsoft.com/windows/servercore       ltsc2022   11cbc9e36c7a   8 days ago   4.95GB

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

PS C:\Users\Administrator> docker ps 
CONTAINER ID   IMAGE                  COMMAND   CREATED          STATUS          PORTS     NAMES
3d3783c315d1   srv.world/iis-server   "cmd"     52 seconds ago   Up 49 seconds   80/tcp    focused_cartwright

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

Windows IP Configuration

Ethernet adapter vEthernet (Ethernet) 2:

   Connection-specific DNS Suffix  . : srv.world
   Link-local IPv6 Address . . . . . : fe80::9eb:30fe:683f:8e11%26
   IPv4 Address. . . . . . . . . . . : 10.0.0.244
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.0.0.1

# verify accesses
PS C:\Users\Administrator> curl.exe 10.0.0.244 
Dockerfile test example
Matched Content