MongoDB, a NoSQL database, has gained widespread popularity due to its flexibility, scalability, and document-oriented data model. Docker, a containerization platform, enables developers to package and deploy applications in isolated environments. By combining MongoDB with Docker, you can build and run database applications efficiently and consistently across different environments.
Prerequisites
Before starting this tutorial, ensure you have the following prerequisites:
- Docker Engine: Download and install Docker Engine on your system. You can find installation guides for various operating systems on the official Docker website https://docs.docker.com/get-docker:
- MongoDB Image: Pull the official MongoDB Docker image from Docker Hub using the following command:
docker pull mongo:latest
This will download the latest MongoDB image from Docker Hub .
Setting up a MongoDB Container with Docker
Run the MongoDB Container:Â Run the MongoDB container using the following command:
docker run -p 27017:27017 mongo:latest
This command will start a MongoDB container and map the host machine’s port 27017 to the container’s port 27017, allowing you to connect to the MongoDB instance from the host machine.
Setting up a MongoDB Container with Docker Compose
- Creating a Docker Compose File: Create a file named
docker-compose.yml
in the desired directory. This file will define the services and configurations for your MongoDB application. - Defining Services: Add the following lines to the
docker-compose.yml
file to define the MongoDB service: - Creating a Data Volume:Â Create a volume namedÂ
mongodb-data
 to persist MongoDB data across container restarts:
version: "3.9"
services:
mongodb:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- mongodb-data:/data/db
- Building and Running the Containers:Â Build and run the containers using the following command:
docker-compose up -d
This command will build the MongoDB image from the Dockerfile if necessary, run the MongoDB container in the background, and map the host machine’s port 27017 to the container’s port 27017, allowing you to connect to the MongoDB instance from the host machine.
- Verifying MongoDB Connection:Â Open a new terminal window and use the following command to verify that the MongoDB instance is running:
docker logs mongodb
This should display the MongoDB logs, indicating that the server has started successfully.
Connecting to MongoDB
- Install MongoDB Compass: Download and install MongoDB Compass, a graphical user interface (GUI) tool for managing MongoDB instances. You can download Compass from the official MongoDB website https://www.mongodb.com/download-center/compass.
- Connect to MongoDB Instance: Launch MongoDB Compass and connect to the MongoDB instance running on the Docker container using the following parameters:
- Hostname:
localhost
- Port:
27017
- Hostname:
- Create a Database: Create a database in MongoDB Compass to store your data. For example, you can create a database named
mydb
.
Working with MongoDB in Docker
- Creating Collections: Create collections within the
mydb
database to categorize your data. For instance, you can create a collection namedusers
to store user information. - Inserting Data: Insert documents into the collections to store data. For example, you can insert documents into the
users
collection to represent individual users. - Querying Data: Query the collections to retrieve specific data. For example, you can query the
users
collection to find users based on their names, email addresses, or other attributes.
Conclusion
This tutorial has shown you how to use MongoDB with Docker. By leveraging Docker, you can easily manage and deploy MongoDB instances, allowing you to focus on developing your applications without worrying about the underlying infrastructure.
Here are some additional resources for learning more about MongoDB and Docker:
- MongoDB Documentation: https://www.mongodb.com/docs/
- Docker Documentation: https://docs.docker.com/
- MongoDB with Docker Tutorial: https://www.digitalocean.com/community/tutorial-collections/how-to-install-mongodb
- MongoDB with Docker Compose Tutorial: https://linuxhint.com/how-to-run-a-mongodb-server-with-docker-compose/
I hope this tutorial has been helpful. Please let me know if you have any questions.