<p>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 <code>5000</code> 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.</p>
<p>This is what my folder structure looks like</p>
<pre><code>e-commerce
├── backend
├── docker-compose.yml
├── frontend
└── nginx
    ├── dockerfile
    └── nginx.conf
</code></pre>
<p><strong>Nginx dockerfile:</strong></p>
<pre><code>FROM nginx:latest


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

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

CMD [ &quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot; ]
</code></pre>
<p><strong>nginx.conf file:</strong></p>
<pre><code>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;
        }
    }
}
</code></pre>
<p><strong>Dockerfile for frontend:</strong></p>
<pre><code>FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD npm run dev
</code></pre>
<p><strong>Dockerfile for backend:</strong></p>
<pre><code>FROM node:20-alpine

RUN npm install -g nodemon


WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 5000

CMD [&quot;npm&quot;, &quot;run&quot;, &quot;dev&quot;]
</code></pre>
<p><strong>docker-compose.yml file:</strong></p>
<pre><code>version: '3'
services:
  frontend:
    image: my frontend image from docker hub
    ports:
      - &quot;3000:3000&quot;

  backend:
    image:my backend image from dockerhub
    ports:
      - &quot;5000:5000&quot;

  nginx:
    image: my nginx image from dockerhub
    ports:
      - &quot;80:80&quot;
</code></pre>
<p>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 <code>docker-compose</code> I still need to set up <strong>PM2</strong>.
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.</p>
<p><a href="https://i.sstatic.net/ONPQ0.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ONPQ0.png" alt="sg-screenshot" /></a></p>

CC BY-SA 4.0
2024-04-01T05:15:37.297Z
Welcome to DevOps! Can you access the front end using the Public IP and the port number(3000) assigned for the container??
Ajay
CC BY-SA 4.0
2024-04-01T14:21:25.517Z
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
CC BY-SA 4.0
2024-04-01T14:22:29.980Z
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
<docker><amazon-web-services><docker-compose><amazon-ec2><nginx>
How to deploy dockerized MERN stack app on AWS EC2 instance?
Rana Faraz
# How to deploy dockerized MERN stack app on AWS EC2 instance?

(Asked by: username_0 on 2024-03-31)

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.

**Tags:** docker, amazon-web-services, docker-compose, amazon-ec2, nginx

> **Comments:**
> **username_1**: Welcome to DevOps! Can you access the front end using the Public IP and the port number(3000) assigned for the container??  
> **username_0**: 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  
> **username_0**: 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.  

---

## 0 answers

