skip to Main Content

I created simple nodeJS application with docker and when I run it locally, everything works fine. I’ve installed Docker and Docker-Compose on my EC2 instance then deployed docker container on it but web-app is not accessible even through:

  1. Instance Public IPv4 address: 13.48.193.158 or
  2. Public IPv4 DNS: ec2-13-48-193-158.eu-north-1.compute.amazonaws.com

Test Project files:
https://github.com/mostafanabil71/Simple-test-NodeJSApp-with-docker.git

EC2 Instance inbound rules:
enter image description here

Container on Instance logs

enter image description here

Dockerfile:

FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 8080
CMD [ "node", "app.js" ]

enter image description here

a. Docker version 26.1.4, build 5650f9b
b. npm version 10.8.1
c. node version v12.22.9
d. Docker Compose version v2.27.1-desktop.1
e. npm express: 10.8.1
f. installed platform on EC2 is ubuntu-noble-24.04-amd64-server-20240423

2

Answers


  1. Chosen as BEST ANSWER

    problem was solved after lunching the Security groups and editing the inbound rules as below image and easily APP was accessible by entering IP:Port in the web URL

    enter image description here


  2. In your Dockerfile, you exposed only port 8080, you didn’t expose the port 80. if you want to run your application on port 8080, add it as well on your security group. Furthermore, I will recommend you run your nodejs server behind nginx

    Also upon, checking your application source code, you have to replace these lines with the correct port.

    const path = __dirname + '/views/';
    const port = 8080;
    

    change it to port = 80

    Once again, use nginx is much better

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