How to build connection from the host machine to containers using host network?

Hi, mates. I am struggling to build a connection from a host machine(windows system) to a container(ubuntu). Running a command as following:

docker run --hostname=docker-desktop --env=DISPLAY=host.docker.internal:0.0 --env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin --volume=D:\humble_ws:/home/wc/humble_ws --network=host --restart=no --label='org.opencontainers.image.ref.name=ubuntu' --label='org.opencontainers.image.version=22.04' --runtime=runc -t -d 02d58a115dd9b508f1554bda8d7a33a59c9adcc559d810cc6d7692d51e0680c6

and then run into the container, I input “ifconfig” and got the following output:

# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:a1:70:c1:b5  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.65.3  netmask 255.255.255.0  broadcast 192.168.65.255
        inet6 fe80::c44e:eeff:fe8b:31c  prefixlen 64  scopeid 0x20<link>
        inet6 fdc4:f303:9324::3  prefixlen 64  scopeid 0x0<global>
        ether c6:4e:ee:8b:03:1c  txqueuelen 1000  (Ethernet)
        RX packets 5873  bytes 467277 (467.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6327  bytes 2579176 (2.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 224  bytes 18840 (18.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 224  bytes 18840 (18.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

services1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.65.6  netmask 255.255.255.255  broadcast 0.0.0.0
        inet6 fdc4:f303:9324::6  prefixlen 128  scopeid 0x0<global>
        inet6 fe80::b8fd:c0ff:fecb:f693  prefixlen 64  scopeid 0x20<link>
        ether ba:fd:c0:cb:f6:93  txqueuelen 0  (Ethernet)
        RX packets 5166  bytes 2501839 (2.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5880  bytes 452867 (452.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

the host machine network is :

C:\Windows\system32>ipconfig

Windows IP Configuration


Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::9603:b327:7aef:81e9%48
   IPv4 Address. . . . . . . . . . . : 172.18.96.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

Ethernet adapter 以太网:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::3260:472b:9360:1c82%9
   IPv4 Address. . . . . . . . . . . : 192.168.31.209
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.31.1

and the setting of docker desktop like that:

windows(host machine) route list like that:

C:\Windows\system32>route print
===========================================================================
Interface List
 48...00 15 5d 32 b8 8e ......Hyper-V Virtual Ethernet Adapter
  9...e0 d5 5e d4 74 b5 ......Intel(R) Ethernet Connection (7) I219-V
  1...........................Software Loopback Interface 1
===========================================================================

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0     192.168.31.1   192.168.31.209     25
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
       172.17.0.0    255.255.255.0     192.168.31.1   192.168.31.209     26
      172.18.96.0    255.255.240.0         On-link       172.18.96.1    271
      172.18.96.1  255.255.255.255         On-link       172.18.96.1    271
   172.18.111.255  255.255.255.255         On-link       172.18.96.1    271
     192.168.31.0    255.255.255.0         On-link    192.168.31.209    281
     192.168.31.0    255.255.255.0     192.168.31.1   192.168.31.209     26
   192.168.31.209  255.255.255.255         On-link    192.168.31.209    281
   192.168.31.255  255.255.255.255         On-link    192.168.31.209    281
     192.168.65.0    255.255.255.0     192.168.31.1   192.168.31.209     26
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link    192.168.31.209    281
        224.0.0.0        240.0.0.0         On-link       172.18.96.1    271
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  255.255.255.255  255.255.255.255         On-link    192.168.31.209    281
  255.255.255.255  255.255.255.255         On-link       172.18.96.1    271
===========================================================================
Persistent Routes:
  None

IPv6 Route Table
===========================================================================
Active Routes:
 If Metric Network Destination      Gateway
  1    331 ::1/128                  On-link
  9    281 fe80::/64                On-link
 48    271 fe80::/64                On-link
  9    281 fe80::3260:472b:9360:1c82/128
                                    On-link
 48    271 fe80::9603:b327:7aef:81e9/128
                                    On-link
  1    331 ff00::/8                 On-link
  9    281 ff00::/8                 On-link
 48    271 ff00::/8                 On-link
===========================================================================
Persistent Routes:
  None

OK. Aboves are all the background. Now I am facing the following three problems, highly appreciate anyone could share any ideas to fix them out.

  1. when I try to ping the one of container such as IP:192.168.65.3 from the host machine, the
    outputs are:
C:\Windows\system32>ping 192.168.65.3

Pinging 192.168.65.3 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.168.65.3:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

meaning " Request Timeout"。 So, how do I ping the container successfully?

  1. I notice that there is a network name “services1” in the output of container network. what is that?

  2. the docker desktop setting for docker subnetwork is 192.168.65.0/24. should I change to 192.168.31.0/24?

Please, read the Known limitations part in the documentation. The host network in Docker Desktop cannot be a real host network as you can’t share the network between the physical host and the virtual machine in which the docker daemon is running. So you will not see the axctual host network. It just help with proxies to forward ports from the host to containers and from the containers to the host in some cases.

https://docs.docker.com/desktop/networking/#known-limitations

That is probably the ip of the virtual machine but that special network is not available from the host. At least it wasn’t when I last tried. So whenever you list interfaces in the container, it is the interfaces of the Linux VM. When you list interfaces on the host, you see the actual host interfaces.