How To Dockerize MERN Stack
A step by step guide with an example project

Docker is an enterprise-ready container platform that enables organizations to seamlessly build, share, and run any application, anywhere. Almost every company is containerizing its applications for faster production workloads so that they can deploy anytime and sometimes several times a day. There are so many ways we can build a MERN Stack. One way is to dockerize it and create a docker image so that we can deploy that image any time or sometimes several times a day.
In this post, we look at the example project and see the step-by-step guide on how we can dockerize the MERN Stack.
- Introduction
- Prerequisites
- Example Project
- Run With Docker Compose
- Build The UI
- Build the API
- Dockerizing the WebApp
- Running The App on Docker
- Summary
- Conclusion
Introduction
Nowadays, it’s very common to dockerize and deploy the Docker image in production with the help of container orchestration engines such as Docker Swarn or Kubernetes. We are going to Dockerize the app and create an image and run it on Docker on our local machine. We could also push that Image into the Docker hub and pull it whenever and wherever we need it.
Here is the complete guide on how to develop and build MERN Stack. If you are not familiar with the process or you want to know before studying this guide, I would recommend you go through it.
Prerequisites
There are some prerequisites for this post. You need to have a NodeJS installed on your machine and some other tools that are required to complete this project.
- NodeJS
- Express Framework
- Mongoose
- MongoDB
- VSCode
- Postman
- nodemon
- dotenv
- Create React App
- Typescript
- react-bootstrap
- Docker
- Docker Compose
NodeJS: As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.
Express Framework: Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
Mongoose: elegant MongoDB object modeling for node.js
MongoDB: MongoDB is a general-purpose, document-based, distributed database built for modern application developers and for the cloud era.
VSCode: The editor we are using for the project. It’s open-source and you can download it here.
Postman: Manual testing your APIs
nodemon: To speed up the development
If you are a complete beginner and don’t know how to build from scratch, I would recommend going through the below articles. We used these projects from this article as a basis for this post.
How to write production-ready Node.js Rest API — Javascript version
Example Project
Here is an example of a simple tasks application that creates, retrieves, edits, and deletes tasks. We actually run the API on the NodeJS server and you can use MongoDB to save all these tasks.

As you add users we are making an API call to the nodejs server to store them and get the same data from the server when we retrieve them. You can see network calls in the following video.

Here is a Github link to this project. You can clone it and run it on your machine.
// clone the project
git clone https://github.com/bbachi/mern-stack-example// React Code
cd ui
npm install
npm start// API code
cd api
npm install
npm run devRun With Docker Compose
Docker Compose is really useful when we don’t have the development environment setup on our local machine to run all parts of the application to test or we want to run all parts of the application with one command. For example, if you want to run NodeJS REST API and MongoDB database on different ports and need a single command to set up and run the whole thing. You can accomplish that with Docker Compose.
Here is the detailed post on how to run MERN Stack on Docker Compose.
Build The UI
Building the UI is very simple with React CLI, all you need to run the following command, and this, in turn, runs the react-scripts build command.

Once you run the above command, the entire build of the react in under the folder build.

Here is the output build folder.

Build the API
We need to use webpack to build the API and use DefinePlugin to pass environment variables. You need to install webpack as dev dependencies with the following commands.
npm install webpack webpack-cli --save-devYou have to put this webpack.config.js file under the folder api. The webpack takes the entry file and builds all the dependencies and puts the output in one file named api.bundle.js.











