Home:ALL Converter>Mongodb with replica set in docker compose

Mongodb with replica set in docker compose

Ask Time:2019-03-12T21:14:27         Author:Harry

Json Formatter

I am able to set the username, password and create database using the docker compose.yml file

version: '3'

services:
  mongodb:
    image: mongo
    volumes:
      - /home/temp/finalewd/temp:/var/lib/mongodb
    restart: on-failure
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: XXXX
      MONGO_INITDB_ROOT_PASSWORD: XXXX
      MONGO_INITDB_DATABASE: XXXX

It brings up the new mongo container with database as XXXX , username and password configured.

But when I try to set up the mongo docker container with replica set like below,

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: on-failure
    volumes:
      - /home/temp/mongo/compose/data:/data/db
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all","--replSet", "rs0" ]
    environment:
      MONGO_INITDB_ROOT_USERNAME: XXXX
      MONGO_INITDB_ROOT_PASSWORD: XXXX
      MONGO_INITDB_DATABASE: XXXX

Using the above docker-compose.yml, It is bringing up with replica set but not creating the database / username / password.

Why the environment variable is not used in the above case?

Anyhelp is appreciated.

Content Added for Thomas's answer :

Tried the docker-compose.yml using the content in the Thomas's answer and once the mongo is up, I tried "rs.initiate but its throwing the already initialized status so How to bring the mongo up with master status in this case?

rs0:OTHER> rs.status();
{
    "operationTime" : Timestamp(1552397666, 1),
    "ok" : 0,
    "errmsg" : "Our replica set config is invalid or we are not a member of it",
    "code" : 93,
    "codeName" : "InvalidReplicaSetConfig",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1552397666, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}


rs0:OTHER> rs.initiate()
{
    "operationTime" : Timestamp(1552397666, 1),
    "ok" : 0,
    "errmsg" : "already initialized",
    "code" : 23,
    "codeName" : "AlreadyInitialized",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1552397666, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
rs0:OTHER> 

Thanks, Harry

Author:Harry,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55122346/mongodb-with-replica-set-in-docker-compose
yy