【问题标题】:nodejs app doesn't connect to localhost when running within a docker container在 docker 容器中运行时,nodejs 应用程序未连接到 localhost
【发布时间】:2018-06-25 10:24:39
【问题描述】:
My environment:
Ubunut 17.04 LTS
npm --version: 5.6.0
nodejs --version: 4.7.2
angular cli version: 1.6.4

docker-compose 文件:

version: '3'

services:
 my-app:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    volumes:
      - .:/usr/src/app
    ports:
      - "4200:4200"

我在 dockerfile 中注释掉了 EXPOSE 4200,因为我已经从 docker-compose.yml 文件中挂载了它,这不行吗,我应该在 dockerfile 中公开并在 docker-compose 中挂载吗?

在命令行上运行 npm start 会在浏览器上成功启动应用程序,因为我可以转到 localhost:4200 并查看应用程序正在运行。

但是,如果我使用 docker 构建我的应用程序并运行 docker-compose up,我看到 nodejs 服务器仍在 localhost:4200 上运行,但是,我无法访问该应用程序,转到 localhost:4200 不会出现页面。

手动运行应用程序效果很好,我可以在浏览器上看到它:

ubuntu17@ubuntu17:~/playground/apps/myapp-ui$ ng serve
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2018-01-16T16:22:51.912Z                                                          
Hash: 40ac2bd0588ee2136d15
Time: 13963ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 275 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 559 kB [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 514 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 12.1 MB [initial] [rendered]

webpack: Compiled successfully.

从 Docker-COMPOSE 运行应用程序运行良好,但我在 localhost:4200 的浏览器上看不到应用程序

从 docker-compose up

ubuntu17@ubuntu17:~/playground/apps/my-app-ui$ docker-compose up 
Creating network "my-app_default" with the default driver
Creating my-app_my-app_1 ... done
Attaching to my-app_my-app_1
my-app_1  | ** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
my-app_1  | Date: 2018-01-16T16:28:05.444Z
my-app_1  | Hash: 40ac2bd0588ee2136d15
my-app_1  | Time: 13584ms
my-app_1  | chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
my-app_1  | chunk {main} main.bundle.js (main) 271 kB [initial] [rendered]
my-app_1  | chunk {polyfills} polyfills.bundle.js (polyfills) 549 kB [initial] [rendered]
my-app_1  | chunk {styles} styles.bundle.js (styles) 511 kB [initial] [rendered]
my-app_1  | chunk {vendor} vendor.bundle.js (vendor) 12.1 MB [initial] [rendered]
my-app_1  | 
my-app_1  | webpack: Compiled successfully.

【问题讨论】:

  • 分享你的 docker-compose.yml

标签: docker docker-compose dockerfile docker-swarm docker-machine


【解决方案1】:

我最近遇到了这个问题。要使其在 Docker 容器中正常工作,您需要在节点应用服务器中绑定到 0.0.0.0 而不是 127.0.0.1。例如:

const http = require('http');

const hostname = '0.0.0.0'; // Do not use 127.0.0.1 here
const port = 3000;

const server = http.createServer((req, res) => {
  // do stuff
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

【讨论】:

    【解决方案2】:

    好的,正如您在问题中所说,您的进程正在侦听localhost:4200 上的连接。

    在 docker 容器内,localhost 是容器本身的环回设备的地址,它与您的主机网络是分开的,无法访问。

    您需要编辑您的节点进程,以便通过编辑 Dockerfile 中的入口点使其监听所有地址,如下所示:

    ENTRYPOINT ["ng", "serve", "-H", "0.0.0.0"]
    

    【讨论】:

    • 谢谢先生,如果 0.0.0.0 指向 ip 地址,即我应该 ping localhost 和从中获得的 ip,我将它添加到入口点,如下所示 -> ENTRYPOINT ["ng", “服务”、“-H”、“ipObtainedFromPing”]
    • 不,0.0.0.0 是一个特殊地址,它绑定到 docker 容器中的所有接口,因此您需要从字面上使用“0.0.0.0”
    • 好的,我现在试试;请稍候,我会立即尝试运行并更新您
    • 哇!!哇,谢谢先生,我昨晚和今天都花在了这个上;非常感谢,今天学到了一些东西!!!
    • 这是我的荣幸
    【解决方案3】:

    在您的 docker-compose 文件中,确保导出端口:

    services:
      node:
       ports:
         - 4200:4200
    

    【讨论】:

    • 感谢您的帮助,我更新了上面的问题以包括我的 docker-compose.yml 和 dockerfile
    • 非常感谢@yamenk 帮助我
    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2014-12-30
    相关资源
    最近更新 更多