How to create a DB for MongoDB container on start up?

Cover Image for How to create a DB for MongoDB container on start up?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 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 a mongo-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 prefix MONGO_INITDB_. In our example, we set the database name to mydb, the admin username to admin, and the password to password.

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!



More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello