无法访问在Docker中运行的服务

我无法通过运行在Docker镜像中的http://localhost:8000访问基于nodejs的服务。 我使用Docker for Mac( https://docs.docker.com/docker-for-mac/ )

我在这里https://nodejs.org/en/docs/guides/nodejs-docker-webapp/下面的教程。

服务器在端口8000上运行。我使用以下命令启动docker映像:

 $ docker run -p 8000:8000 -d geuis/node-server:latest 

如果我运行docker ps我看到:

 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9fa2e446918b geuis/node-server:latest "npm start" 6 seconds ago Up 5 seconds 0.0.0.0:8000->8000/tcp unruffled_lewin 

如果我docker exec -it 9fa2e446918b /bin/bash我可以访问docker exec -it 9fa2e446918b /bin/bash ,我可以curl http://localhost:8000并从容器内访问服务器。

但是,我从我的系统terminal尝试相同的curl http://localhost:8000 ,并且无法访问它。

不知道接下来我需要做什么。

尝试下面的listen语句:

 app.listen(PORT, '0.0.0.0'); 

从阅读教程你提到它看起来像快递正在localhost上侦听。 这很好,如果你在本地运行,但是在一个容器内, localhost不是容器外部的同一个localhost

0.0.0.0是未指定的IPv4地址 ,所以Express将绑定在它可以find的任何IP上,这将是您的请求从容器外部进入的IP。