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:
- Instance Public IPv4 address: 13.48.193.158 or
- 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
Container on Instance logs
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" ]
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
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
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.
change it to port = 80
Once again, use nginx is much better