为什么我无法从Docker容器访问主机端口?

时间:2017-02-13 00:41:36

标签: networking docker

我读过this is just ubuntu + curl,它提出了同样的问题,但那里的解决方案似乎没有用。基本上我正试图从docker内部访问主机操作系统上的端口,并且我正在使用链接帖子中建议的--net="host"标志。但是,我仍然无法访问该端口。对我有用的唯一方法是在0.0.0.0上运行我的主机Web服务,然后从192.168.###.###访问它,但该地址根据我所使用的Wifi进行更改,所以我不想要要做到这一点。这是我尝试过的:

设置一个测试网络服务器,我可以尝试从容器内部访问:

bash-3.2$ echo hi > index.html
bash-3.2$ python -m SimpleHTTPServer 1234 >/dev/null 2>&1 &
[1] 57942

从主机中卷曲以确保它正在运行:

bash-3.2$ curl localhost:1234
hi

启动已安装curl的容器(https://nodejs.org/en/download):

bash-3.2$ docker run --rm -it --net="host" tutum/curl bash

尝试从容器内部卷曲:

root@moby:/# curl localhost:1234
curl: (7) Failed to connect to localhost port 1234: Connection refused

我在 macOS ,所以我认为它可能与容器的主机 boot2docker 而不是我的mac有关,但我仍然没有知道如何减轻这种情况。

任何建议都将不胜感激! :)

0 个答案:

没有答案