
Docker makes it easy to run your applications anywhere by packaging everything into containers. In this guide, I will Dockerize a MERN stack application step by step. starting with the frontend, then backend, and finally setting up docker-compose for the whole project.
Let’s start with the frontend. Inside your frontend folder, create a new file named Dockerfile and paste the following code:
Now that the frontend is dockerized, let’s move on to the backend.
Inside your backend folder, create a new file called Dockerfile and add the following code:
At this point, you have two separate Dockerfiles (one for frontend and one for backend). Running them manually with multiple docker run commands can be messy. This is where Docker Compose makes life easier, it lets you define and run multi-container apps in a single configuration file.
Inside the root of your project (same level as frontend and backend folders), create a new file called docker-compose.yml and add the following:
You can download the code here from my repository.
GitHub Link: https://github.com/richard9004/mern_stack_with_docker