How to create a DB for MongoDB container on start up?
📝 Creating a MongoDB Container with a Default Database on Start-up
Are you working with Docker and trying to add MongoDB to your stack? You might have noticed that the official MongoDB Docker image doesn't provide a straightforward way to set up a default database, an admin user with a password, and even an authentication method. Don't worry, though! We've got you covered with an easy solution!
The MySQL Equivalent
Before we delve into MongoDB, let's take a quick look at how we can achieve a similar setup with MySQL. In MySQL, you can utilize environment variables defined in a .env
file to configure your database. Here's an example:
db:
image: mysql:5.7
env_file: .env
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
With these configurations, you can effortlessly set the database name, admin user, and password. Easy peasy, right? Now, let's dive into MongoDB and explore a similar approach.
Setting Up a MongoDB Container on Start-up
To achieve the same level of convenience with MongoDB, we'll make use of the docker-compose.yml
file to define our desired configurations. Let's take a look at an example:
version: '3'
services:
mongo:
image: mongo:latest
ports:
- 27017:27017
volumes:
- ./data:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js
environment:
- MONGO_INITDB_DATABASE=mydb
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
Let's break it down step-by-step:
image: mongo:latest
sets the MongoDB Docker image you want to use.ports: - 27017:27017
exposes the standard MongoDB port.volumes
is optional but recommended. It helps you store your database files persistently on your host machine, which means your data won't be lost when the container restarts.volumes
links a directory (./data
) on your host machine to the container's/data/db
directory, where MongoDB stores its data. This ensures that your data is preserved even if the container is removed.volumes
also includes amongo-init.js
file in the/docker-entrypoint-initdb.d/
directory. It's a convenience feature that allows you to initialize your database with custom scripts as soon as MongoDB starts up.environment
variables set up the initial database, admin username, and password using the prefixMONGO_INITDB_
. In our example, we set the database name tomydb
, the admin username toadmin
, and the password topassword
.
Final Thoughts
With these configurations in your docker-compose.yml
file, your MongoDB container will start up with a default database, an admin user, and a password. Feel free to modify the example to match your specific requirements. Additionally, the use of volumes will help preserve your data even if the container is recreated.
We hope this guide has helped you overcome the hurdle of setting up a MongoDB container with the necessary configurations. If you have any other questions or need further assistance, don't hesitate to reach out! Happy coding! 😊
Have you successfully set up a MongoDB container with a default database? Let us know in the comments below!