访问宿主机网络

host mode

使用host模式:

docker run -d --network=host my-container:latest
services:
  my-service:
    network_mode: host

添加hosts

使用--add-host选项添加映射到/etc/hosts文件,添加host.docker.internal到hosts

docker run --rm -it --add-host host.docker.internal:host-gateway goexpect bash

hosts in container

root@00e0febe04e2:/app# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.65.2    host.docker.internal
172.17.0.2      00e0febe04e2

访问宿主机网络

ssh root@host.docker.internal -p 2222