我有个码头集装箱在运行詹金斯。作为构建过程的一部分,我需要访问在主机上本地运行的web服务器。是否有一种方法可以将主机web服务器(可以配置为在端口上运行)暴露给jenkins容器?

我在Linux机器上本机运行docker。

更新:

除了下面的@larsks答案,要从主机上获取主机IP的IP地址,我做了以下操作:

ip addr show docker0 | grep -Po 'inet \K[\d.]+'

当前回答

适用于所有平台

Docker v 20.10及以上版本(自2020年12月14日起)

在Linux上,在Docker命令中添加——add-host=host.docker.internal:host-gateway来启用该功能。(Docker Compose配置见下文)

使用您的内部IP地址或连接到特殊的DNS名称host.docker.internal,它将解析为主机使用的内部IP地址。

要在Linux上的Docker Compose中启用此功能,请在容器定义中添加以下行: extra_hosts: ——“host.docker.internal: host-gateway”

适用于macOS和Windows

Docker v 18.03及以上版本(自2018年3月21日起)

使用您的内部IP地址或连接到特殊的DNS名称host.docker.internal,它将解析为主机使用的内部IP地址。

Linux支持未决https://github.com/docker/for-linux/issues/264

MacOS和早期版本的Docker

Mac v 17.12到v 18.02的Docker

与上述相同,但使用docker.for.mac.host.internal代替。

Mac v 17.06到v 17.11的Docker

与上述相同,但使用docker.for.mac.localhost代替。

Docker适用于Mac 17.05及以下版本

要从docker容器访问主机,必须为网络接口附加一个IP别名。你可以绑定任何你想要的IP,只要确保你没有使用它到其他任何东西。

sudo ifconfig lo0又名123,123,123 /24

然后确保您的服务器正在侦听上面提到的IP或0.0.0.0。如果它正在监听localhost 127.0.0.1,它将不接受连接。

然后只需将docker容器指向这个IP,就可以访问主机了!

为了测试,可以在容器内运行curl -X GET 123.123.123.123:3000。

别名将在每次重新启动时重置,因此如果需要,创建一个启动脚本。

解决方案和更多文档请访问:https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

其他回答

为此我创建了一个docker容器https://github.com/qoomon/docker-host

然后,您可以简单地使用容器名称dns访问主机系统。 curl http://dockerhost: 9200

答案是……

将http://127.0.0.1或http://localhost替换为http://host.docker.internal。

Why?

Docker文档中的源代码。

我的谷歌搜索把我带到了这里,在挖掘评论后,我发现它是从Docker容器内部复制的,我如何连接到机器的本地主机?我投票认为这是重复的,但由于人们(包括我自己!)经常向下滚动答案,而不是仔细阅读评论,这里是一个简短的答案。

适用于所有平台

Docker v 20.10及以上版本(自2020年12月14日起)

在Linux上,在Docker命令中添加——add-host=host.docker.internal:host-gateway来启用该功能。(Docker Compose配置见下文)

使用您的内部IP地址或连接到特殊的DNS名称host.docker.internal,它将解析为主机使用的内部IP地址。

要在Linux上的Docker Compose中启用此功能,请在容器定义中添加以下行: extra_hosts: ——“host.docker.internal: host-gateway”

适用于macOS和Windows

Docker v 18.03及以上版本(自2018年3月21日起)

使用您的内部IP地址或连接到特殊的DNS名称host.docker.internal,它将解析为主机使用的内部IP地址。

Linux支持未决https://github.com/docker/for-linux/issues/264

MacOS和早期版本的Docker

Mac v 17.12到v 18.02的Docker

与上述相同,但使用docker.for.mac.host.internal代替。

Mac v 17.06到v 17.11的Docker

与上述相同,但使用docker.for.mac.localhost代替。

Docker适用于Mac 17.05及以下版本

要从docker容器访问主机,必须为网络接口附加一个IP别名。你可以绑定任何你想要的IP,只要确保你没有使用它到其他任何东西。

sudo ifconfig lo0又名123,123,123 /24

然后确保您的服务器正在侦听上面提到的IP或0.0.0.0。如果它正在监听localhost 127.0.0.1,它将不接受连接。

然后只需将docker容器指向这个IP,就可以访问主机了!

为了测试,可以在容器内运行curl -X GET 123.123.123.123:3000。

别名将在每次重新启动时重置,因此如果需要,创建一个启动脚本。

解决方案和更多文档请访问:https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

对我来说最简单的选择是, 我使用我的机器在本地网络上的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地址。这可以让我连接机器上的任何端口。

对我来说(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