How to Add MongoDB to Docker: A Practical Step-by-Step Guide
If you're a developer or hobbyist looking to spin up a MongoDB instance quickly without installing it directly on your machine, Docker makes it incredibly simple. In this post, I’ll walk you through the process of adding MongoDB to Docker — whether for local development, testing, or as part of a larger containerized app setup.
Why Use MongoDB with Docker?
MongoDB on Docker lets you:
- Avoid cluttering your base system with database installations.
- Run isolated instances easily.
- Quickly reset your database environment.
- Version control your database setup configuration with
docker-compose
.
Prerequisites
Before starting:
- Install Docker on your system (Windows/Mac/Linux).
- Have basic familiarity with the terminal/command line.
- Optional but recommended: Install Docker Compose.
Step 1: Pull the Official MongoDB Docker Image
Docker Hub hosts an official MongoDB image maintained by the MongoDB team.
Open your terminal and run:
docker pull mongo
This command downloads the latest official MongoDB image.
Step 2: Run a Single MongoDB Container
Start a new container running MongoDB:
docker run -d --name mymongo -p 27017:27017 mongo
Explanation:
-d
runs the container detached (in background).--name mymongo
gives your container an easy-to-use name.-p 27017:27017
publishes port 27017 from the container to the host (default MongoDB port).
You can verify it’s running by checking containers:
docker ps
Step 3: Connect to Your MongoDB Instance
If you have the mongo
shell installed on your host machine, connect like this:
mongo --host localhost --port 27017
Alternatively, you can get an interactive mongo shell inside the container:
docker exec -it mymongo mongo
Once inside, try inserting some data:
use testdb
db.users.insertOne({ name: "Alice", age: 29 })
db.users.find()
Step 4 (Optional): Persist Data Using Docker Volumes
By default, data inside your container is ephemeral — when you delete the container, you lose your data.
To persist data in a volume outside the container, run:
docker run -d --name mymongo -p 27017:27017 \
-v mongodbdata:/data/db \
mongo
Explanation:
-v mongodbdata:/data/db
mounts a Docker volume namedmongodbdata
at/data/db
, which is where Mongo stores its data files.
This way, even if your container stops or is removed, data remains safe in mongodbdata
.
You can list volumes with:
docker volume ls
Step 5: Use Docker Compose for More Complex Setups
If you want to add Mongo as part of a multi-container setup (e.g., along with a backend app), use Docker Compose. Create a file named docker-compose.yml
in your project folder with this content:
version: "3.8"
services:
mongodb:
image: mongo
ports:
- "27017:27017"
volumes:
- mongodbdata:/data/db
volumes:
mongodbdata:
Start services by running:
docker-compose up -d
Your app can now connect to Mongo at localhost:27017
.
Troubleshooting Tips
- If another app is using port 27017 on your host, change it in the docker run command like so:
-p 28000:27017
Your host connects on port 28000
.
- To stop/remove containers gracefully:
docker stop mymongo
docker rm mymongo
Or for docker-compose setups:
docker-compose down
Conclusion
Adding MongoDB to Docker is straightforward and extremely useful for development workflows. Whether testing quickly or orchestrating several services via docker-compose, you get fast spins up/down without polluting your local environment.
Give it a try today! Once comfortable with these basics, you can explore further topics like setting up users/passwords for secured access or running shards and replicas — all powered by containerized MongoDB instances.
Happy coding!
If you'd like me to help create content around specific titles or hooks next time, feel free to provide those details!