nodejs app doesn't connect to localhost when r

2020-02-06 17:56发布

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

docker-compose file:

version: '3'

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

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?

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.

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.

RUNNING THE APP FROM DOCKER-COMPOSE UP RUNS FINE BUT I CANNOT SEE THE APP ON BROWSER with localhost:4200

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.

2条回答
放我归山
2楼-- · 2020-02-06 18:01

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

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.

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"]
查看更多
虎瘦雄心在
3楼-- · 2020-02-06 18:04

In your docker-compose file, make sure to export the ports:

services:
  node:
   ports:
     - 4200:4200
查看更多
登录 后发表回答