How To Containerize A MERN Stack Application?

Pankaj SharmaPankaj Sharma
3 min read

Introduction

Containerizing a MERN stack (MongoDB, Express.js, React.js, Node.js) application using Docker ensures consistency, scalability, and ease of deployment across different environments. Containers package the application with all dependencies, eliminating the “works on my machine” problem. By leveraging Docker Compose, we can efficiently manage the backend, frontend, and database as separate services. Refer to the Best MERN Stack Course for complete guidance. This guide will walk you through the step-by-step process of containerizing a MERN application, making it ready for seamless development, testing, and cloud deployment.

Steps To Containerize A MERN Stack Application?

Containerizing a MERN stack (MongoDB, Express.js, React.js, Node.js) application allows developers to create a portable and scalable environment, ensuring that the application runs seamlessly across different systems. Using Docker, we can package our MERN application along with its dependencies into a container.

Step-by-Step Guide

1. Install Docker

Ensure Docker is installed on your system. You can download it from the official Docker website. After installation, verify it by running:

“docker –version”

2. Setup Folder Structure

Organize your MERN project with separate backend and frontend directories:

“mern-app/

│── backend/

│ ├── server.js

│ ├── package.json

│ ├── Dockerfile

│── frontend/

│ ├── src/

│ ├── package.json

│ ├── Dockerfile

│── docker-compose.yml

│── .dockerignore”

3. Dockerizing the Backend (Node.js & Express)

Create a Dockerfile inside the backend/ directory:

Backend Dockerfile

“# Use official Node.js image

FROM node:18

# Set working directory inside container

WORKDIR /app

# Copy package.json and install dependencies

COPY package.json package-lock.json ./

RUN npm install

# Copy application files

COPY . .

# Expose the port

EXPOSE 5000

# Start the backend server

CMD ["node", "server.js"]”

4. Dockerizing the Frontend (React)

Create a Dockerfile inside the frontend/ directory:

Frontend Dockerfile

“# Use official Node.js image

FROM node:18

# Set working directory inside container

WORKDIR /app

# Copy package.json and install dependencies

COPY package.json package-lock.json ./

RUN npm install

# Copy application files

COPY . .

# Build the React application

RUN npm run build

# Install a lightweight web server (e.g., serve)

RUN npm install -g serve

# Expose the frontend port

EXPOSE 3000

# Start the frontend

CMD ["serve", "-s", "build"]”

5. Setting Up Docker Compose

We use Docker Compose to manage multiple containers (MongoDB, backend, frontend) in a single configuration file. Refer to the MERN Certification course for more information.

Create docker-compose.yml in the root folder:

“version: '3.8'

services:

frontend:

build: ./frontend

ports:

- "3000:3000"

depends_on:

- backend

backend:

build: ./backend

ports:

- "5000:5000"

environment:

MONGO_URI: mongodb:// mongo:27017/ mernDB

depends_on:

- mongo

mongo:

image: mongo:latest

ports:

- "27017:27017"

volumes:

- mongo-data:/data/db

volumes:

mongo-data:”

6. Add a .dockerignore File

Create .dockerignore in the project root to prevent unnecessary files from being copied into the container:

“node_modules

npm-debug.log

build

.env”

7. Build and Run the Containers

Run the following command in the project root:

“docker-compose up –build”

This will:

· Build and start MongoDB, backend, and frontend containers.

· Expose the necessary ports.

You can check the running containers with:

“docker ps”

To stop all containers:

“docker-compose down”

Containerizing a MERN application with Docker ensures consistency across environments and simplifies deployment. By using Docker Compose, we manage the MongoDB, backend, and frontend services efficiently. One can join the Mern Stack Course in Hyderabad for the best guidance and opportunities. This setup is beneficial for local development, CI/CD pipelines, and cloud deployments.

Other Full Stack Courses:

Full Stack Developer Course

Java Full Stack Course Online

MEAN Stack Online Course

Python Full Stack Course Online

React Full Stack Developer

Conclusion

To sum up, Containerizing a MERN stack application using Docker enhances portability, scalability, and ease of deployment. By structuring the application into separate backend, frontend, and database containers, we ensure modularity and seamless integration. Using Docker Compose, we manage services efficiently, simplifying both local development and cloud deployments. This approach streamlines the development workflow, reduces dependency conflicts, and ensures the application runs consistently across different environments.

0
Subscribe to my newsletter

Read articles from Pankaj Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pankaj Sharma
Pankaj Sharma

Hi, I’m Pankaj Sharma from Noida and working as an educational blogger.