Skip to content

MongoDB on docker-compose - standalone || sharded || replicaSet

Posted on:May 29, 2022 at 06:20 PM

Standalone

To deploy standalone MongoDB using docker-compose you can follow instructions provided in MongoDB image on DockerHub.

Result:

version: "3.8"

services:
  mongodb5-standalone:
    image: mongo:5.0
    restart: always
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

After running docker-compose up on the yaml above, you should be able to connect to standalone MongoDB with uri:

mongodb://root:example@localhost:27017

Sharded

To deploy sharded MongoDB using docker-compose you can follow instructions provided in Bitnami image for sharded MongoDB on DockerHub.

Result:

version: "3.8"

services:
  mongodb5-sharded:
    hostname: mongodb5-sharded
    image: docker.io/bitnami/mongodb-sharded:5.0
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb5-sharded
      - MONGODB_SHARDING_MODE=mongos
      - MONGODB_CFG_PRIMARY_HOST=mongodb-cfg
      - MONGODB_CFG_REPLICA_SET_NAME=cfgreplicaset
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_ROOT_PASSWORD=password123
    ports:
      - "27017:27017"

  mongodb-shard0:
    hostname: mongodb-shard0
    image: docker.io/bitnami/mongodb-sharded:5.0
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-shard0
      - MONGODB_SHARDING_MODE=shardsvr
      - MONGODB_MONGOS_HOST=mongodb5-sharded
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_REPLICA_SET_NAME=shard0
    volumes:
      - 'shard0_data:/bitnami'

  mongodb-cfg:
    hostname: mongodb-cfg
    image: docker.io/bitnami/mongodb-sharded:5.0
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-cfg
      - MONGODB_SHARDING_MODE=configsvr
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
      - MONGODB_REPLICA_SET_NAME=cfgreplicaset
    volumes:
      - 'cfg_data:/bitnami'

volumes:
  shard0_data:
    driver: local
  cfg_data:
    driver: local

After running docker-compose up on the yaml above, you should be able to connect to sharded MongoDB with mongos uri:

mongodb://root:password123@localhost:27017

Replica set

To deploy replica set MongoDB using docker-compose you can follow instructions provided in Soham Kamani’s article. Unfortunately I had problems with starting original solution (because of no healtheckeck and missing /etc/hosts configuration).

Result after fixes:

version: "3.8"

services:
  mongodb5-replicaset-1:
    hostname: mongodb5-replicaset-1
    image: mongo:5.0
    expose:
      - 27017
    ports:
      - "27017:27017"
    restart: always
    command: mongod --port 27017 --replSet replicaSetName
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo localhost:27017/test --quiet
      interval: 2s
      timeout: 2s
      retries: 5
      start_period: 0s
  mongodb5-replicaset-2:
    hostname: mongodb5-replicaset-2
    image: mongo:5.0
    expose:
      - 27018
    ports:
      - "27018:27018"
    restart: always
    command: mongod --port 27018 --replSet replicaSetName
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo localhost:27018/test --quiet
      interval: 2s
      timeout: 2s
      retries: 5
      start_period: 0s
  mongodb5-replicaset-3:
    hostname: mongodb5-replicaset-3
    image: mongo:5.0
    expose:
      - 27019
    ports:
      - "27019:27019"
    restart: always
    command: mongod --port 27019 --replSet replicaSetName
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo localhost:27019/test --quiet
      interval: 2s
      timeout: 2s
      retries: 5
      start_period: 0s

  init-mongodb5-replicaset:
    image: mongo:5.0
    restart: "no"
    depends_on:
      mongodb5-replicaset-1:
          condition: service_healthy
      mongodb5-replicaset-2:
          condition: service_healthy
      mongodb5-replicaset-3:
          condition: service_healthy
    command: >
      mongo --host mongodb5-replicaset-1:27017 --eval '
        db = (new Mongo("mongodb5-replicaset-1:27017")).getDB("test");
        config = {
            "_id" : "replicaSetName",
            "members" : [
                {
                "_id" : 0,
                "host" : "mongodb5-replicaset-1:27017"
                },
                {
                "_id" : 1,
                "host" : "mongodb5-replicaset-2:27018"
                },
                {
                "_id" : 2,
                "host" : "mongodb5-replicaset-3:27019"
                }
            ]
        };
        rs.initiate(config);
      '

To be able to connect to all members of replica set from your local machine, add these lines to /etc/hosts:

127.0.0.1   mongodb5-replicaset-1
127.0.0.1   mongodb5-replicaset-2
127.0.0.1   mongodb5-replicaset-3

After running docker-compose up on the yaml above, you should be able to connect to replica set MongoDB with mongod instances uri:

mongodb://mongodb5-replicaset-1:27017,mongodb5-replicaset-2:27018,mongodb5-replicaset-3:27019/?replicaSet=replicaSetName