How to Seed a MongoDB Database Using Docker Compose
1. Create a Docker Compose File
Create a docker-compose.yml file in your project directory and define the MongoDB service along with its configuration. Additionally, specify a volume for persisting MongoDB data and mount a directory containing the seed data initialization script.
version: '3.8'
services:
mongodb:
image: mongo
ports:
- '27017:27017'
volumes:
- ./data:/data/db
- ./scripts:/docker-entrypoint-initdb.d
In this configuration:
- We define a MongoDB service named mongodb based on the official mongo Docker image.
- The MONGO_INITDB_DATABASE environment variable specifies the name of the database to create upon initialization.
- We map port 27017 on the host machine to the MongoDB container.
- We mount the mongo-seed directory from the host to /docker-entrypoint-initdb.d inside the container, where MongoDB looks for initialization scripts.
2. Create Seed Data Initialization Script
Create a JavaScript file named seed.js in the scripts directory. This script will be executed when the MongoDB container starts and will populate the database with seed data. Here’s an example script to insert sample documents into a users collection:
// seed.js
db.users.insertMany([
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 35 },
{ name: 'Charlie', age: 25 }
]);
This script uses the insertMany method to seed the users collection with sample documents.
3. Start Docker Compose
Navigate to your project directory containing the docker-compose.yml file and run the following command to start Docker Compose:
docker-compose up -d
This command will create and start the MongoDB container in the background, mount the data and scripts directories, and execute the seed.js script to seed the database.
Docker Compose will:
- Download the MongoDB image if not already available.
- Create and start a container named my-mongodb based on the specified configuration.
- Mount the mongo-seed directory into the MongoDB container.
- Execute the seed.js script during MongoDB initialization.
4. Verify Seeding
Once the Docker Compose services are up and running, you can verify that the seeding process was successful by connecting to the MongoDB database and checking the seeded data.
Connect to MongoDB Container
Open a new terminal window and run the following command to connect to the MongoDB container:
docker exec -it my-mongodb mongo mydatabase
Verify Seeded Data
Once connected to the MongoDB shell, run the following commands to verify the seeded data
> use mydatabase
> db.users.find().pretty()
This will display the seeded users collection with the inserted documents
{ "_id" : ObjectId("60e26f48e5d856002e54713d"), "name" : "Alice", "email" : "alice@example.com" }
{ "_id" : ObjectId("60e26f48e5d856002e54713e"), "name" : "Bob", "email" : "bob@example.com" }
{ "_id" : ObjectId("60e26f48e5d856002e54713f"), "name" : "Charlie", "email" : "charlie@example.com" }
5. Stop and Remove Docker Containers
To stop the Docker Compose services and remove the containers, press Ctrl + C in the terminal where docker-compose up is running. Then, run the following command to clean up:
docker-compose down
How to Seed a MongoDB Database Using Docker Compose
Seeding a MongoDB database is a common task in many development and testing scenarios. It involves populating the database with initial data to ensure consistent and predictable behavior during the application development and testing phases. Docker Compose is a powerful tool that simplifies the process of managing multi-container Docker applications, making it an excellent choice for seeding MongoDB databases in a containerized environment.
In this article, we’ll explore how to seed a MongoDB database using Docker Compose, covering all the necessary concepts with beginner-friendly examples and outputs.