无法从 docker 容器中访问 MacOSX 主机上的端口
Cannot access ports on MacOSX host from within docker container
这些是我采取的步骤:
准备工作:
第一个:
docker-machine create -d virtualbox default
然后,我使用以下 Dockerfile 创建了一个容器:
FROM centos:latest
没有别的 — 只是 CentOS 的副本。我构建了容器:
docker build -t mycontainer .
和运行它:
docker run -it --net="host" --name="test" -p 9200:9200 mycontainer
问题: 当我进入容器并尝试访问 MacOSX 上的服务 运行ning(例如简单的网络服务器或本地弹性搜索)时,我得到:
curl localhost:9200
curl: (7) Failed connect to localhost:9200; Connection refused
我在我的 docker 虚拟机 (docker-machine ssh default
) 中得到同样的错误。
我尝试在 virtualbox 中进行端口转发,将 9200 设置为 9200 — 但它没有帮助。
有什么想法吗?
您无法使用 localhost:port 从 docker 容器连接到主机上的端口(除非您 运行 使用 --net="host"
的容器)
您需要指定要连接的主机的 IP 地址。
请检查主机上的 IP:
dude-server:Whosebug cwoehrle$ ping $(hostname)
PING dude-server (192.168.1.169): 56 data bytes
64 bytes from 192.168.1.169: icmp_seq=0 ttl=64 time=0.053 ms
64 bytes from 192.168.1.169: icmp_seq=1 ttl=64 time=0.069 ms
容器中(分别使用你的ip):
root@8975fada1ac3:/# nc 192.168.1.169 9200
已编辑:
要连接到 mac 上的主机端口,您可以使用默认网关地址 10.0.2.2,例如nc 10.0.2.2 9200
在容器中,只需引用:
host.docker.internal
因此,您的 curl 命令将是:
curl http://host.docker.internal:9200
这些是我采取的步骤:
准备工作:
第一个:
docker-machine create -d virtualbox default
然后,我使用以下 Dockerfile 创建了一个容器:
FROM centos:latest
没有别的 — 只是 CentOS 的副本。我构建了容器:
docker build -t mycontainer .
和运行它:
docker run -it --net="host" --name="test" -p 9200:9200 mycontainer
问题: 当我进入容器并尝试访问 MacOSX 上的服务 运行ning(例如简单的网络服务器或本地弹性搜索)时,我得到:
curl localhost:9200
curl: (7) Failed connect to localhost:9200; Connection refused
我在我的 docker 虚拟机 (docker-machine ssh default
) 中得到同样的错误。
我尝试在 virtualbox 中进行端口转发,将 9200 设置为 9200 — 但它没有帮助。
有什么想法吗?
您无法使用 localhost:port 从 docker 容器连接到主机上的端口(除非您 运行 使用 --net="host"
的容器)
您需要指定要连接的主机的 IP 地址。
请检查主机上的 IP:
dude-server:Whosebug cwoehrle$ ping $(hostname)
PING dude-server (192.168.1.169): 56 data bytes
64 bytes from 192.168.1.169: icmp_seq=0 ttl=64 time=0.053 ms
64 bytes from 192.168.1.169: icmp_seq=1 ttl=64 time=0.069 ms
容器中(分别使用你的ip):
root@8975fada1ac3:/# nc 192.168.1.169 9200
已编辑:
要连接到 mac 上的主机端口,您可以使用默认网关地址 10.0.2.2,例如nc 10.0.2.2 9200
在容器中,只需引用:
host.docker.internal
因此,您的 curl 命令将是:
curl http://host.docker.internal:9200