Search code examples
mongodbdockerdocker-composebackupreplication

Setup mongo image enabling replication and restore backup from /data/db with docker


I am trying by deploying a mongo image that enables replica with a master a slave and restores information through a volume from the /data/db/ directory

when I do not do any replication, the database works correctly, without the need for directConnect and restores all the documents, but when I configure the replicas it does not restore the information

My docker-compose without replication:

version: "3.7"
services:
  dbComMongoGR:
    container_name: dbComMongoTR
    image: mongo:5.0.5
    restart: always
    environment:
      - MONGO_INITDB_ROOT_USERNAME=ComAdmin
      - MONGO_INITDB_ROOT_PASSWORD=C0m202Z
      - MONGO_INITDB_DATABASE=TransactionesMD
    volumes:
      - ./databdTR:/data/db
    ports:
      - 27017:27017

My setup with replication:

Dockerfile:

FROM mongo:5.0.5

COPY mongod.conf /etc/mongod.conf

EXPOSE 27017

RUN echo  '10.192.85.46 dbRepComMongoTR' >> /etc/hosts \&& echo '10.192.85.28 dbComMongoTR' >> /etc/hosts

CMD ["mongod", "--config", "/etc/mongod.conf","--dbpath","/data/db"]

mongod.conf:

net:
  port: 27017
  bindIp: 127.0.0.1,dbComMongoTR
replication:
  replSet: "myRs0"

docker-compose.yml:

version: '3.7'
services:
  dbRepMongoTR:
    container_name: dbComMongoTR
    image: mongorep:1.0
    ports:
      - 27017:27017
    restart: always
    volumes:
      - ./databdTR:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=ComAdmin
      - MONGO_INITDB_ROOT_PASSWORD=C0m202Z
      - MONGO_INITDB_DATABASE=TransactionesMD
    command: bin/sh -c "mongod --replSet myRs0 --bind_ip 127.0.0.1,dbComMongoTR --dbpath /data/db"
              \&& echo  '10.192.85.46 dbRepComMongoTR' >> /etc/hosts 
              \&& echo '10.192.85.28 dbComMongoTR' >> /etc/hosts

I need to restore this backup, the original DB has 150GB of data.

In this way, to connect to mongo I have to use direct Connection, but it does not restore the volume information, I run docker exec to the container in the directory and indeed there are all the files

From the main server to the /databdTR directory I change the permissions to 777 as a solution test, it did not work


Solution

  • --storageEngine wiredTiger
    

    must be added to the start command, The complete command looks like this:

    command: bin/sh -c "mongod --replSet myRs0 --bind_ip 127.0.0.1,dbComMongoTR --dbpath /data/db --storageEngine wiredTiger"
    

    Add this to Dockerfile