How to Run MongoDB in Docker- Step by Step Guide

Running MongoDB inside a Docker container allows you to quickly set up a MongoDB environment without installing it directly on your host machine. This method provides isolation and portability, making it easier to manage and deploy your database applications.

How to Run MongoDB in Docker- Step by Step Guide
How to Run MongoDB in Docker- Step by Step Guide

In this guide, we’ll walk you through the steps to set up and manage MongoDB within Docker. From pulling the latest MongoDB Docker image to setting up data persistence and security measures. You’ll learn how to connect to MongoDB, manage your container, and even use Docker Compose for more complex setups. By the end of this guide, you’ll have a flexible and isolated environment for your MongoDB applications, simplifying deployment and ensuring your host system remains clean and secure.

Prerequisites

  • Docker Installed: Ensure that Docker is installed on your system. You can download it from the official Docker website.

Steps to Run MongoDB in Docker

1. Pull the MongoDB Docker Image

First, download the latest MongoDB image from Docker Hub by running:

docker pull mongo

If you need a specific version, specify it like so:

docker pull mongo:5.0

2. Run a MongoDB Container

To start a MongoDB container, use the following command:

docker run --name mongodb -d mongo
  • --name mongodb: Assigns the name “mongodb” to your container.
  • -d: Runs the container in detached mode (in the background).

To map the default MongoDB port (27017) to your host machine, modify the command:

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

3. Persist Data with Volumes

By default, data stored inside the container is ephemeral. To persist data, mount a volume to the container:

docker run --name mongodb -d -p 27017:27017 -v /my/own/datadir:/data/db mongo

Replace /my/own/datadir with the path where you want MongoDB data to be stored on your host.

Alternatively, create a Docker volume:

docker volume create mongodb_data
docker run --name mongodb -d -p 27017:27017 -v mongodb_data:/data/db mongo

4. Set Up Authentication (Optional)

To enhance security, you can set up a username and password:

docker run --name mongodb -d -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=secret \
  mongo
  • -e: Sets environment variables inside the container.

5. Connect to MongoDB

From the Host Machine:

Ensure you have MongoDB client tools installed. Connect using:

mongo -u admin -p secret --authenticationDatabase admin

From Within the Container:

Use docker exec to run the Mongo shell inside the container:

docker exec -it mongodb mongo -u admin -p secret --authenticationDatabase admin

6. Manage the MongoDB Container

Stop the Container:

docker stop mongodb

Start the Container:

docker start mongodb

Remove the Container:

docker rm mongodb

7. Use Docker Compose (Optional)

For more complex setups or to manage multiple services, Docker Compose can be handy.

Create a docker-compose.yml file:

version: '3.8'

services:
  mongodb:
    image: mongo
    container_name: mongodb
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: secret
    volumes:
      - mongodb_data:/data/db

volumes:
  mongodb_data:

Run Docker Compose:

docker-compose up -d

Conclusion

By following these steps, you can efficiently run MongoDB in a Docker container, providing a flexible and isolated environment for your database applications. This approach simplifies deployment and scaling while keeping your host system clean. Always ensure that you manage your database security appropriately, especially when exposing ports or using default credentials.

Learn More: AI and Machine Learning

Automated Log Cleanup for GBase 8a: A Step-by-Step Guide

How to Set Up a Self-Hosted PostgreSQL Database with SSL Support

7 Best Open-Source AI Coding Tools Every Developer Should Know

Build Your Own AI-Powered Search Engine with SWIRL

Top 8 Open Source MLOps Tools for Production

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Comments