Running MongoDB in Docker is an excellent way to set up a database without installing it directly on your local machine. This guide will show you how to use Docker to quickly spin up a MongoDB instance using the latest mongo:8 image.
Prerequisites
Before you begin, make sure you have the following:
Docker installed on your system (Docker Desktop for Windows/Mac, or Docker CLI for Linux).
Basic understanding of how Docker works.
*Step 1: Pull the MongoDB Image
*
Docker Hub provides an official MongoDB image. To ensure you’re using the latest MongoDB 8 version, pull the mongo:8 image with this command:
docker pull mongo:8
This command fetches the latest MongoDB 8 image from Docker Hub and makes it available locally.
Step 2: Run MongoDB in a Container
Run a container with the mongo:8 image using the following command:
docker run -d \
--name mongodb-container \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=adminpassword \
mongo:8
Breaking Down the Command:
• docker run -d: Starts the container in detached mode.
• --name mongodb-container: Assigns a name to the container (you can choose any name).
• -p 27017:27017: Maps MongoDB’s default port 27017 to your local machine, making it accessible.
•-e: Sets environment variables to configure the database:
• MONGO_INITDB_ROOT_USERNAME: Sets the root username (e.g., admin).
• MONGO_INITDB_ROOT_PASSWORD: Sets the root password (e.g., adminpassword).
• mongo:8:
Specifies the image to use.
Step 3: Verify the Container is Running
Check if the container is running with the following command:
docker ps
You should see something like this:
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
abcdef123456 mongo:8 "docker-entry..." Up 5 minutes 0.0.0.0:27017->27017/tcp mongodb-container
This confirms MongoDB is running and accessible on
localhost:27017
Step 4: Connect to MongoDB
Using the Mongo Shell:
If you have the MongoDB client installed locally, connect to the running container with:
mongo --host localhost --port 27017 -u admin -p adminpassword
Using a GUI:
Tools like MongoDB Compass or Robo 3T make it easy to interact with your database visually. Just connect to localhost:27017 and use the credentials:
• Username: admin
• Password: adminpassword
Step 5: Persist Data with Volumes (Optional)
By default, Docker containers are ephemeral, meaning all data will be lost when the container is removed. To persist MongoDB data, map a local directory to the container’s data directory.
Run the container with a volume:
docker run -d \
--name mongodb-container \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=adminpassword \
-v /path/to/your/local/data:/data/db \
mongo:8
What Changed?
• -v /path/to/your/local/data:/data/db
: Maps a local directory (/path/to/your/local/data) to MongoDB’s data directory (/data/db), ensuring data persists even after the container is removed.
Step 6: Stop and Remove the Container
When you’re done, you can stop the MongoDB container:
docker stop mongodb-container
Conclusion
Using Docker to run MongoDB is a simple and efficient way to manage your database. It saves time, ensures consistency, and keeps your local machine clean from unnecessary installations. By following these steps, you can set up a MongoDB instance with Docker in just a few minutes
Top comments (0)