How to Configure Persistent Storage for MongoDB Docker Clusters Using Named Volumes

The minhhungit/mongodb-cluster-docker-compose repository implements persistent storage by mounting named Docker volumes to /data/db and /data/configdb for every MongoDB service, ensuring data survives container restarts and recreations.

Managing persistent storage in containerized MongoDB clusters requires careful volume configuration to prevent data loss during deployments. The minhhungit/mongodb-cluster-docker-compose repository demonstrates a production-ready approach to persistent storage using Docker named volumes across router, config server, and shard nodes. This implementation ensures that your MongoDB data, replica sets, and sharding metadata remain intact even when containers are stopped, removed, or updated.

Understanding Named Docker Volumes for MongoDB Data Persistence

Named volumes provide the most robust persistent storage mechanism for stateful containers like MongoDB. Unlike bind mounts that depend on host directory structures, named volumes are managed entirely by Docker's storage driver, making them portable across different host systems and immune to host filesystem differences.

Volume Mount Points and Container Paths

In this MongoDB cluster implementation, every service mounts two distinct persistent storage locations:

  • /data/db: Stores the actual MongoDB database files, collections, and indexes
  • /data/configdb: Stores MongoDB configuration files and metadata

These paths follow the official MongoDB Docker image conventions, ensuring compatibility with MongoDB's expected directory structure.

Persistent Storage Configuration in docker-compose.yml

The repository's docker-compose.yml file defines persistent storage through explicit volume declarations for each MongoDB component. This declarative approach ensures that Docker creates and manages the volumes automatically when you first start the cluster.

Router Service Volumes

The router service (router01) mounts dedicated volumes for both data and configuration persistence. According to the source code in docker-compose.yml (lines 9-13):

router01:
  image: mongo:latest
  container_name: router01
  volumes:
    - ./scripts:/scripts
    - mongodb_cluster_router01_db:/data/db
    - mongodb_cluster_router01_config:/data/configdb

The named volumes mongodb_cluster_router01_db and mongodb_cluster_router01_config ensure that routing metadata and configuration persist across container lifecycle events.

Config Server Volumes

Config servers store the cluster's metadata and sharding configuration, making persistent storage critical for cluster integrity. The repository implements three config servers (configsvr01, configsvr02, configsvr03), each with dedicated volumes following the naming pattern mongodb_cluster_configsvrXX_db and mongodb_cluster_configsvrXX_config.

Shard Node Volumes

Each shard node in the cluster maintains its own persistent storage volumes. The repository supports multiple shards (e.g., shard01-a, shard01-b, shard02-a), with volumes named according to the pattern mongodb_cluster_shard0X_Y_db and mongodb_cluster_shard0X_Y_config. This isolation prevents data conflicts between different shards while ensuring each replica set member maintains its data persistence.

Volume Definitions and Docker Management

All named volumes are explicitly declared in the top-level volumes section of docker-compose.yml (lines 37-63):

volumes:
  mongodb_cluster_router01_db:
  mongodb_cluster_router01_config:
  mongodb_cluster_configsvr01_db:
  mongodb_cluster_configsvr01_config:
  mongodb_cluster_configsvr02_db:
  mongodb_cluster_configsvr02_config:
  mongodb_cluster_configsvr03_db:
  mongodb_cluster_configsvr03_config:
  # ... additional shard volumes

Docker automatically creates these volumes on first use if they do not exist, storing them in Docker's managed storage area (typically /var/lib/docker/volumes/ on Linux hosts). Because these are named volumes rather than anonymous volumes, they persist even when you run docker compose down, ensuring your MongoDB data remains intact between deployments.

Managing Persistent Storage Operations

Understanding how to interact with these volumes is essential for backup, migration, and troubleshooting tasks.

Starting the Cluster with Persistent Storage

When you first start the cluster, Docker automatically creates the named volumes and mounts them to the appropriate containers:

docker compose up -d

MongoDB initializes its data files in /data/db within each container, which are actually stored in the Docker-managed volume on the host.

Inspecting Volume Contents

To verify where Docker stores your MongoDB data on the host filesystem:


# Retrieve the host path for a specific volume

docker volume inspect mongodb_cluster_router01_db -f '{{ .Mountpoint }}'

# List the MongoDB data files

ls $(docker volume inspect mongodb_cluster_router01_db -f '{{ .Mountpoint }}')

This inspection capability is crucial for performing host-level backups or monitoring disk usage.

Safely Removing Persistent Data

To completely remove the persistent storage and start fresh:


# Stop and remove containers (preserves volumes)

docker compose down

# Remove specific named volumes

docker volume rm mongodb_cluster_router01_db
docker volume rm mongodb_cluster_router01_config

Warning: Removing volumes destroys all MongoDB data permanently. Always ensure you have backups before executing these commands.

Summary

  • Named Docker volumes provide the persistent storage mechanism for all MongoDB services in the minhhungit/mongodb-cluster-docker-compose repository.
  • Each service mounts two volumes: one for database files at /data/db and one for configuration at /data/configdb.
  • Volume naming follows the pattern mongodb_cluster_{service}_db and mongodb_cluster_{service}_config, ensuring isolation between router, config servers, and shard nodes.
  • The docker-compose.yml file explicitly declares these volumes (lines 37-63) and mounts them in each service definition (e.g., lines 9-13 for the router).
  • Data persists across docker compose down operations because named volumes are not removed unless explicitly deleted with docker volume rm.

Frequently Asked Questions

How does persistent storage survive container restarts in this MongoDB cluster?

The repository uses named Docker volumes that exist independently of container lifecycles. When a container restarts, Docker reconnects the same volume to the new container instance, preserving all data in /data/db and /data/configdb. This ensures MongoDB replica sets and sharding metadata remain intact even after docker compose down and docker compose up cycles.

What is the difference between the /data/db and /data/configdb volumes?

Each MongoDB service mounts two distinct persistent storage locations. The /data/db volume stores the actual database files, collections, indexes, and oplog data required for MongoDB operations. The /data/configdb volume stores MongoDB configuration files and metadata specific to the container's role in the cluster. This separation allows for granular backup strategies and configuration management.

Where are the MongoDB data files physically stored on the host machine?

Docker stores named volumes in its managed storage area, typically /var/lib/docker/volumes/ on Linux hosts. Each volume appears as a directory named after the volume (e.g., mongodb_cluster_router01_db). You can locate the exact path using docker volume inspect <volume_name> -f '{{ .Mountpoint }}'. This host-level access enables direct backups without entering containers.

Can I change the volume configuration without losing existing MongoDB data?

Modifying volume names or mount points in docker-compose.yml requires careful migration to prevent data loss. If you change a volume name, Docker treats it as a new volume and creates empty storage. To migrate, copy data from the old volume to the new one using a temporary container with both volumes mounted. Always back up your data using docker cp or volume inspection methods before modifying storage configurations.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →