Skip to main content

Running MongoDB in Docker: A Complete Guide with Examples

This guide walks you through running MongoDB in Docker, covering both quick command-line deployments and full Docker Compose configurations. It’s perfect for developers looking to self-host or experiment locally without immediately jumping into cloud hosting.
Jul 13, 2025  · 5 min read

So you're looking to self-host MongoDB or start dabbling with it in a local setting? There are a few options to get started if you don't want to jump directly into MongoDB Atlas, one of those options being containers with Docker. 

Making use of Docker is a solid choice when managing your MongoDB instance because it doesn't take more than a minute to do and it is easy to maintain or move between host computers.

In this article, we're going to see a few approaches toward deploying MongoDB with Docker and explore a few tips and tricks along the way. You can also check out this video to get a quick rundown of the topic.

Prerequisites

To follow along with this tutorial, you'll need to have the Docker Engine installed on your computer. This could be on macOS, Windows, or Linux. You don't explicitly need Docker Desktop installed, but it might make your Docker experience easier depending on which operating system you're using. 

I'm using the Docker Engine through Rancher Desktop on macOS, for example, and it works fine.

To brush up on your Docker skills, it’s worth checking out Docker for Beginners: A Practical Guide to Containers to catch up on the basics.

Downloading a MongoDB Docker Image

Before you can start running MongoDB in a Docker container, you need to first download an image that meets your needs. From the command line, execute the following:

docker pull mongodb/mongodb-community-server:latest

The above command will download the official MongoDB Community Edition (CE) image with the "latest" tag. 

It is important to note that mongodb/mongodb-community-server and mongodb/mongodb-enterprise-server are official Docker images maintained by MongoDB. All other images are maintained by the external community and may give you different or unexpected results.

With an image downloaded, we can look at deployment options.

Running MongoDB as an Isolated Container With the Docker Run Command

The quickest way to get MongoDB deployed with Docker is to just use the docker run command with a few configuration parameters. For example, you can execute the following from your command line:

docker run -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=nraboy -e MONGO_INITDB_ROOT_PASSWORD=password1234 --name mongodb mongodb/mongodb-community-server:latest

The above command will take our image and deploy it as a container named "MongoDB." The container will be port-mapped, meaning the host will be able to interact with it on port "27017." The root authentication information is also set in this example, but depending on your deployment needs, you can use Docker Secrets or another protected way to pass sensitive information.

In the above example, all data is stored within the container. The MongoDB data, which includes databases, collections, and even documents, will only persist for as long as that particular container exists. It might be appropriate for testing, but many times, you'll want reliable persistence of your MongoDB data in Docker. To handle this, you should create a Docker volume using the following command:

docker volume create mongodb

To tell your container to use that volume, you can change your deployment command to look more like the following:

docker run -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=nraboy -e MONGO_INITDB_ROOT_PASSWORD=password1234 -v mongodb:/data/db --name mongodb mongodb/mongodb-community-server:latest

Notice that this time, we're specifying the volume to use. When using a volume, you can be sure that your data will exist even after terminating or updating your container.

With MongoDB running from within a Docker container, you can connect to it using mongodb://nraboy:password1234@localhost:27017 as the connection string from a tool of your preference.

Including MongoDB in Your Stack or Project With Docker Compose

If you're planning on using MongoDB with a particular project, also managed with Docker, it probably makes sense to manage MongoDB with a Docker Compose configuration. These configurations are often used for sandboxing your projects, often referred to as stacks.

Create a docker-compose.yml file somewhere on your computer with the following:

services:
    mongodb:
        image: mongodb/mongodb-community-server:latest
        container_name: mongodb
        restart: unless-stopped
        ports:
            - "27017:27017"
        volumes:
            - mongodb:/data/db
        environment:
            MONGO_INITDB_ROOT_USERNAME: nraboy
            MONGO_INITDB_ROOT_PASSWORD: password1234

volumes:
    mongodb:
        external: true

The above configuration uses information that we've already seen in the singular command line command. Like with the other method, the expectation here is that you've already created a mongodb volume to be managed by Docker. Once again, if you need to do this, execute the following command:

docker volume create mongodb

Of course, you can manage your Docker volumes however it makes sense to you. Creating a volume is only one of several possibilities.

With your command line set to the same working directory as your docker-compose.yml file, execute the following command:

docker compose up -d

The above command starts your stack in detached mode. 

You can validate that everything is working by attempting to connect to your instance with a tool like MongoDB Compass, one of the language drivers like Node.js, or through the MongoDB Shell. 

If following the example exactly, your connection string will be mongodb://nraboy:password1234@localhost:27017, but depending on how you've followed along, it could be different.

To bring down your MongoDB instance, execute the following from the command line with your YAML configuration in the same working directory:

docker compose down

Because we're using an external volume, the next time you start your container, everything will still be persisted.

Conclusion

Docker makes it easy to work with MongoDB in a self-hosted or local environment. You don't need to worry about the general operating system configurations or the experience you'll get between one computer and another. You'll get a consistent experience every time. 

To take things to the next step, it is worth learning how to create a database in MongoDB.

Become a Data Engineer

Become a data engineer through advanced Python learning
Start Learning for Free

FAQs

Can I run multiple MongoDB containers at the same time with Docker?

Yes, you can run multiple MongoDB containers as long as each one maps to a different port or runs on a separate Docker network to avoid conflicts.

How do I back up a MongoDB container's data in Docker?

To back up data, you can use docker cp to extract files from the volume or use mongodump inside the container. Persisted volumes also help maintain data between sessions.

Is it safe to store MongoDB passwords in the Docker Compose file?

It's not ideal. Instead, use Docker Secrets or environment variable files (.env) that are excluded from version control to manage sensitive credentials securely.

Does MongoDB run well in Docker on Windows?

Yes, MongoDB runs smoothly in Docker across Windows, macOS, and Linux, especially when using WSL 2 or Docker Desktop for Windows to ensure compatibility.

Can I use MongoDB with Docker in production?

Yes, but it's recommended to set up proper networking, storage volumes, authentication, and backups before using Dockerized MongoDB in a production environment.

How do I connect to MongoDB in Docker from another container?

Use Docker Compose and reference the MongoDB container by its service name as the hostname. This enables service-to-service communication on the same network.

Do I need to install MongoDB locally if I use Docker?

No. Docker runs MongoDB in a containerized environment, so you don’t need to install MongoDB directly on your host machine.

Can I use MongoDB Compass with a Dockerized MongoDB instance?

Absolutely. Just use the correct connection string (e.g., mongodb://user:pass@localhost:27017) to connect from MongoDB Compass to your local container.

How do I monitor the performance of MongoDB in Docker?

You can integrate tools like mongostat, mongotop, or external monitoring solutions such as Prometheus and Grafana for performance tracking inside Docker.

Will my MongoDB data persist in a Docker container?

As long as you’ve chosen to map the container directory to a volume on your host computer, the data should persist without issues between updates and launches.

Should I use Docker or MongoDB Atlas?

In most scenarios, it is better to use MongoDB Atlas because of its available features, pricing, and ease of use, but MongoDB through Docker is also a valid option.

Do I have to use Docker Desktop with MongoDB?

Docker Desktop makes working with Docker easier most of the time, but it is not a requirement. Most Linux distributions include the Docker Engine, and other interfaces like Rancher Desktop work, as well.


Nic Raboy's photo
Author
Nic Raboy

Nic Raboy is a Developer Relations Lead at MongoDB where he leads a team of Python, Java, C#, and PHP developers who create awesome content to help developers be successful at including MongoDB in their projects. He has experience with Golang and JavaScript and often writes about many of his development adventures.

Topics

Learn more about MongoDB with these courses!

Course

Introduction to MongoDB in Python

3 hr
22.2K
Learn to manipulate and analyze flexibly structured data with MongoDB.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

What Is MongoDB? Key Concepts, Use Cases, and Best Practices

This guide explains MongoDB, how it works, why developers love it, and how to start using this flexible NoSQL database.
Karen Zhang's photo

Karen Zhang

15 min

Tutorial

How to Install MongoDB on Ubuntu: A Step-by-Step Guide for Beginners

Learn how to install MongoDB on Ubuntu, configure the service, and verify the setup in this beginner-friendly tutorial.
Nic Raboy's photo

Nic Raboy

Tutorial

Docker for Beginners: A Practical Guide to Containers

This beginner-friendly tutorial covers the essentials of containerization, helping you build, run, and manage containers with hands-on examples.
Moez Ali's photo

Moez Ali

Tutorial

Docker Compose Guide: Simplify Multi-Container Development

Master Docker Compose for efficient multi-container application development. Learn best practices, scaling, orchestration, and real-world examples.
Derrick Mwiti's photo

Derrick Mwiti

Tutorial

Run a Docker Image as a Container: A Practical Beginner’s Guide

This tutorial teaches you how to confidently run Docker images using docker run, with clear examples, practical tips, and troubleshooting advice.
Srujana Maddula's photo

Srujana Maddula

Tutorial

How to Create a Database in MongoDB: A Quick Guide

Discover how to create a MongoDB database from the shell or with a script, plus common pitfalls to avoid.
Nic Raboy's photo

Nic Raboy

See MoreSee More