应用程序不会连接到本地主机

应用程序不会连接到本地主机

本文介绍了在 docker 容器中运行时,nodejs 应用程序不会连接到本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My environment:
Ubunut 17.04 LTS
npm --version: 5.6.0
nodejs --version: 4.7.2
angular cli version: 1.6.4

docker-compose 文件:

docker-compose file:

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 中挂载吗?

I commented out the EXPOSE 4200 in dockerfile because I'm already mounting it from docker-compose.yml file, is that NOT ok, should I expose in dockerfile and mount in docker-compose?

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

Running npm start on command line launches the app successfully on the browser as I'm able to go to localhost:4200 and see the app running.

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

However, if I build my app with docker and run docker-compose up, I see that nodejs server is still running on localhost:4200, however, I CANNOT access the app, going to localhost:4200 doesn't bring up the page.

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

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 UP 运行应用程序运行良好,但我无法在 localhost:4200 的浏览器上看到应用程序

从 docker-compose up

And from 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.

推荐答案

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

Ok, as you say in your question, your process is listening to connection on localhost:4200.

在docker容器内,localhost是容器本身的loopback设备的地址,它与你的主机网络分离且无法访问.

Inside a docker container, localhost is the address of the loopback device of the container itself, which is separated and not reachable from your host network.

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

You need to edit your node process in order to make it listen to all addresses by editing the entrypoint in your Dockerfile as follows:

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

这篇关于在 docker 容器中运行时,nodejs 应用程序不会连接到本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 08:25