composebuilder

Docker Compose: MongoDB 7 + Mongo Express Admin UI

Docker Compose stack for MongoDB 7 with root authentication, a healthcheck and a named volume, plus Mongo Express 1.0.2 on localhost with its own web login.

When to use this stack

MongoDB stores documents in flexible JSON-like structures, which suits applications whose data shapes change often, such as catalogs, event logs or content with nested fields. This stack runs MongoDB 7 with root authentication, a healthcheck that uses mongosh, and a named volume for the data. It adds Mongo Express 1.0.2, a web interface for browsing collections, editing documents and running simple queries. The stack is useful for a development database, a small internal tool or a single-server application that needs a visual admin screen.

Use this stack when you want MongoDB running quickly with clear credentials and an admin UI, or when a service on the same Compose network needs a document store. Do not use it as-is for mission-critical data without a backup plan. A single MongoDB container has no failover, and Mongo Express adds a second login surface that you must protect. For production-grade durability, plan a replica set with scheduled backups, and keep both ports private. The database port is not published at all in this file, and the UI is bound to 127.0.0.1.

docker-compose.yml

services:
  mongodb:
    image: mongo:7
    restart: unless-stopped
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
      MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
    volumes:
      - mongo_data:/data/db
    healthcheck:
      test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping')"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

  mongo-express:
    image: mongo-express:1.0.2
    restart: unless-stopped
    environment:
      ME_CONFIG_MONGODB_URL: "mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@mongodb:27017/?authSource=admin"
      ME_CONFIG_MONGODB_ENABLE_ADMIN: "true"
      ME_CONFIG_BASICAUTH_ENABLED: "true"
      ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_EXPRESS_USER}
      ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_EXPRESS_PASSWORD}
    ports:
      - "127.0.0.1:8081:8081"
    depends_on:
      mongodb:
        condition: service_healthy

volumes:
  mongo_data:

.env

.env
# Root account created on the first start of the data volume
# Use only letters and digits in the passwords, or URL-encode them in ME_CONFIG_MONGODB_URL
MONGO_INITDB_ROOT_USERNAME=root_admin
MONGO_INITDB_ROOT_PASSWORD=CHANGE_ME_MONGO_ROOT_PASSWORD
MONGO_INITDB_DATABASE=app_db

# Web login for the Mongo Express interface
MONGO_EXPRESS_USER=ui_admin
MONGO_EXPRESS_PASSWORD=CHANGE_ME_MONGO_EXPRESS_PASSWORD

Start and verify

Load the variables for the verification commands. Compose reads .env on its own. The mongodb service becomes healthy after its first initialization, and Mongo Express starts only after that.

Shell
set -a; . ./.env; set +a
docker compose config --quiet
docker compose up -d
docker compose ps
docker compose logs --tail=50 mongodb
docker compose exec mongodb mongosh --quiet -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin --eval "db.runCommand({ ping: 1 })"
curl -s -o /dev/null -w "Mongo Express HTTP status: %{http_code}\n" -u "$MONGO_EXPRESS_USER:$MONGO_EXPRESS_PASSWORD" http://127.0.0.1:8081/

Production checklist

  • Replace every CHANGE_ME value with a long random password, and keep the root account for administration only.
  • Create an application user with readWrite rights on its own database, and do not give applications the root account.
  • Keep port 27017 unpublished. Services on the Compose network connect with the host name mongodb.
  • Use a replica set in production so that you get failover and multi-document transactions, and back up each member.
  • Run regular mongodump or filesystem snapshot backups, store them off the server and test restores.
  • Keep Mongo Express on localhost, or disable it when it is not needed, because it can read every database.
  • Set a memory limit on the container, and cap the WiredTiger cache size if the host has little RAM.
  • Create indexes for the fields you query, and check slow queries with explain before you ship new features.

Common problems

  • Symptom: the application logs an authentication failed error even though the password looks correct. Cause: the connection string does not authenticate against the admin database. Fix: add authSource=admin to the connection string, as the examples in this file do.

  • Symptom: Mongo Express cannot connect and logs ECONNREFUSED. Cause: the URL uses the wrong host name, or MongoDB is not yet healthy. Fix: keep the host name mongodb in ME_CONFIG_MONGODB_URL and wait for the mongodb service to report healthy.

  • Symptom: the password in .env was changed, but the root login still fails. Cause: MONGO_INITDB_ROOT_PASSWORD is applied only when the data directory is first created. Fix: change the password inside MongoDB with a user management command, or remove the volume with docker compose down -v if its data is disposable.

  • Symptom: the URL with a special character in the password fails to parse. Cause: characters such as @, : or / must be percent-encoded inside a connection string. Fix: choose a password made of letters and digits, or encode it before you put it in the URL.

Variations

  • Replica set for transactions: start mongod with the replSet option, initiate the set once with rs.initiate(), and point the URL at the replica set name.
  • Application API on top: place a FastAPI or Express service on the same network, and use the mongodb host name from that service.
  • TLS in front of the UI: add Caddy or Nginx with HTTPS and HTTP authentication, and expose only the proxy ports.

Questions

Why does the connection string include authSource=admin?

The official MongoDB image creates the root user in the admin database. Clients must authenticate against admin, otherwise the login fails even when the password is correct.

Why is Mongo Express limited to 127.0.0.1?

With ME_CONFIG_MONGODB_ENABLE_ADMIN set to true, the UI can read every database on the server. Keep it on localhost and reach it through an SSH tunnel, or place it behind a TLS proxy with its own access control.

Why does the MongoDB password not change after I edit .env?

The root user is created only when the data directory is empty. Change the password inside MongoDB with a user management command, or remove the volume if its contents are disposable.

Do I need a replica set for this stack?

No. A single server works for the admin UI and for ordinary reads and writes. Multi-document transactions need a replica set, which is described in the variations section.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • FastAPI + MongoDB 7 + Mongo Express

    Docker Compose stack for a Python FastAPI API on MongoDB 7 with authentication, a healthcheck, persistent storage and a protected Mongo Express UI on localhost.

    • api
    • mongodb
    • mongo-express
    Difficulty: Intermediate
  • ClickHouse 25.8 Analytics Database

    Docker Compose stack for ClickHouse 25.8 with a dedicated user, a healthcheck, raised file limits and HTTP and native ports on localhost for analytical queries.

    • clickhouse
    Difficulty: Advanced
  • Elasticsearch 8 + Kibana Search Stack

    Docker Compose stack for Elasticsearch 8.19 and Kibana 8.19 with security enabled, a persistent volume, a healthcheck and localhost-only ports.

    • elasticsearch
    • kibana
    Difficulty: Advanced
  • MariaDB 11.4 LTS + Adminer 5

    Docker Compose stack with MariaDB 11.4 LTS, a healthcheck, a named volume and Adminer 5 on localhost. A lightweight admin UI for MySQL-compatible databases.

    • mariadb
    • adminer
    Difficulty: Beginner

Browse more Databases & Search stacks or the full library.