Open In App

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.



Why Seed a MongoDB Database?

Seeding a MongoDB database using Docker Compose offers several advantages:

Using Docker Compose for Seeding MongoDB

Docker Compose is a tool for defining and running multi-container Docker applications. It uses YAML files to configure the services, networks, and volumes required for your application. By leveraging Docker Compose, we can easily set up a MongoDB container and seed it with initial data using a custom initialization script.



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:

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:

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

Conclusion

Seeding a MongoDB database using Docker Compose is a straightforward process that offers numerous benefits for development and testing workflows. By leveraging Docker Compose, you can easily set up a MongoDB container and populate it with initial data using a custom initialization script.

This ensures consistent data across environments and enables developers and testers to work with realistic datasets during application development and testing. Experiment with the provided examples and explore additional customization options to suit your specific requirements.

Article Tags :