1

I created a dockerfile file to build the Mongo replica set.It is not a problem to start creating a copy set, but when configuring a copy of Mongo, I don't want to manually go into the Mongo shell default configuration, which is too troublesome, so I automatically execute a script to complete automatic configuration when creating docker, and try a lot of methods, the following my specific way is not running. Work And run a script The following is my detailed document

dockerfile

FROM mongo:3.4
ENV AUTO_RUN_DIR /docker-entrypoint-initdb.d
COPY ./setup.js /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/setup.js
ENTRYPOINT ["/docker-entrypoint-initdb.d/setup.js"]

setup.js

#!/bin/bash
echo "Started.."
mongod --replSet replset0
mongo 127.0.0.1/admin -u test -p test --authenticationDatabase "admin"<<EOF
   var cfg = {
        "_id": "replset0",
        "members": [
            {
                "_id": 0,
                "host": "192.168.1.233:27018"
            }
        ]
    };
    rs.initiate(cfg, { force: true });
    rs.reconfig(cfg, { force: true });
EOF
exec "$@"

After executing the mongod --replSet replset0, mongo Unable to execute.I want to know what you can do. Thank you very much.

young
  • 19
  • 3
  • i would like to suggest different approach to accomplished dockerize mongo replica sets. please let me know – Frodo Jul 05 '18 at 07:30
  • I can only use docker-compos to create a replica set Mongo and then connect to Mongo shell by opening a CMD window to configure the replica set manually, do you have any other good ways? – young Jul 05 '18 at 07:51
  • @Frodo I have found a solution. I hope to share with you if there is any shortage. My answer on GitHub https://github.com/docker-library/mongo/issues/211#issuecomment-344347819 The answer to the ft0907 user – young Jul 10 '18 at 05:34
  • yes that's the way to go. did you able to implement it? – Frodo Jul 10 '18 at 06:12
  • @Frodo The answer to github above is the answer after my implementation. – young Jul 10 '18 at 07:21
  • Hi young, and welcome to Stack Overflow. There have been previous questions in the past about trying to use docker to automatically initiate a replica set; I recommend you take a look at my answer here: https://stackoverflow.com/a/48111781/174843 – Vince Bowdren Jul 26 '18 at 12:25
  • Thank you very much for your answer, but my way can be used to handle the set of replica sets. It's just not very elegant. I read your answer. Thank you very much for your advice. I will consider whether there is a better way. In fact, it is better for the Mongo team to come up with a solution that is the best. – young Jul 27 '18 at 08:01

0 Answers0