Table of Contents
Docker is a popular containerization tool that simplifies managing applications and their dependencies. MongoDB is a widely used NoSQL database known for its flexibility and scalability. Combining Docker and MongoDB allows developers to set up and manage MongoDB instances in isolated environments quickly.
This guide will walk you through creating a MongoDB Docker container, covering everything from pulling the MongoDB image to running the container and connecting to it. We’ll also provide practical examples to help you understand each step.
Prerequisites
Before we begin, ensure you have the following installed on your system:
- Docker is installed and running on your server
- Basic knowledge of Docker commands
Step 1 – Pulling the MongoDB Docker Image
The first step is to pull the official MongoDB image from Docker Hub. Open your terminal and run the following command:
docker pull mongo:4.4
This command downloads the latest MongoDB image. You can verify the MongoDB docker image using the following command.
docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo 4.4 d896c071ac69 4 months ago 427MB
Step 2 – Running a MongoDB Container
Once the image is downloaded, you can create and run a MongoDB container. Use the following command:
docker run --name mongodb -d mongo:4.4
Here’s a breakdown of the command:
- docker run: Command to create and start a container.
- –name mongodb: Names the container “MongoDB”.
- -d: Runs the container in detached mode.
- mongo:4.4: Specifies the image to use.
You can verify that the container is running by using:
docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3dc272f119f5 mongo:4.4 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 27017/tcp mongodb
Step 3 – Connecting to the MongoDB Container
You need to use a MongoDB client to connect to the MongoDB instance running in your Docker container. The MongoDB CLI client is included in the container, so you can connect using the following command:
docker exec -it mongodb /bin/bash
This command opens an interactive MongoDB shell inside the running container.
root@3dc272f119f5:/#
Run the following command to exit from the Mongodb container.
exit
Step 4 – Persisting Data with Volumes
By default, data stored in a Docker container is ephemeral. To persist data, you need to use Docker volumes. Create a directory on your host machine to store MongoDB data, then run the container with a volume:
mkdir -p ~/mongo-data
docker run --name mongodb-volume -v ~/mongo-data:/data/db -d mongo:4.4
This command mounts the host directory ~/mongo-data to the container’s /data/db directory, ensuring data persistence.
Step 5 – Configuring MongoDB with Environment Variables
You can configure MongoDB using environment variables. For example, to set a root username and password, use the following command:
docker run --name mongodb-env -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret -d mongo:4.4
This command sets the root username to admin and the password to secret.
Step 6 – Managing MongoDB Containers
You can start, stop, and remove MongoDB containers using Docker commands:
Stop the container:
docker stop mongodb
Start the container:
docker start mongodb
Remove the container:
docker rm mongodb
Step 7 – Practical Example
Let’s go through a practical example where we create a MongoDB container with data persistence and authentication. Before getting started, all existing MongoDB containers must be deleted.
1. Create a directory for MongoDB data and give proper permissions:
mkdir -p ~/mongodb-data
chmod -R 777 ~/mongodb-data
2. Run the MongoDB container with volume and authentication:
docker run --name mongodb-data -v ~/mongodb-data:/data/db -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret -d mongo:4.4
3. Connect to the MongoDB container:
docker exec -it mongodb-data mongo -u admin -p secret --authenticationDatabase admin
This command connects to the MongoDB instance using the specified username and password.
MongoDB shell version v4.4.29
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b59b44c8-a977-4770-9b1e-0abfd0ce5c16") }
MongoDB server version: 4.4.29
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2024-07-15T15:26:54.439+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
---
>
4. Create a new database and collection:
use mydatabase
db.createCollection("mycollection")
5. Insert a document:
db.mycollection.insert({ name: "example", type: "demo" })
6. Exit from the container:
exit
7. Stop and remove the container:
docker stop mongodb-data
docker rm mongodb-data
8. Run the container again:
docker run --name mongodb-data -v ~/mongodb-data:/data/db -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret -d mongo:4.4
9. Connect and verify the data:
docker exec -it mongodb-data mongo -u admin -p secret --authenticationDatabase admin
use mydatabase
db.mycollection.find()
You should see the document you inserted earlier, confirming that data persistence is working.
> db.mycollection.find()
{ "_id" : ObjectId("6695471d6ae167ad03dcadfd"), "name" : "example", "type" : "demo" }
Conclusion
Creating a MongoDB Docker container is a straightforward process that provides a flexible and isolated environment for your database. By following this guide, you can set up a MongoDB container with data persistence and authentication, ensuring your data is secure and persistent across container restarts. You can now create a MongoDB docker container on dedicated server hosting from Atlantic.Net!