我看过其他类似的问题,例如:Access to Docker Container Port和access docker container's port和Cannot access port on host mapped to docker container port,我认为我正在尽我所能访问Docker容器上的端口4200我无法。
这是docker ps
的输出:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
298f832ca50d redcricket/fisherman:latest "/usr/sbin/init" About an hour ago Up 39 minutes 4200/tcp gallant_matsumoto
我的容器可ping通:
$ ping `docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' gallant_matsumoto`
Pinging 172.17.0.2 with 32 bytes of data:
Reply from 172.17.0.2: bytes=32 time=110ms TTL=239
Reply from 172.17.0.2: bytes=32 time=82ms TTL=239
Reply from 172.17.0.2: bytes=32 time=92ms TTL=239
Reply from 172.17.0.2: bytes=32 time=80ms TTL=239
Ping statistics for 172.17.0.2:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 80ms, Maximum = 110ms, Average = 91ms
在我的服务器上,我开始像这样:
[root@298f832ca50d HelloWorld]# ng serve --host 0.0.0.0 --port 4200 --disableHostCheck
WARNING: This is a simple server for use in testing or debugging Angular applications
locally. It hasn't been reviewed for security issues.
Binding this server to an open connection can result in compromising your application or
computer. Using a different host than the one passed to the "--host" flag might result in
websocket connection issues. You might need to use "--disableHostCheck" if that's the
case.
WARNING: Running a server with --disable-host-check is a security risk. See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a for more information.
10% building 3/3 modules 0 activeℹ 「wds」: Project is running at http://0.0.0.0:4200/webpack-dev-server/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: 404s will fallback to //index.html
chunk {main} main.js, main.js.map (main) 9.77 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 251 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.09 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 16.3 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 3.8 MB [initial] [rendered]
Date: 2019-07-06T19:58:08.308Z - Hash: 201a042264b357c18e36 - Time: 14593ms
** Angular Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **
ℹ 「wdm」: Compiled successfully.
但是我仍然无法访问http://172.17.0.2:4200/
C:\Users\plankton>curl http://172.17.0.2:4200/
curl: (7) Failed to connect to 172.17.0.2 port 4200: Timed out
我想念什么?
更新:谢谢seahorsepip我尝试了此操作:
$ docker run --privileged -p=4200:4200 -itd -e container=docker -v /sys/fs/cgroup:/sys/fs/cgroup redcricket/fisherman:latest /usr/sbin/init
870cd55d0bc65ffe27c595b0092f28a5e333c235ae355a2469563b09058cdf22
plankton@DESKTOP-C8MFTFD MINGW64 /c/Program Files/Docker Toolbox
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
870cd55d0bc6 redcricket/fisherman:latest "/usr/sbin/init" 8 seconds ago Up 3 seconds 0.0.0.0:4200->4200/tcp kind_kapitsa
但是我仍然无法连接...
C:\Users\plankton>curl http://localhost:4200/
curl: (7) Failed to connect to localhost port 4200: Connection refused
更新II:多亏了David,我现在知道了虚拟机的IP
$ docker-machine.exe ip
192.168.99.100
我不确定如何使用此IP。
在客户端上,我得到...
C:\Users\plankton>curl http://192.168.99.100:4200
curl: (7) Failed to connect to 192.168.99.100 port 4200: Connection refused
如果我尝试像这样重新启动服务器...
[root@bfd864884a59 HelloWorld]# ng serve --host 192.168.99.100
An unhandled exception occurred: listen EADDRNOTAVAIL: address not available 192.168.99.100:4200
See "/tmp/ng-YKKmQ9/angular-errors.log" for further details.
答案 0 :(得分:0)
该端口仅可在Docker网络本身中访问,您需要将端口转发到Docker网络外部:
docker run -p 4200:4200 ...
然后,您可以在localhost:4200
到达docker实例。
有关Docker网络的更多信息:https://docs.docker.com/config/containers/container-networking/
有关发布和公开(差异)的更多信息: What is the difference between "expose" and "publish" in Docker?