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

Getting the empty response from the server even when the container is port mapped in docker

我正在尝试通过 npm start 使用 http-server 启动我的应用程序。它在本地主机上运行良好,但是当我通过 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。 命令的输出是:

但是当我尝试在 chrome 浏览器中打开我的应用程序时,它向我抛出一个空响应。

另外,当我 运行 通过我的本地主机访问同一个应用程序时,它是可以访问的。

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

localhost 容器内部无法从容器外部访问。 运行 您的服务器位于 0.0.0.0 容器内。