2
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.
pelican
  • 5,846
  • 9
  • 43
  • 67

3 Answers3

9

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"]
whites11
  • 12,008
  • 3
  • 36
  • 53
  • Thank you sir, should the 0.0.0.0 be pointing to ip address i.e should I ping localhost and the ip I get from that, I add it to entrypoint like this -> ENTRYPOINT ["ng", "serve", "-H", "ipObtainedFromPing"] – pelican Jan 16 '18 at 16:55
  • nope, 0.0.0.0 is a special address that binds to all interfaces in your docker container so you need to literally use "0.0.0.0" – whites11 Jan 16 '18 at 16:56
  • Ok let me try that right now; please stand by I'll try running and update you right away – pelican Jan 16 '18 at 16:57
  • Wow!! Just wow, thank you sir, I spent last night and today on this; thank you very much, I learned something today!!! – pelican Jan 16 '18 at 16:59
  • 1
    it was my pleasure – whites11 Jan 16 '18 at 16:59
2

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

services:
  node:
   ports:
     - 4200:4200
yamenk
  • 46,736
  • 10
  • 93
  • 87
1

I recently faced this. For it to work well within a Docker container you need to bind to 0.0.0.0 instead of 127.0.0.1 within your node app server. For example:

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}/`);
});
Dre
  • 1,985
  • 16
  • 13