Getting Started with MongoDB on Docker: A Step-by-Step Guide

Are you eager to dive into MongoDB but unsure where to start? Docker provides a convenient way to set up MongoDB for local development, allowing you to experiment without the complexity of traditional installations.

In this guide, we’ll walk you through the process of installing MongoDB on Docker, persisting database data files, connecting to MongoDB, and even running MongoDB Server via Docker Compose.

Installing MongoDB on Docker

First things first, make sure Docker is installed on your system. Docker simplifies the process of setting up MongoDB by encapsulating it within a container. We won’t delve into Docker installation instructions here, as Docker’s cross-platform compatibility means the setup process can vary depending on your operating system.

To start MongoDB as a basic container, simply run the following command:

docker run --name mongodb-server -d -p 27017:27017 mongo:7.0

This command initializes a container named “mongodb-server” running MongoDB Server version 7.0. The -d flag ensures the container runs in the background.

By default, MongoDB runs on port 27017, which we expose using the -p flag.

Persisting Database Data Files

One drawback of running MongoDB within a container is that data files are confined to the container’s filesystem. To ensure data persistence across container instances, you can map the data directory to a location on your local machine using the -v flag:

docker run --name mongodb-server -d -p 27017:27017 -v LOCAL_DIR:/data/db mongo:7.0

Replace LOCAL_DIR with the desired path on your system. This allows MongoDB’s data files to be stored locally, safeguarding them from container deletion.

Connecting to MongoDB on Docker

Once MongoDB is up and running, you can connect to it using various methods. One approach is to utilize the MongoDB Shell (mongosh), which is included by default in the MongoDB Docker image.

To connect via MongoDB Shell, execute the following command:

docker exec -it [[YOUR CONTAINER ID]] mongosh

Replace [[YOUR CONTAINER ID]] with the actual container ID obtained when running the MongoDB container. This command launches the MongoDB Shell within the container, enabling you to interact with the database.

Running MongoDB Server via Docker Compose

For more complex setups or when managing multiple containers, Docker Compose offers a streamlined solution. Below is a sample docker-compose.yaml file for running MongoDB with Docker Compose:

version: '3'
services:
  mongodb:
    image: mongo:7.0
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=password123
    ports:
      - 27017:27017
    volumes:
      - ./data/db:/data/db

This configuration defines a MongoDB service with version 7.0, sets up initial credentials, exposes port 27017, and mounts the data directory to ensure data persistence.

To launch MongoDB with Docker Compose, navigate to the directory containing the docker-compose.yaml file and run:

docker-compose up -d

This command initializes the MongoDB container in the background, using the specified configuration!

###Conclusion

In this guide, we’ve covered the essentials of setting up MongoDB on Docker for local development. Whether you prefer running MongoDB as a standalone container or managing it with Docker Compose, Docker provides a flexible and efficient environment for experimenting with MongoDB.

With these steps, you’re well-equipped to begin your MongoDB journey and explore its myriad capabilities within a Dockerized environment. Happy coding!

MongodbDockerDatabases
Avatar for Justin Jenkins

Written by Justin Jenkins

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.