Dockerize Your Node.js App: MongoDB, Docker Compose, and Mongo Express
A simple guide to using Docker and Docker Compose to containerize a Node.js, Express, MongoDB API, including Mongo Express for DB Management
Introduction
This article will discuss using Docker (and Docker Compose) to containerize your application and its importance in modern development.
Small disclaimer.
This article is part of a series about building a Node.js RESTful API for a to-do task manager using Express, MongoDB, and TypeScript.
Each article builds on the previous one and presents a new topic to make a Node.js application more robust.
You can check the previous articles here:
But why are we using Docker for our application?
When we use Docker, we are containerizing our app into a box (container) that we can use to our advantage.
This box will contain an isolated and scalable instance of our application, which we can consistently reuse (and deploy) across different environments (dev, test, staging, production).
Therefore, Docker is a tool that enables us to run our application by packaging it, along with all the necessary libraries and code, into a container that we can deploy to a machine (your computer, a cloud computer, etc.).
Before I begin the article, I would like to clarify a few points.
Prerequisites
I will assume the following:
You have a basic understanding of JavaScript/TypeScript.
You have Node.js and npm installed on your local machine.
You have Docker or Docker Desktop installed on your local machine. Check here for instructions: https://docs.docker.com/desktop/.
To follow along, you downloaded the repository from https://github.com/daniosoriov/todo-manager.
Even if you’re newer to Docker, I aim to keep explanations straightforward and accessible.
Docker Basics
We already discussed what Docker is—a box (container) with our app.
So now let’s look at its components and how it works in more detail.
Docker Image
A Docker image is a template or blueprint that defines all the software, libraries, and dependencies (packages) needed to run an application within a Docker container.
A Docker image defines what’s inside it and how it will operate.
A few characteristics of a Docker image:
They are read-only: you cannot modify the contents of an image.
Portable: you can use them in different environments, guaranteeing consistency.
Contains what you need: the images contain everything you need for your application.
Docker container
A Docker container is an executable instance built with a Docker image.
They are portable and lightweight, and contain everything we need to run our application (the image).
Some characteristics of a Docker container:
They are self-contained: they contain everything they need to run, so they do not depend on the host’s operating system.
Consistent: they will work on any machine so that we can use them in different environments without issues.
They are lightweight: they use the host’s kernel, becoming more efficient when using resources.
Docker compose
Docker Compose is a tool for defining and running multi-container applications. It is the key to unlocking a streamlined and efficient development and deployment experience.
From Docker’s website.
As stated above, Docker Compose will allow us to run multiple containers in an application.
And why would we need multiple containers?
Since each container is an executable instance of an application, we may need to connect a few systems that rely on each other, such as a backend and a database.
With Docker Compose, we can create a container for the backend and one for the database, and then establish a connection between them.
In our case, we will create three different containers to run our application efficiently:
One container for the Node.js application.
One container for the MongoDB database.
One container for Mongo Express, a client to manage our MongoDB database.
Docker Compose helps us build these three containers to run our application efficiently in any environment.
Dockerizing the Node.js Application
We will need a Dockerfile that includes the instructions for creating the Docker image to dockerize our application.
Keep reading with a 7-day free trial
Subscribe to Daniel’s Substack to keep reading this post and get 7 days of free access to the full post archives.