![](/img/trans.png)
[英]Docker on Windows: how to connect to container from host using container IP?
[英]Cannot connect to guest docker container from host on port 1300 with netcat
我试图遵循这个问题: 如何连接到在 docker 容器中运行的 netcat?
我有
C:\Users\Chloe\workspace\spinsci>docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0df9daa8d8cc b7bd807e363a "/bin/sh -c /bin/ash" 9 seconds ago Up 6 seconds 0.0.0.0:1300->1300/tcp optimistic_newton
C:\Users\Chloe\workspace\spinsci>docker exec -it 0df9daa8d8cc /bin/ash
/ # nc -l -p 1300
在我的主机上,我尝试连接但它失败了:
$ nc -vv localhost 1300
nc: connect to localhost port 1300 (tcp) failed: Connection refused
nc: connect to localhost port 1300 (tcp) failed: Connection refused
$ nc 127.0.0.1 1300
$ nc 0.0.0.0 1300
$ telnet localhost 1300
Trying ::1...
Connection failed: Connection refused
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
我的Dockerfile
仅仅是
FROM alpine:latest
EXPOSE 1300
CMD /bin/ash
运行docker inspect 0df9daa8d8cc
产量
...
"HostConfig": {
"PortBindings": {
"1300/tcp": [
{
"HostIp": "",
"HostPort": "1300"
}
]
},
...
"NetworkSettings": {
"Ports": {
"1300/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "1300"
}
]
},
...
"Networks": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
使用 Docker Toolbox (Engine 19.03.5) 因为 Windows 8.1。
Docker Toolbox 在运行在 virtualbox 中的 linux 虚拟机中运行 docker。 VM 有自己的虚拟 NIC,具有不同的 IP 地址。 它应该显示在控制面板(或使用 ipconfig)的 Windows 适配器列表中,默认情况下 IP 位于 192.168.99.x 子网中。 因此,主机上的正确命令是nc 192.168.99.x 1300
,其中 x 是运行设置中的实际值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.