Running MongoDB with Docker

Docker provides a repeatable way to run MongoDB in a container. This is particularly useful when several developers need the same database version and local configuration.

The following example binds the MongoDB port to the loopback interface so that the service is reachable from the host machine without being published on every network interface.

Step 1: Create a persistent volume

docker volume create mongodb-course-data

A named volume stores database files separately from the container's writable layer.

Step 2: Start the container

docker run -d \
 --name mongodb-course \
 -p 127.0.0.1:27017:27017 \
 -e MONGO_INITDB_ROOT_USERNAME=courseadmin \
 -e MONGO_INITDB_ROOT_PASSWORD=local-dev-password-change-me \
 -v mongodb-course-data:/data/db \
 mongo:8.0

This command is intended for local development. The sample credentials must not be reused in production.

The official image initializes the root user when initializing a fresh database directory. If you reuse an existing volume, changing the initialization environment variables does not automatically change the password of an already-created user.

For repeatable team environments, pin a specific image version and, when required by your operational process, a verified image digest. Review the official image documentation for version-specific behavior.

Step 3: Inspect the container

docker ps

This lists running containers. To inspect MongoDB startup logs:

docker logs mongodb-course

Step 4: Connect with mongosh

If mongosh is installed on your host:

mongosh "mongodb://courseadmin:local-dev-password-change-me@127.0.0.1:27017/admin?authSource=admin"

The URI authenticates the user against the admin database. The command includes the example password for demonstration only; avoid passing real secrets through shell commands that may be recorded in history.

Step 5: Test persistence

Insert a document:

use mongodb_course_lab

db.dockerCheck.insertOne({
 message: "Persisted in a Docker volume",
 createdAt: new Date()
});

Stop and remove the container:

docker stop mongodb-course
docker rm mongodb-course

Start a new container using the same volume and configuration. The previously inserted document should remain because the data is stored in the named volume.

Important distinction: container versus volume

Removing a container is different from removing a volume. The following command removes the volume and permanently deletes the data stored in that volume when removal succeeds:

docker volume rm mongodb-course-data

Do not execute it if you need the data.

Official image documentation: https://hub.docker.com/_/mongo

A named volume is persistent storage, not a backup

A Docker volume protects data from ordinary container removal, but it does not replace backups, tested restoration procedures, or secure storage.

Verify Docker persistence
use mongodb_course_lab

db.dockerCheck.find();
💡 After reconnecting to a newly created container that uses the same persistent volume, this query should return the previously inserted document. If the volume was removed, or the new container uses a different empty volume, the document will not be present.
Key Takeaways
  • Docker can run MongoDB in a reproducible container.
  • A named volume stores database files independently of the container.
  • Removing a container does not ordinarily remove its named volume.
  • Removing a volume deletes the data stored in it.
  • Local development credentials and configurations should not be copied into production.
Knowledge Check

You remove a MongoDB container but keep its named data volume. What should happen when a new compatible container is started using the same volume?