Home > Back-end >  Doesn't connect to localhost when running within a docker container using nodejs app & anguar
Doesn't connect to localhost when running within a docker container using nodejs app & anguar

Time:01-12

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..

CodePudding user response:

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

CodePudding user response:

The EXPOSE is only part of the solution. You also need to publish the port when creating the container. You haven't provided detail about how you start the container, but the proper way in your case would be:

docker run -d -P your_image_name

the -P flag publishes all exposed ports.

Another way - to explicitly publish specific port:

docker run -d --publish 4200:4200 your_image_name

Read more in Docker documentation

  •  Tags:  
  • Related