DEV Community

Cover image for MongoDB composed
Ran Cohen
Ran Cohen

Posted on

MongoDB composed

Introduction:

This post provides a ready-to-use Docker Compose based configuration that allows you to effortlessly set up a MongoDB single node replica set with a reliable healthcheck and a Web GUI of mongo-express.

Prerequisites

Configuration

docker-compose.yml

version: '3.9'

services:
  mongo:
    container_name: mongo
    image: mongo:${MONGO_VERSION} # https://hub.docker.com/_/mongo
    entrypoint:
      - bash
      - -c
      - |
        if [ ! -f ${MONGO_REPLICA_SET_KEY_FILE} ]; then
          echo "generating replica set key file ..."
          openssl rand -base64 756 > ${MONGO_REPLICA_SET_KEY_FILE}
          chmod 600 ${MONGO_REPLICA_SET_KEY_FILE}
          chown 999:999 ${MONGO_REPLICA_SET_KEY_FILE}
        fi
        exec docker-entrypoint.sh $$@
    command: "--bind_ip_all --replSet ${MONGO_REPLICA_SET_NAME} --keyFile ${MONGO_REPLICA_SET_KEY_FILE}"
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
    ports:
      - 27017:27017
    healthcheck:
      test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
      start_period: 30s
    restart: unless-stopped

  mongo-gui:
    container_name: mongo-gui
    image: mongo-express:${MONGO_GUI_VERSION} # https://hub.docker.com/_/mongo-express
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_USERNAME}
      ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_PASSWORD}
      ME_CONFIG_MONGODB_SERVER: mongo
      ME_CONFIG_MONGODB_ENABLE_ADMIN: true
    ports:
      - 8081:8081
    depends_on:
      mongo:
        condition: service_healthy
    restart: unless-stopped
Enter fullscreen mode Exit fullscreen mode

.env

MONGO_VERSION=5.0.18 # pick your version from https://hub.docker.com/_/mongo/tags
MONGO_USERNAME=root
MONGO_PASSWORD=root
MONGO_REPLICA_SET_NAME=rs0
MONGO_REPLICA_SET_KEY_FILE=/opt/rs.key
MONGO_GUI_VERSION=0.54.0 # change to "latest" after a stable release, currently on 1.0.0-alpha.X
Enter fullscreen mode Exit fullscreen mode

connection string

# directConnection=true is a must for the local topology - see https://www.mongodb.com/docs/drivers/node/current/fundamentals/connection/connect/#direct-connection, https://jira.mongodb.org/browse/NODE-2966
MONGODB_URL=mongodb://root:root@localhost:27017?replicaSet=rs0&directConnection=true
Enter fullscreen mode Exit fullscreen mode

commands

# start
docker compose --project-name composed-mongodb up --detach

# stop
docker compose --project-name composed-mongodb stop

# clean
docker compose --project-name composed-mongodb down --volumes --remove-orphans
Enter fullscreen mode Exit fullscreen mode

verify

Open the browser and navigate to http://localhost:8081/, you should see the following:

Mongo Express

Resources:

Top comments (0)