[英]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.我想将 Mongo 与事务一起使用,因此我尝试使用 docker 设置一个副本集(仅包含一个 Mongo 节点)。
I want to automatically run rs.initiate()
once my database starts up.我想在数据库启动后自动运行
rs.initiate()
。 To do this, I'm using a script in docker-entrypoint-initdb.d.为此,我在 docker-entrypoint-initdb.d 中使用了一个脚本。
I also need to start the mongo node with the replSet option, so I updated the entrypoint value.我还需要使用 replSet 选项启动 mongo 节点,因此我更新了入口点值。 When the entrypoint is my new command, the scripts I have in
docker-entrypoint-initdb.d
does not run.当入口点是我的新命令时,我在
docker-entrypoint-initdb.d
中的脚本不会运行。 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.我知道我可以将我的
docker-compose up
命令和一个docker-exec... 'rs.initiate()'
命令包装在一个脚本中并调用它而不是docker-compose
,但这感觉就像一个 hack。
How can I make a Mongo node with a replSet and run rs.initiate()
?如何使用 replSet 创建 Mongo 节点并运行
rs.initiate()
?
docker-compose.yaml
: 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
: 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
: docker-entrypoint-initdb.d/a-run.sh
:
echo "running aaaaaaaaaaaaaaaaaaaaaaaaaaa"
docker-entrypoint-initdb.d/run.sh
: docker-entrypoint-initdb.d/run.sh
:
echo "running!!!!!!!!"
Remove your entrypoint
and set your command
to ["--bind_ip_all", "--replSet", "order"]
删除
entrypoint
点并将command
设置为["--bind_ip_all", "--replSet", "order"]
You're overwriting the entrypoint of the mongo
image.您正在覆盖
mongo
图像的入口点。 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.我不确定您期望这些启动脚本如何运行,但它是嵌入到 mongo 图像中的入口点文件的一部分。 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.相反,您可以将额外的
mongod
arguments 定义为command
的一部分,然后将其传递到入口点。 Then the entrypoint will pass them to the mongod call.然后入口点会将它们传递给 mongod 调用。 You can see here and here how the entrypoint handles taking additional arguments and passing them through to
mongod
.您可以在此处和此处看到入口点如何处理额外的 arguments 并将它们传递给
mongod
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.