1. Introduction

Seeding a MongoDB database is an essential step in both development and testing environments. This ensures that an application starts with a reliable set of data. So, whether launching a new project or configuring a development setup, automating this process can save time and prevent inconsistencies.

On the other hand, Docker Compose offers a streamlined solution for managing multi-container applications. Therefore, it’s an ideal tool for seeding MongoDB databases in a containerized setup.

In this tutorial, we’ll explore the process of seeding a MongoDB database using Docker Compose. At the end of this tutorial, we’ll have the skills to efficiently apply this knowledge to populate the database in real-world scenarios.

2. Understanding the Basics

Before we dive into the implementation, let’s clarify what it means to seed a MongoDB database using Docker Compose conceptually.

Seeding is the process of populating a database with initial data when it’s first set up. In this case, we’ll seed a MongoDB database, which is a popular NoSQL database known for its flexibility and scalability.

Docker Compose is a tool for defining and running multi-container Docker applications. It enables us to manage multiple services, such as databases and web applications, in a single configuration file.

3. Setup for Using Docker Compose to Seed MongoDB Database

Now, let’s learn the procedures for setting up Docker Compose to seed a MongoDB database. With proper configuration, our database will be seeded with the necessary data every time we start our environment.

3.1. Creating the Docker Compose File

We begin by creating a project folder named test in our Documents directory. Next, we create and configure a docker-compose.yml file in our project directory. Specifically, we’ll define the MongoDB service and configuration for the container inside the YAML file:

$ cat docker-compose.yml
services:
 mongodb:
  image: mongo
  ports:
   - '27017:27017'
  volumes:
   - ./data:/data/db
   - ./mongo-seed:/docker-entrypoint-initdb.d

As shown above, we define a MongoDB service named mongodb based on the official mongo Docker image. Also, we map port 27017 on our host machine to port 27017 inside the container, allowing access to MongoDB. Importantly, the ./data folder would be created when docker-compose is executed.

In addition, we mount the ./mongo-seed directory from the host to /docker-entrypoint-initdb.d inside the container. MongoDB will automatically execute any initialization scripts found in this directory when the container is first started.

3.2. Creating the Seed Script

Next, let’s create a directory named mongo-seed in the same location as our docker-compose.yml file. Then, inside the directory, we’ll create a JavaScript file named seed.js that contains our seed data.

To demonstrate, let’s write the seed.js file to set up a database that holds student information:

// seed.js
db = db.getSiblingDB("school");
db.students.insertMany([
  { name: "Gbenga Oyatoye", age: 22, major: "Computer Science" },
  { name: "John Doe", age: 24, major: "Mathematics" },
  { name: "Jimmy Azar", age: 28, major: "Physics" }
]);

The line db = db.getSiblingDB(“school”); in the seed script prompts MongoDB to switch to the school database if it exists. Otherwise, it’ll create the database automatically.

Then, the db.students.insertMany() command will create the students collection within the school database and insert the specified dataset.

Thus, when we run the Docker Compose setup, it automatically executes the seed.js seed script.

4. Running Docker Compose

With the setup complete, we can launch the MongoDB container using Docker Compose. To illustrate this, let’s navigate to the directory containing the docker-compose.yml file and execute the docker-compose command:

$ sudo docker-compose up -d
[+] Running 9/9
 ✔ mongodb Pulled                                    381.7s 
... 
   ✔ 403f753f5920 Pull complete                      372.9s 
   ✔ 88cd53ea307c Pull complete                      373.0s 
[+] Running 1/1
 ✔ Network test_default       Created                   0.1s
 ✔ Container test-mongodb-1   Started                   0.5s

The command starts the MongoDB service, pulls all the necessary objects, mounts the ./data and ./mongo-seed directories, and executes the seed.js script to seed the database.

5. Verification of the Seeded Data

Now, let’s verify the seeded data in MongoDB initiated by docker-compose.yml.

5.1. Installing the MongoDB Client Manually

For verification, we’ll install the MongoDB client on our local host to connect to our container running MongoDB. To begin with, we’ll download the MongoDB public key. The key is used to verify the authenticity of MongoDB packages:

$ curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-6.0.gpg

The command above downloads the public key in a suitable format for use by the package manager and then saves it to /usr/share/keyrings/mongodb-server-6.0.gpg.

Then, we can add the MongoDB 6.0 repository to the list of APT sources. This enables us to install MongoDB using the apt package manager. Here’s the break-down of how to do it:

$ echo "deb [ arch=amd64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

This directs our system to where it can find the MongoDB packages, making it possible to install MongoDB 6.0 and keep it updated using the apt command.

Finally, we can install the MongoDB client using the apt-get command:

$ sudo apt-get install -y mongodb-mongosh
Reading package lists... Done
...
The following NEW packages will be installed:
  mongodb-mongosh
...
Unpacking mongodb-mongosh (2.2.15) ...
Setting up mongodb-mongosh (2.2.15) ...
Processing triggers for man-db (2.12.0-4build2) ...

As shown above, the sudo apt-get install command was used to download the mongodb-mongosh, and the installation was successful.

5.2. Verifying MongoDB Docker Container Is Up

Let’s first ensure our container is up and running. We can use the sudo docker ps command for this verification:

$ sudo docker ps
[sudo] password for gbenga: 
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                                           NAMES
a097e3211000   mongo     "docker-entrypoint.s…"   2 hours ago   Up 2 hours   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   test-mongodb-1

From the output, we can see that the container is up and running. The sudo command grants root privileges, while docker ps lists the current containers that are up and running.

5.3. Connecting the MongoDB Client to the Container

Now that we have everything required to verify the seeded dataset in our MongoDB container, let’s connect the MongoDB client, mongosh, to the container:

$ sudo mongosh --host localhost --port 27017
Current Mongosh Log ID:    66b8d82850d340596e838725
...
Using Mongosh:        2.2.15
...
2024-08-11T12:50:24.729+00:00: vm.max_map_count is too low
------

test>

The output shows that we’ve successfully connected to the MongoDB container from our host using mongosh. Finally, we can proceed to query for the seeded data.

5.4. Querying the Seeded Data

Now that we’re connected to the database, let’s query for the dataset in the database. First, we check the list of databases and the active database we’re in:

test> show dbs
admin   40.00 KiB
config  12.00 KiB
local   72.00 KiB
school  40.00 KiB
test> db
test
test> use school
switched to db school
school>

The show dbs command shows the list of databases, while the db command displays the name of the active database, which corresponds to the name of our project folder, test. To switch to the school database, we then run the use school query.

*The syntax for checking the seeded data is db.myCollection.find().pretty(), but we need to substitute myCollection with the collection name in the current database*. Let’s obtain this name for our current database:

school> db.getCollectionNames()
[ 'students' ]

Here, we use the db.getCollectionNames() query to obtain the collection name, which is students. Now, let’s extract the seeded data to confirm whether it matches the expected data derived from the container initialization:

school> db.students.find().pretty()
[
  {
    _id: ObjectId('66b9defc7373a94e19149f48'),
    name: 'Gbenga Oyatoye',
    age: 22,
    major: 'Computer Science'
  },
  {
    _id: ObjectId('66b9defc7373a94e19149f49'),
    name: 'John Doe',
    age: 24,
    major: 'Mathematics'
  },
  {
    _id: ObjectId('66b9defc7373a94e19149f4a'),
    name: 'Jimmy Azar',
    age: 28,
    major: 'Physics'
  }
]
school>

The query displays the seeded data, which is identical to the data we configured earlier.

6. Conclusion

In this article, we’ve successfully streamlined the process of seeding a MongoDB database using Docker Compose. Whether initializing a fresh environment or resetting a database to a known state, the techniques covered here provide a robust foundation.

With the steps outlined, we can confidently apply this knowledge to real-world scenarios, enhancing both the reliability and efficiency of our database workflows.