1

I created a docker container using official node image.

docker pull node

then I created an angular 2 app using angular-cli.

ng new docker-demo

then I ran my docker container from the directory of my angular2 app using following command:

cd docker-demo

& then inside the directory :

docker run -p 8080:4200 -v $(pwd):/var/www -w "/var/www" node npm start

where pwd stands for directory of my angular2 app.

now in my console I can see my angular2 app getting bundled & running just like it runs on my local machine.

but when I try to access it from my browser using ip of my docker-machine :

http://192.168.99.100:8080/

it doesn't work.

PS: angular2 app works perfectly fine in my local system.

is there any extra configuration required?

any inputs?

thanks in advance.

Bhushan Gadekar
  • 13,485
  • 21
  • 82
  • 131
  • What if you first start your angular app container and then try to test if app is really working by `exec`uting curl: `docker exec -it curl localhost:4200`. Does it work? – ronkot Sep 19 '16 at 09:00

3 Answers3

8

I had a similar problem. Was able to fix it using ng serve --host 0.0.0.0 as the starting command for my container. It is important to remember exposing the right ports at build time. Make sure you use EXPOSE 4200 or any other port you want to expose your app in your Dockerfile. 4200 is the default for Angular2/4 apps, so most of the time is the best choice.

If you're using Docker compose, the same thing. Remember to expose the port and use the appropriate arguments in the ng serve command. Hope it helps you and any other fellow captain struggling with the same problem.

Henry Ollarves
  • 509
  • 7
  • 18
  • ng serve --host 0.0.0.0 did the trick. Even if you are EXPOSE-ing the port, angular itself would refuse to host outside localhost, unless explicitly stated. Thanks! – Ardee Aram Mar 04 '18 at 03:28
0

Your docker command should be like this:

docker run --rm --name my-container -it -p 8080:4200 -v $(pwd):/var/www -w "/var/www" node npm start
  • --rm -> will remove you container when you stop it
  • -it -> will folow log output
  • --name -> give a name for your container

You need -it if you want follow the log or -d if you dont wana folow the log output.

I recomment to use --name your-container-name to make easy to remove/stop/start your container.

To check the status of you container:

docker ps -a

Where -a will show all containers, inclusive stoped containers.

See more here: Docker Documentation

Edgard Leal
  • 2,592
  • 26
  • 30
0

You need to expose the port as per the image installation guide:

Create a Dockerfile in your Node.js app project

FROM node:4-onbuild
# replace this with your application's default port
EXPOSE 8888

You can then build and run the Docker image:

$ docker build -t my-nodejs-app .
$ docker run -it --rm --name my-running-app my-nodejs-app

So in your case:

> ng new docker-demo
> cd docker-demo
> echo "FROM node:6.6.0-wheezy
EXPOSE 4200" > Dockerfile
> docker build -t my-nodejs-app .
> docker run --rm --name docker-demo -p 8080:4200 -v $(pwd):/var/www -w "/var/www" my-nodejs-app npm start

You app should be accessible on 127.0.0.1:8080 (replace 127.0.0.1 with relevant docker ip).

jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • 1
    Exposing port through Dockerfile is not necessary. One can always publish any ports on container using `docker run -p :`. – ronkot Sep 19 '16 at 08:56