skip to Main Content

I am using Nodejs with angular app. I deploy angular to my docker desktop. It is running but when I want access from my pc using localhost from PC browser , it does not connect..

Here is docker image running

enter image description here

Here is Dockerfile code

    FROM node:16.13.1 as build
    WORKDIR /app
    COPY package.json /app
    COPY . .
    RUN npm install
    RUN npm run build --prod
    RUN ls -la dist
    FROM nginx:alpine
    COPY --from=build /app/dist /usr/share/nginx/html
    EXPOSE 4200

When I want access using http://localhost:4200/ from my pc , it is not connecting.
Please help to find out the problem..

2

Answers


  1. nginx:alpine expose on port 80.
    port 4200 is used for development when you run npm start or ng serve.

    Login or Signup to reply.
  2. EXPOSE 4200 does nothing and is in this case misleading. You use EXPOSE to document what port the image listens on. As Antoine says, nginx listens on port 80, so if you want an EXPOSE statement, it should be EXPOSE 80.

    Remove the EXPOSE statement since it’s misleading

    FROM node:16.13.1 as build
    WORKDIR /app
    COPY package.json /app
    COPY . .
    RUN npm install
    RUN npm run build --prod
    RUN ls -la dist
    FROM nginx:alpine
    COPY --from=build /app/dist /usr/share/nginx/html
    

    When you run your container, you can then map port 80 in the container to port 4200 on your machine like this

    docker run -d -p 4200:80 my_image_name
    

    You’ll then be able to load your app by going to http://localhost:4200/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search