我不应该能够访问我的 docker 容器上的端口 4200 吗?
Shouldn't I be able to access port 4200 on my docker container?
我看过其他类似的问题,例如: and access docker container's port and 我想我正在尽我所能来访问我的 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
在我的服务器上,我这样启动 angular:
[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
更新二:感谢下面的 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.
该端口只能在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?
我看过其他类似的问题,例如:
这是 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
在我的服务器上,我这样启动 angular:
[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
更新二:感谢下面的 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.
该端口只能在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?