3

Current State

  • I have a MongoDB instance running on a server without the replication set flag (--replSet)
  • I have some previously stored information on the Database and wish to retain the information

Aim

  • I wish to however restart the container with the --replSet "my-set" flag set for the daemon and keep the previous information intact

Implementation

I am trying to follow along a tutorial for setting replica sets in MongoDB with Docker and trying it out on my local machine.

  1. Create a standard MongoDB Docker container w/o the flag replSet set which represents the current state:

    docker run -d --name mongo_rs --publish 37017:27017 mongo
    
  2. Using the MongoDB Compass I connected to the DB and added some dummy information to a Database called test and collection called players

  3. I stop the container:

    docker container stop mongo_rs
    

From here onwards I wish to add the --replSet "my-set" to the mongo_rs container and configuring the Replica set via the mongo Shell as mentioned in the tutorial. What is the possible solution for achieving it?

Shan-Desai
  • 3,101
  • 3
  • 46
  • 89
  • 1
    Unfortunately, you can't update the `CMD` of a container. You can only update what is mentioned here: [`docker update`](https://docs.docker.com/engine/reference/commandline/update/). Backup your database and spin up a new container with the flags you want. – tgogos Oct 22 '19 at 09:30
  • Also check this: [How to start a stopped Docker container with a different command?](https://stackoverflow.com/questions/32353055/how-to-start-a-stopped-docker-container-with-a-different-command). A "hackish" solution is to go to your container's configuration, somewhere like `/var/lib/docker/containers//config.json`, modify it and then restart the docker daemon. – tgogos Oct 22 '19 at 09:32
  • @tgogos I am looking into it. Would this thing be easily had it been written in a `docker-compose` file? – Shan-Desai Oct 22 '19 at 09:55
  • I can't see how `docker-compose` might seem helpful in this case. I would `docker exec -it mongo_rs bash` to get "inside" the mongo container, then use `mongoexport` to save the db to a file. You can copy this file to your host by using `docker cp ...` and then I would spin up a new mongo container and try to create the db from scratch with `mongoimport`. – tgogos Oct 24 '19 at 09:46

2 Answers2

2

Here is a workaround:

1- copy the entrypoint script to your host:

docker cp mongo_rs:/usr/local/bin/docker-entrypoint.sh .

2- edit the script , change the line (last line) exec "$@" to :

mongod --replSet my-mongo-set --port 27017

3- re-copy the script to your container:

docker cp docker-entrypoint.sh mongo_rs:/usr/local/bin/docker-entrypoint.sh

4- start your container:

docker start mongo_rs
LinPy
  • 16,987
  • 4
  • 43
  • 57
  • I don't think this works for me. I ran (persist data) ```docker run --name mongo_rs -v (dir):(dir) -p 27017:27017 mongo```, and stop the container ```docker stop mongo_rs```, and reran with ```docker start mongo_rs``` and it can not start again. I implemented the new entry point script and still does not work... – sutan Oct 28 '22 at 09:43
2

Here is my .yml file

version: '3.7'

services:
  node1:
    image: mongo 
    ports:
      - 30001:27017 
    volumes:
      - $HOME/mongoclusterdata/node1:/data/db 
    networks:
      - mongocluster
    command: mongod --replSet comments 
  node2:
    image: mongo
    ports:
      - 30002:27017
    volumes:
      - $HOME/mongoclusterdata/node2:/data/db
    networks:
      - mongocluster
    command: mongod --replSet comments
    depends_on :
      - node1 
  node3:
    image: mongo
    ports:
      - 30003:27017
    volumes:
      - $HOME/mongoclusterdata/node3:/data/db
    networks:
      - mongocluster
    command: mongod --replSet comments
    depends_on :
        - node2 

networks:
  mongocluster:
    driver: bridge 

The volume section has absolute path which is different from the root.Actually docker file creates a self config file on root , so if u have root as a docker-compose install location change it to else where, and now the config file settings would never delete on docker-install up/down.

Llex
  • 1,770
  • 1
  • 12
  • 27
Anshuman Bardhan
  • 1,986
  • 2
  • 8
  • 10