即使容器在docker中端口映射,也从服务器获取空响应

问题描述 投票:1回答:2

我正在尝试通过npm start使用http服务器来启动我的应用程序。它在localhost中工作正常,但是当我通过docker容器(端口映射)部署相同的应用程序时,它没有出现在本地浏览器中。

我在docker容器(Ubuntu)中有一个目录,该目录通过以下命令运行docker run -it -p 8001:5500 ubuntu。我已经在容器中安装了所有内容-npm,vim编辑器等。

我的package.json文件是

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "http-server -a localhost -p 5500"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "concurrently": "^5.0.2",
    "http-server": "^0.12.1"
  }
}

之后,我完成了npm install,然后执行了命令npm start。该命令的输出为:

enter image description here

但是,当我尝试在Chrome浏览器中打开我的应用程序时,它给我的响应是空的。

enter image description here

此外,当我通过本地主机运行相同的应用程序时,也可以访问它。enter image description here

为什么我无法通过docker容器的端口映射访问服务器。?

docker httpserver
2个回答
0
投票

localhost容器内部将无法从容器外部访问。在容器内的0.0.0.0上运行服务器。


0
投票

您正在尝试将容器作为端口5500运行,其中Container_Port:Host_port条件应匹配。将其更改为5500:8001

© www.soinside.com 2019 - 2024. All rights reserved.