Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb with replica set in docker compose

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

like image 381
Harry Avatar asked Mar 12 '19 13:03

Harry


People also ask

How does replica set connect to MongoDB?

To connect to a replica set deployment, specify the hostname and port numbers of each instance, separated by commas, and the replica set name as the value of the replicaSet parameter in the connection string. In the following example, the hostnames are host1 , host2 , and host3 , and the port numbers are all 27017 .

Can I use MongoDB in Docker?

Can MongoDB Run in a Docker Container? MongoDB can run in a container. The official image available on Docker Hub contains the community edition of MongoDB and is maintained by the Docker team. This image can be used in a development environment.

How do I reinitialize a replica set in MongoDB?

To reset the configuration, make sure every node in your replica set is stopped. Then delete the "local" database for every node. Once you are confident all nodes are not running and the local database are gone, start the mongod process again (of course using the --replSet flag).


1 Answers

The original entrypoint from the mongo docker image is responsible for creating the (eventually missing) database on container start.

Since you overridden this entry point in your docker-compose.yml file (entrypoint: [ "/usr/bin/mongod", "--bind_ip_all","--replSet", "rs0" ]), you are loosing all the features from the original entrypoint.

This docker image documentation tells us we can pass options to the mongo process using the docker compose command: directive. Your docker-compose.yml file should be:

version: '3.1'

services:

  mongo:
    image: mongo
    restart: on-failure
    volumes:
      - /home/temp/mongo/compose/data:/data/db
    command: "--bind_ip_all --replSet rs0"
    environment:
      MONGO_INITDB_ROOT_USERNAME: XXXX
      MONGO_INITDB_ROOT_PASSWORD: XXXX
      MONGO_INITDB_DATABASE: XXXX
like image 144
Thomasleveil Avatar answered Nov 15 '22 08:11

Thomasleveil