在docker容器中运行时

在docker容器中运行时

本文介绍了在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.

手动运行该应用程序非常有效,我可以在浏览器上看到它:

RUNNING THE APP MANUALLY WORKS GREAT, I CAN SEE IT ON BROWSER:

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起来

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 是容器本身的回送设备的地址,该地址是从主机网络中分离且无法访问的。

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:21