东莞塘厦做网站,wordpress多功能图片主题,制造网,腾讯云域名购买流程主要解决从容器内访问外网的服务。 网上文章的很大内容雷同#xff0c;除了用–nethost这种硬来的可行外#xff0c;其它的方法都没看的很懂#xff0c;按自己的思路整理。
用–add-hosthost.docker.internal:host-gateway参数解决
docker run -d --add-hosthost.docker.i…主要解决从容器内访问外网的服务。 网上文章的很大内容雷同除了用–nethost这种硬来的可行外其它的方法都没看的很懂按自己的思路整理。
用–add-hosthost.docker.internal:host-gateway参数解决
docker run -d --add-hosthost.docker.internal:host-gateway xyzxyz 是容器名 –add-host 参数意思是向xyz的/etc/hosts文件中增加一条解析
如果不想了解更多只要解决问题看到这里就可以了。 点击这里查看官方解释 The --add-host flag supports a special host-gateway value that resolves to the internal IP address of the host. This is useful when you want containers to connect to services running on the host machine. It’s conventional to use host.docker.internal as the hostname referring to host-gateway. Docker Desktop automatically resolves this hostname, see Explore networking features. The following example shows how the special host-gateway value works. The example runs an HTTP server that serves a file from host to container over the host.docker.internal hostname, which resolves to the host’s internal IP. 意思是说host.docker.internal和host-gateway是docker约定的写法。host.docker.internal代表容器的iphost-gateway代表容器的网关和宿主机ip是绑定的ip地址不一样。因为网关和容器的ip地址不固定所以在容器内需要对应ip地址的地方用这两个写法替代可以减少耦合。
在看下容器内的/etc/hosts文件
sudo docker exec d1fe131fca0e cat /etc/hosts127.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
172.17.0.1 host.docker.internal #增加了这行解析其它都相对固定的原生解析
172.17.0.2 e341ad84d2cc可以看到增加了一条解析。172.17.0.1就是容器的gateway。
野生写法
docker run -d --add-hostwaiwang.com:host-gateway xyz打印容器的/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
172.17.0.1 waiwang.com #增加了这行解析
172.17.0.2 e341ad84d2cc还有更野生的写法 比如宿主机的ip是192.168.100.100。
docker run -d --add-hostwaiwang.com:192.168.100.100 xyz或者容器内都直接写宿主机的ip。 虽然也可以但宿主机和容器的ip不固定可能每次运行都要核对ip可见就太麻烦了