How to Run Mongodb as a Docker Container

MongoDB is among the most popular NoSQL databases today. And containers offer easy app usage and scalability. In this article, I’ll show you how to:

  • Configure MongoDB as a container in Docker
  • Set up the Docker platform with docker-compose
  • Create a docker-compose file to create the MongoDB container
  • And more

The last part of this tutorial will look at advanced configurations. These can give you a glimpse of the extensibility of a containerized project. So, we’ll create a self-containing project with a MongoDB instance and Mongo Express web interface on a dedicated network and docker volume to maximize the portability of the project.

Let’s get started.

(This article is part of our MongoDB Guide. Use the right-hand menu to navigate.)

Docker containers & MongoDB

Docker is a tool to create, deploy, and run applications using containers easily. A container is a standard unit of software that can be used to package applications and all the dependencies to a single package. These containers can be run on any server platform regardless of the underlying configuration or hardware structure.

Docker can be used to run MongoDB instances. Setting up MongoDB as a container allows the user to create a portable and extensible NoSQL database. A containerized MongoDB instance behaves exactly like a non-containerized MongoDB instance without having to worry about the underlying configuration.


Interested in Enterprise DevOps? Learn more about DevOps Solutions and Tools with BMC. ›

Installing Docker

In this section, we’ll set up a simple Docker installation to run containers on a Ubuntu-based server. We can get the Docker installation packages from the official Docker repository. Here are the installation steps:

  1. Update existing packages.
sudo apt update && sudo apt upgrade -y

  1. Install prerequisite packages.
sudo apt install apt-transport-https ca-certificates curl software-properties-common

  1. Add the GPG key from the official Docker repository.
curl -fsSL  | sudo apt-key add -

  1. Add the official docker repository to APT sources.
sudo add-apt-repository 
"deb [arch=amd64]  $(lsb_release -cs) stable"

  1. Update the Ubuntu package list.
sudo apt update

  1. Verify the docker repository.
apt-cache policy docker-ce

  1. Install the Docker community edition.
sudo apt install docker-ce

  1. Check the status of the installation with the following command. If the service status returns active (running), Docker is successfully installed and active on the system.
sudo systemctl status docker

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article