skip to Main Content

I tried to dockerize an Angular app.. I tried with two different Dockerfile, because I was not able to run this.. First file I tried:

FROM node:latest as node

WORKDIR /app

COPY . .
RUN npm install
EXPOSE 4200
RUN npm run build --prod

FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=node /app/dist/front-end-blog-demo /usr/share/nginx/html

Second file (actual file):

FROM node:16-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
CMD ["npm", "start"]

I build it with: docker build -t mydhid/project .
pushed it, and ran it with: docker run -p 4200:4200 -d mydhid/project
It seems that is listening fine and everything went fine, but when I go to localhost:4200 it doesn’t show anything.. This is the port that is actually really listening:
enter image description here
This is the docker desktop log:
enter image description here
And this is the docker ps command:
enter image description here

Everything seems ok, but still (with both dockerfile images) I can’t see anything displayed on my localhost:4200.. I tried all that with another port, 3000, but same issue that everything looks fine but not showing anything.. what should I do? Yesterday it was working, today I tried Docker compose to run both Front End and Back End, and from today seems nothing working..
I’m 100% the apps work fine, because when I run it from terminal with: ng serve -o it opens me the localhost:4200 and my login page

2

Answers


  1. Chosen as BEST ANSWER

    I solved it, and I'm not sure why, but I got it working using port 80 inside the Docker container. The port on my localhost is still 4200, but inside the container, it's 80, so I ran the container like this:

    docker run -p 4200:80 -d mydhid/project

    I think I have changed something in the dockerfile too, here's the one I'm using right now if it can be helpful:

    FROM node:latest as node
    WORKDIR /app
    COPY . .
    RUN npm install
    RUN npm run build --prod
    
    FROM nginx:alpine
    COPY --from=node /app/dist/front-end-blog-demo /usr/share/nginx/html
    

    I tried again running it as 4200:4200, and it's not working. It seems that the Docker port must be mandatory at 80.


  2. It seems that the Docker port must be mandatory at 80

    1. It’s is not mandatory . check if the build has been placed where it
      supposed to be in this context its /usr/share/nginx/html.

    2. Are you facing any http error then try searching for that specific
      HTTP error on the internet.

    3. Also you can’t use the same port twice for host if the port is
      already listening on your docker host (your pc).

    4. Try killing your existing process with the help of pid

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