MongoDB Docker con­tain­ers are easy to replicate and scale. If the load on the database increases, you can start ad­di­tion­al MongoDB con­tain­ers. This keeps the database per­for­mance stable.

Does MongoDB run in a Docker container?

MongoDB can run ef­fec­tive­ly in a Docker container. There are two types of MongoDB images on Docker Hub: the Community Edition and the En­ter­prise Edition. The choice between these two versions depends on your specific needs. The Community Edition is usually ideal for non-com­mer­cial use or smaller setups. The En­ter­prise Edition, however, includes extra features and support for larger ap­pli­ca­tions or busi­ness­es with advanced needs like en­cryp­tion, auditing, and LDAP in­te­gra­tion.

If the pre-built MongoDB images on Docker Hub don’t fully meet your needs, Docker allows you to create a custom Docker image using a Dock­er­file. With a Dock­er­file, you can select the MongoDB version, configure specific options (like au­then­ti­ca­tion methods), and install any ad­di­tion­al tools or drivers needed for your en­vi­ron­ment.

How to use MongoDB as a Docker container step by step

Docker is known for its light­weight vir­tu­al­iza­tion, enabling porta­bil­i­ty and con­sis­ten­cy across various de­vel­op­ment and pro­duc­tion en­vi­ron­ments. Below, we’ll guide you step by step on how to set up MongoDB con­tain­ers in Docker. For beginners, we suggest checking out our Docker tutorial: In­stal­la­tion and first steps, which provides a detailed in­tro­duc­tion to the container platform.

Step 1: Download MongoDB Docker image

To create a MongoDB Docker container, we first obtain the ap­pro­pri­ate image for running Docker Hub. Open your terminal or command line and run the following command:

docker pull mongo:latest
bash

If the image already exists locally, Docker won’t download a new version unless you specif­i­cal­ly indicate the version you want to use (for example mongo:4.4).

Step 2: Launch MongoDB Docker container

Once the Docker image for MongoDB has been suc­cess­ful­ly down­loaded, you can start a container based on this image:

  • docker run: Launches a new Docker container
  • --name mongodb-container: Names the container “mongodb-container”
  • -d: Use this parameter to launch the container in the back­ground (detached mode). This means that the terminal can still be used while the container is running
  • -p 27017:27017: Opens the MongoDB standard port 27017 of the container on your host system
  • mongo:latest: Provides the in­struc­tion to obtain the latest available image

Step 3: Check that the container is running

To ensure that the container has been launched suc­cess­ful­ly, enter the following command:

docker ps
bash

This in­for­ma­tion provides a quick overview of the active MongoDB Docker con­tain­ers on your system, including how long they have been running and which ports they are using. For more details, such as stopped con­tain­ers or specific filtering options, it’s rec­om­mend­ed to use the docker ps -a command.

Step 4: Establish a con­nec­tion to MongoDB in the Docker container

Now you can establish a con­nec­tion to the MongoDB instance in your Docker container. To do this, launch the MongoDB Shell directly in the container:

  • docker exec: Executes a command in the running container
  • -it: Allows in­ter­ac­tion with the terminal in the container
  • mongodb-container: The name of the set-up MongoDB container
  • mongo: Launches the MongoDB shell

Once you’ve executed this command, you should see the MongoDB shell ready to receive commands. You can now enter the usual MongoDB commands to manage or query your database.

Step 5: Perform op­er­a­tions in the MongoDB shell

You can now display all existing databases on the MongoDB server as follows:

show databases
bash

The output contains the names of the databases:

admin   0.000GB
local      0.000GB
test       0.000GB
bash

To work with a specific database in the MongoDB shell, use the following command:

use mydatabase
bash

Use the following command to switch to the database named “my­data­base”. If the database doesn’t exist, it will be created au­to­mat­i­cal­ly when a document is inserted.

You can also retrieve documents from a specific col­lec­tion:

db.users.find()
bash

The MongoDB shell returns all documents that are stored in the “users” col­lec­tion.

{ "_id": ObjectId("609823e9f9a5f7f364fc3f90"), "username": "alice", "age": 28 }
{ "_id": ObjectId("609823f2f9a5f7f364fc3f91"), "username": "bob", "age": 32 }
{ "_id": ObjectId("609823f9f9a5f7f364fc3f92"), "username": "charlie", "age": 25 }
bson

Each document contains a unique _id field (MongoDB-specific iden­ti­fi­er) and other fields such as username and age.

Find more in­for­ma­tion on Docker tools and a MongoDB pre­sen­ta­tion including a com­par­i­son with MySQL in our guide.

Managed Database Services
Time-saving database services
  • En­ter­prise-grade ar­chi­tec­ture managed by experts
  • Flexible solutions tailored to your re­quire­ments
  • Leading security in ISO-certified data centers
Go to Main Menu