1

I want to use Mongo with transactions so I am trying to set up a replica set (containing only one Mongo node) with docker.

I want to automatically run rs.initiate() once my database starts up. To do this, I'm using a script in docker-entrypoint-initdb.d.

I also need to start the mongo node with the replSet option, so I updated the entrypoint value. When the entrypoint is my new command, the scripts I have in docker-entrypoint-initdb.d does not run. When I comment out my new entrypoint, the scripts do run.

I know I could wrap my docker-compose up command and a docker-exec ... 'rs.initiate()' command together in a script and call that instead of docker-compose, but that feels like a hack.

How can I make a Mongo node with a replSet and run rs.initiate()?

docker-compose.yaml:

version: "3"
services:
  order-db:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: mongo
    # entrypoint: ["mongod", "--bind_ip_all", "--replSet", "order"]
    volumes:
      - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      - ./docker-entrypoint-initdb.d/run.sh:/docker-entrypoint-initdb.d/run.sh:ro
      - ./docker-entrypoint-initdb.d/arun.sh:/docker-entrypoint-initdb.d/arun.sh:ro

docker-entrypoint-initdb.d/mongo-init.js:

rs.initiate();
db.createCollection("just-testing-to-see-if-this-file-runs");

docker-entrypoint-initdb.d/a-run.sh:

echo "running aaaaaaaaaaaaaaaaaaaaaaaaaaa"

docker-entrypoint-initdb.d/run.sh:

echo "running!!!!!!!!"

1 Answer 1

2

Solution

Remove your entrypoint and set your command to ["--bind_ip_all", "--replSet", "order"]

Problem

You're overwriting the entrypoint of the mongo image. I'm not sure how you are expecting those startup scripts to run, but it's part of the entrypoint file that comes baked into the mongo image. By defining your own entrypoint, you are preventing the container from executing the existing one, which is why those initialization scripts are not run. Instead you can define your additional mongod arguments as part of the command which will then be passed into the entrypoint. Then the entrypoint will pass them to the mongod call. You can see here and here how the entrypoint handles taking additional arguments and passing them through to mongod.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It seems that this approach creates a regular node first, and then recreates it in the replica set? I get the error that This node was not started with the replSet option when my mongo-init.js runs, and then a message that the server was restarted, and then a message that I need to initialize the replica set.
Np. Yes you're right about the way the entrypoint works. A mongo instance is started, then the init scripts are run, then it is stopped. Then later it starts up mongo again with your arguments. You need to run rs.Initialize() after the db is up, not during the init. To do this use --eval instead of an init script, see here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.