MongoDB is a popular document-oriented database management system and a common choice for developers to handle their data needs, from simple CRUD operations to advanced AI apps. It offers native, flexible document data modeling that aligns with application objects, reducing mapping overhead and improving developer productivity.

MongoDB provides language-specific client drivers, high availability through multi-node replication, rich querying and analytics, advanced indexing (including lexical and vector), and horizontal scalability for high-throughput workloads. It also supports modern workload types such as time series, search, and vector embeddings as first-class capabilities. MongoDB is available in Community and Enterprise Editions, and can be deployed via user install, Docker images, or managed cloud deployments via MongoDB’s Atlas service.

What is Docker?

Docker is a platform that simplifies the process of building, testing, and shipping applications. Software is packaged into standardized, lightweight units called images, which are deployed to containers. Each Docker image contains the application code, runtime, system libraries, and any dependencies required to run consistently across environments.

In addition to building your own containers, Docker enables you to download prebuilt images from Docker Hub. If you’re coding locally, running automated CI/CD tests, or deploying to the cloud, Docker lets you create consistent, reproducible environments. With a few simple commands or through its API, you can build images, spin up containers, orchestrate updates, and automate entire workflows.

Setting up a MongoDB container

In this tutorial, we’ll use a specific Docker feature called Compose. This feature enables you to utilize a YAML file to deploy one or more images with customized configurations. The best part, in my opinion, is that Compose manages all networking between containers. If you’ve ever struggled to get one container to talk to another, this feature alone makes Docker Compose worth using.

First, you will need to have Docker Desktop or a Docker server. For example, I have a micro PC running Docker and Portainer as my home lab server.

The second step is to decide which MongoDB image you want to deploy. There are many images available, but start with the official MongoDB images for either community or enterprise servers. There are other official MongoDB images you can try out if you’re interested.

Create a file named compose.yaml

<code>%> vi compose.yaml

Copy and paste the below YAML into the file

<code>version: '3.9'
<code>services:
<code>mongo:
image: mongodb/mongodb-community-server:latest
container_name: mongo_dev
ports: ["27017:27017"]
environment:
<code>MONGO_INITDB_ROOT_USERNAME: <your_username>
MONGO_INITDB_ROOT_PASSWORD: <your_password>

   volumes:

     - mongo_data:/data/db

   restart: unless-stopped

volumes:

 mongo_data:

Make sure to modify the file to replace <your_username> and <your_password> with values you’ll use.

Start container

%> docker compose up

Once this command is run, Docker downloads the specified MongoDB image from Docker Hub and creates a container to run it.

Once the container is up, you should be able to connect to it on localhost:27017 using mongosh (MongoDB’s command-line tool), MongoDB Compass, or with an app using any of the MongoDB client drivers.

We could stop there, and that’d be usable, but what if we want to run our code against MongoDB to make CI/CD testing easier and automated? We simply add another entry to the compose.yaml file for the app.

Add an app container to connect to MongoDB

Add the code in color below to the compose.yaml file. You will need to change the username and password locations, update the path to your app in volumes, and make other customizations for deploying your app, but this should give you a start on what you need.

version: '3.9'

services:

 mongo:

   image: mongodb/mongodb-community-server:latest

   container_name: mongo_dev

   ports: ["27017:27017"]

   environment:

     MONGO_INITDB_ROOT_USERNAME: <your_username>

     MONGO_INITDB_ROOT_PASSWORD: <your_password>

   volumes:

     - mongo_data:/data/db

   restart: unless-stopped

 app:

   container_name: my_app

   build: ./app  # assumes there's a Dockerfile in ./app

   ports:

     - "3000:3000"

   environment:

     MONGO_URI: mongodb://<your_username>:<your_password>@mongo:27017/mydb?authSource=admin

     NODE_ENV: development

   depends_on:

     - mongo

   volumes:

     - ./app:/usr/src/app

   command: npm start

   restart: unless-stopped

volumes:

 mongo_data:

The key part is in the MONGO_URI definition, which specifies ‘mongo’ as the server name to connect to. This is one of the benefits of using Docker Compose, as that server name is defined and maintained by Compose. Your app uses that environment variable, and it connects to MongoDB. There are more secure ways to do usernames and passwords, but when it comes to quick testing and these containers are thrown away each time, sometimes I don’t bother. If this were a container bound for production, I’d definitely do things differently, and you should too.

Conclusion

Running MongoDB with Docker provides a consistent, reproducible, and efficient workflow for both development and testing. Using containers lets you isolate dependencies, eliminate environment drift, and streamline local testing without manual configuration. Docker Compose further simplifies multi-service setups, enabling applications and databases to run in unison with minimal effort. When combined with running your app, automating your CI/CD pipelines becomes easier.

FAQ

Can I run MongoDB on Docker?

Yes, MongoDB can be run on Docker by using the official MongoDB Docker image with a simple docker run command.

How to create a Docker image with MongoDB?

To create a Docker image with MongoDB, you can use the official MongoDB image in a Dockerfile like this: FROM mongo, then build it using docker build -t mongo-image

How to connect Docker container to local MongoDB?

To connect a Docker container to a local MongoDB, use the host network mode with --network host or link the container to the host machine using --add-host=host.docker.internal:host-gateway when running the container.

Kirk is a Staff Developer Advocate at MongoDB, specializing in NoSQL Databases, AI, and cloud technologies. He writes code, technical articles, and videos primarily to help developers get up and running more quickly and efficiently. Drawing on his experience at AWS, Couchbase, Redis, and many more, he helps MongoDB’s top customers to learn, understand, and implement best practices around NoSQL data modeling, using AI in practical ways, and overall cloud architecture.