我有个码头集装箱在运行詹金斯。作为构建过程的一部分,我需要访问在主机上本地运行的web服务器。是否有一种方法可以将主机web服务器(可以配置为在端口上运行)暴露给jenkins容器?
我在Linux机器上本机运行docker。
更新:
除了下面的@larsks答案,要从主机上获取主机IP的IP地址,我做了以下操作:
ip addr show docker0 | grep -Po 'inet \K[\d.]+'
我有个码头集装箱在运行詹金斯。作为构建过程的一部分,我需要访问在主机上本地运行的web服务器。是否有一种方法可以将主机web服务器(可以配置为在端口上运行)暴露给jenkins容器?
我在Linux机器上本机运行docker。
更新:
除了下面的@larsks答案,要从主机上获取主机IP的IP地址,我做了以下操作:
ip addr show docker0 | grep -Po 'inet \K[\d.]+'
当前回答
目前在Mac和Windows上最简单的方法是使用主机host.docker.internal,它解析为主机的IP地址。不幸的是,它还不能在linux上运行(截至2018年4月)。
其他回答
答案是……
将http://127.0.0.1或http://localhost替换为http://host.docker.internal。
Why?
Docker文档中的源代码。
我的谷歌搜索把我带到了这里,在挖掘评论后,我发现它是从Docker容器内部复制的,我如何连接到机器的本地主机?我投票认为这是重复的,但由于人们(包括我自己!)经常向下滚动答案,而不是仔细阅读评论,这里是一个简短的答案。
当你“已经”创建了两个docker映像,并且你想让两个容器彼此通信时。
为此,您可以方便地使用自己的——name运行每个容器,并使用——link标志来启用它们之间的通信。但是在docker构建期间你不会得到这个。
当你和我一样身处险境时,那是你的
docker build -t "centos7/someApp" someApp/
当你尝试的时候,它就会被打破
curl http://172.17.0.1:localPort/fileIWouldLikeToDownload.tar.gz > dump.tar.gz
你会被“curl/wget”卡住,返回没有“到主机的路由”。
原因是docker设置的安全性,默认情况下禁止容器与主机或主机上运行的其他容器通信。 这让我很惊讶,我必须说,你会期望在本地机器上运行的docker机器的回声系统能够完美地相互访问,没有太多障碍。
对此的解释将在以下文档中详细描述。
http://www.dedoimedo.com/computers/docker-networking.html
本文给出了两个快速解决方案,可以帮助您降低网络安全性。
最简单的选择是关闭防火墙——或者允许所有防火墙。这意味着运行必要的命令,可以是systemctl stop firewall, iptables -F或等效命令。
对我来说(Windows 10, Docker Engine v19.03.8),它是https://stackoverflow.com/a/43541732/7924573和https://stackoverflow.com/a/50866007/7924573的组合。
change the host/ip to host.docker.internal e.g.: LOGGER_URL = "http://host.docker.internal:8085/log" set the network_mode to bridge (if you want to maintain the port forwarding; if not use host): version: '3.7' services: server: build: . ports: - "5000:5000" network_mode: bridge or alternatively: Use --net="bridge" if you are not using docker-compose (similar to https://stackoverflow.com/a/48806927/7924573) As pointed out in previous answers: This should only be used in a local development environment. For more information read: https://docs.docker.com/compose/compose-file/#network_mode and https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds
在docker run命令中使用——net="host",那么docker容器中的localhost将指向docker主机。
对我来说最简单的选择是, 我使用我的机器在本地网络上的IP地址(由路由器分配)
您可以使用ifconfig命令找到这一点
e.g
ifconfig
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=400<CHANNEL_IO>
ether f0:18:98:08:74:d4
inet 192.168.178.63 netmask 0xffffff00 broadcast 192.168.178.255
media: autoselect
status: active
然后使用inet地址。这可以让我连接机器上的任何端口。