How to create a DB for MongoDB container on start up?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Overview
When deploying applications using Docker containers, initializing databases like MongoDB upon startup is a common requirement. This ensures that the database is ready, configured, and possibly pre-filled with essential data for application operation. This article will guide you through the steps necessary to create a MongoDB database automatically when the MongoDB container starts. We'll delve into using docker-compose, initialization scripts, and other methodologies for setting up the MongoDB database.
Prerequisites
Before you begin, ensure you have the following:
- Docker installed on your machine.
- Basic understanding of Docker and containerization.
- Basic knowledge of MongoDB.
Using Docker Compose
Docker Compose is a tool used for defining and running multi-container Docker applications. With Docker Compose, you can use a YAML file to configure your application's services. Use the following steps to automatically create a MongoDB database on startup:
Step 1: Create a docker-compose.yml File
Create a docker-compose.yml file to define the MongoDB service. Here's an example configuration:
Step 2: Initialize Scripts
Place any JavaScript or SQL files into the mongo-init-scripts directory. These scripts will run automatically when the container is initialized.
- The directory setup:
- Create a script file:Suppose you want to create a
userscollection with some initial data.File:mongo-init-scripts/init.js
Step 3: Run Docker Compose
Execute the following command in the terminal to start the compose services:
Explanation
- Environment Variables: The
MONGO_INITDB_ROOT_USERNAME,MONGO_INITDB_ROOT_PASSWORD, andMONGO_INITDB_DATABASEare used to set up the root user's credentials and the initial database the scripts will target. - Volume Mounting: The scripts are mounted to
/docker-entrypoint-initdb.d, which the MongoDB Docker image monitors for initialization scripts.
Summary Table
| Step | Description |
docker-compose.yml | Defines the MongoDB service with initial setup parameters. |
| Initialization Scripts | JavaScript/SQL files placed in mongo-init-scripts to initialize the database and collections. |
| Running Service | Execute docker-compose up to start the container and apply configurations. |
Additional Considerations
Authentication and Security
- Environment Variables: Avoid hardcoding passwords or sensitive information in the compose file. Use a
.envfile to store environment variables, which can be referenced in thedocker-compose.ymlfile. - Network Configuration: Ensure that the MongoDB instance is only accessible within the Docker network or from specified trusted sources.
Scaling and Management
- Container Naming and Networking: Use meaningful names for containers and networks. Consider network segmentation if deploying a multi-tier application.
- Data Persistence: Ensure data durability by connecting a persistent volume to the
/data/dbdirectory inside the container.
Automated Deployment
For automated deployment scenarios, integrate this setup with CI/CD pipelines to ensure seamless database deployment and setup in different environments including staging, testing, and production.
Conclusion
This guide provides a method for initializing a MongoDB database in a containerized environment using Docker Compose. Through the use of initialization scripts and proper configuration, automating the creation and setting up of databases becomes streamlined, reducing manual setup and enhancing reproducibility across different systems.
By utilizing Docker's capabilities and MongoDB's flexibility, you ensure that your applications have a consistent database environment irrespective of where they are deployed.
Related reading
- How to create a new docker image from a running container on Amazon?
- How to create a post-init container in Kubernetes?
- How to create named and latest tag in Docker?
- How to create new docker image based on existing image?
- How to create a LINQ to SQL Transaction?
- How to create a multi-tenant database with shared table structures?
- How to create postgis extension for postgresql in docker?
- How to create run .net Core Console App in docker

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.