-1

I am brand new to Docker so pleas bear with me.

Dockerfile:

FROM node:alpine
WORKDIR '/app'
COPY ./package.json .
EXPOSE 4200
RUN npm i
COPY . .
CMD ["npm","start"]

Commands:

docker build -t angu .
docker run  -p 4300:4200 angu

enter image description here enter image description here

I am not sure if I need to include EXPOSE 4200 in Dockerfile. But it is not working either ways.

SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • Including a screenshot of a terminal window is a _faux pas_. I'll see if I can find a canonical answer to this but it does get asked once a day or so (the phrase `listening on localhost:4200` about 10% above the bottom of the image is a big hint). – David Maze Mar 30 '20 at 11:40

1 Answers1

0

Your server inside the container is listening on localhost which is different from the localhost on your machine. The container has its own localhost.

To change the app to listen to outside traffic you need to add --host 0.0.0.0 to the ng serve command in the npm start script in package.json like so:

"start": "ng server --host 0.0.0.0"

You don't need to add EXPOSE in your Dockerfile since it doesn't do much practically, it's mostly for documentation purposes. You're already publishing the port with --p. You can read more about this in this article

Maxim Orlov
  • 1,942
  • 15
  • 18
  • Yep! It worked. I didn't think of adding that because I did the same with a react app and I did not have to mention --host anywhere. Thanks for the help. – SamuraiJack Mar 30 '20 at 11:43