1

I dockerized my MERN stack app (Next js, node js, MongoDB) and tried to deploy it on the AWS EC2 instance. When I try to access my backend on port 5000 via AWS Public IP, it works fine but when I try to access the front end, the terminal gets stuck making the SSH throw an error if I try to reload. I have to restart the instance to access the server and the issue persists.

This is what my folder structure looks like

e-commerce
├── backend
├── docker-compose.yml
├── frontend
└── nginx
    ├── dockerfile
    └── nginx.conf

Nginx dockerfile:

FROM nginx:latest


RUN rm /etc/nginx/conf.d/*

COPY ./nginx.conf /etc/nginx/conf.d/

CMD [ "nginx", "-g", "daemon off;" ]

nginx.conf file:

events {}

http {
    server {
        listen 80;
        server_name here my aws public ip;

        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    }
}

Dockerfile for frontend:

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD npm run dev

Dockerfile for backend:

FROM node:20-alpine

RUN npm install -g nodemon


WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 5000

CMD ["npm", "run", "dev"]

docker-compose.yml file:

version: '3'
services:
  frontend:
    image: my frontend image from docker hub
    ports:
      - "3000:3000"

  backend:
    image:my backend image from dockerhub
    ports:
      - "5000:5000"

  nginx:
    image: my nginx image from dockerhub
    ports:
      - "80:80"

Later, I want to set up GitHub ci/cd pipelines and use a custom domain to access the website. I am not sure if I am using docker-compose I still need to set up PM2. I am also posting my inbound rules I don't know why the front end is not working. I am a beginner in AWS deployment and containerization. I am improving my skills please help me I have been stuck in this for many days I saw a lot of videos and watched multiple videos but not a single article or video doing what I am trying to do.

sg-screenshot

3
  • Welcome to DevOps! Can you access the front end using the Public IP and the port number(3000) assigned for the container??
    – Ajay
    Apr 1 at 5:15
  • no i can't. i can access backend but whenever i try to access frontend then my terminal is stuck and i have to stop the instance and restart it. The backend works fine. one more thing in my local its works fine, frontend and backend both. i am facing this issue when i deploy site on ec2
    – Rana Faraz
    Apr 1 at 14:21
  • This is the error terminal gives if i try to reload the ec2 terminal. Failed to connect to your instance Error establishing SSH connection to your instance. Try again later.
    – Rana Faraz
    Apr 1 at 14:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.