8. Create a Docker Compose File for MongoDB
Goal
In this tutorial, you will learn how to:
- Create a docker-compose.yaml file to run MongoDB in a container.
- Add Mongo Express to visually manage the MongoDB database through a web interface.
- Configure persistent storage for MongoDB data.
- Connect your ASP.NET application to the MongoDB instance running in Docker.
- Verify that the application correctly interacts with MongoDB.
Step-by-step Guide
1. Create the Docker Compose File
We’ll use Docker Compose to define and manage multi-container applications. In this case, we’ll set up MongoDB along with Mongo Express, a web-based UI to manage MongoDB.
Steps:
- Navigate to the root of your project in the terminal.
- Create a new folder named
infra
to store infrastructure-related files - Create a new file named
mongodb_and_express.yaml
- Add the following content to the file:
infra/mongodb_and_express.yaml
services:
# MongoDB service
db:
image: mongo
restart: always
ports:
- "27017:27017"
volumes:
- mongodb-data:/data/db
# Mongo Express service
mongo-express:
image: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_SERVER: db
volumes:
mongodb-data:
Purpose:
- db Service: Runs the MongoDB database on port 27017 and stores data persistently in the mongodb-data volume.
- mongo-express Service: Provides a web-based UI accessible on port 8081 to manage your MongoDB instance.
- Volumes: Ensures MongoDB data persists across container restarts.
2. Start the MongoDB and Mongo Express Containers
Now that the Docker Compose file is ready, we’ll spin up the containers. Make sure Docker Desktop is running.
Steps:
In your terminal, navigate to the project root directory (if you’re not already there).
Run the following command to start the MongoDB and Mongo Express services in detached mode (-d):
docker compose -f ./infra/mongodb_and_express.yaml up -d
Open your browser and navigate to http://localhost:8081 to access the Mongo Express interface. You should see your MongoDB instance running.
- The credentials are:
admin:pass
- The credentials are:
Purpose:
This step runs MongoDB in the background and provides a UI to easily view and manage your data.
3. Clean Up
If you want to stop the MongoDB containers, use the following command:
docker compose -f ./infra/mongodb_and_express.yaml down
This will stop and remove the containers but keep the data in the mongodb-data volume. To remove the volume as well, run:
docker compose -f ./infra/mongodb_and_express.yaml down -v
Summary
In this tutorial, we:
- Created a Docker Compose file to run MongoDB and Mongo Express.
- Configured persistent storage for MongoDB data.
- Updated the ASP.NET application to connect to MongoDB running in Docker.
- Verified that the application can successfully add subscribers to the MongoDB database.
- Used Mongo Express to visually manage and inspect MongoDB data.