'Docker MongoDB image doesn't run scripts in docker-entrypoint-initdb.d when entrypoint is supplied
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!!!!!!!!"
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
