Прескочи към съдържанието

MongoDB on Docker

Published: at 03:45 PMSuggest Changes

MongoDB on Docker

This guide will walk you through setting up MongoDB in a Docker container with persistent data storage using Docker volumes. We’ll create a dedicated directory structure and configure port mapping for easy access.

Table of Contents

Open Table of Contents

Prerequisites

Before starting, ensure you have:

Directory Setup

First, let’s create a dedicated directory structure for our MongoDB instance:

# Create parent directory
mkdir ~/mongodb-docker

# Create data directory for persistence
mkdir ~/mongodb-docker/data

# Navigate to the MongoDB directory
cd ~/mongodb-docker

Docker Configuration

Create a docker-compose.yml file in the mongodb-docker directory:

version: '3.8'

services:
  mongodb:
    image: mongo:latest
    container_name: mongodb
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password123
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db
    restart: unless-stopped
volumes:
  mongo-data:
    driver: local

Let’s break down the configuration:

Running MongoDB

Start the MongoDB container using Docker Compose:

docker-compose up -d

The -d flag runs the container in detached mode (background).

Verifying the Setup

  1. Check if the container is running:
docker ps
  1. Verify the volume mounting:
docker volume ls
  1. Connect to MongoDB using the MongoDB shell:
docker exec -it mongodb mongosh -u admin -p password123

You should now see the MongoDB shell prompt. Try some basic commands:

// Show databases
show dbs

// Create a test database
use test_db

// Insert a test document
db.test_db.insertOne({ message: "Hello Docker MongoDB!" })

// Query the document
db.test.find()

Directory Structure

After setup, your directory structure should look like this:

~/mongodb-docker/
├── docker-compose.yml
└── data/
    └── ... (MongoDB data files)

Important Notes

  1. Data Persistence: All your MongoDB data will be stored in the ~/mongodb-docker/data directory. This ensures your data survives container restarts or removals.

  2. Security:

    • Change the default username and password in production
    • Consider adding network restrictions
    • Never expose MongoDB directly to the internet
  3. Backup: You can backup your data by copying the contents of the data directory. Just ensure MongoDB is stopped first:

docker-compose down
# Now you can safely backup the data directory
docker-compose up -d

Now you have a fully functional MongoDB instance running in Docker with persistent storage! The database will be accessible on localhost:27017 using the credentials specified in the docker-compose file.


Previous Post
Second Exercise
Next Post
Environment Setup