How to Run an Init Script for MongoDB in Docker Container

MongoDB is a popular and powerful NoSQL database system used by developers and organizations worldwide. When working with MongoDB in a Docker container, you may encounter scenarios where you need to run initialization scripts to set up your database with initial data, users, or configurations. In this blog post, we’ll walk you through the process of running an init script for MongoDB in Docker container.

Before we begin, make sure you have the following prerequisites installed on your system:

  1. Docker: Ensure that Docker is installed and running on your system. You can download and install Docker from the official website here.
  2. A MongoDB Docker Image: You can use an official MongoDB image from Docker Hub. You can pull the image using the following command:
docker pull mongo

Creating an Initialization Script

To initialize your MongoDB container with custom configurations or data, you need to create an initialization script. This script can be written in JavaScript or any scripting language supported by MongoDB. Let’s create a simple JavaScript script as an example:

Create a file named init-mongo.js with the following content:

// Create a new database and switch to it
db = db.getSiblingDB('mydatabase');

// Create a new collection and insert documents
  { name: 'Document 1' },
  { name: 'Document 2' },
  { name: 'Document 3' }

// Create a user with read and write privileges for the database
  user: 'myuser',
  pwd: 'mypassword',
  roles: [
    { role: 'readWrite', db: 'mydatabase' }

Replace 'mydatabase', 'mycollection', 'myuser', and 'mypassword' with your preferred database, collection, username, and password.

Running the MongoDB Container with Initialization Script

Now that you have your initialization script ready, you can run the MongoDB container with the script as follows:

docker run -d \
  --name mongodb \
  -e MONGO_INITDB_DATABASE=mydatabase \
  -e MONGO_INITDB_ROOT_PASSWORD=rootpassword \
  -v /path/to/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro \

Let’s break down the command:

  • -d: Run the container in detached mode.
  • --name mongodb: Assign a name to your MongoDB container (you can use any name you prefer).
  • -e MONGO_INITDB_DATABASE=mydatabase: Set the name of the database you want to initialize.
  • -e MONGO_INITDB_ROOT_USERNAME=root and -e MONGO_INITDB_ROOT_PASSWORD=rootpassword: Set the root username and password for MongoDB.
  • -v /path/to/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro: Mount your initialization script into the container’s /docker-entrypoint-initdb.d/ directory as a read-only file.

This will ensure that your script is executed when the MongoDB container starts up, populating the database with your desired data and configurations.

Verifying Initialization

After the container is up and running, you can verify that your initialization script was executed successfully by connecting to the MongoDB container and checking the database and collections:

docker exec -it mongodb mongo -u root -p rootpassword --authenticationDatabase admin

# Switch to your database
use mydatabase

# Check the documents in your collection

# Verify user creation

That’s it! You’ve successfully run an initialization script for your MongoDB Docker container. You can adapt this process to suit your specific needs, whether it’s creating users, configuring replica sets, or populating your database with initial data. Docker makes it easy to automate these tasks and maintain consistency across different environments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment