简体   繁体   English

Docker MongoDB 当提供入口点时,图像不会在 docker-entrypoint-initdb.d 中运行脚本

[英]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!!!!!!!!"

Solution解决方案

Remove your entrypoint and set your command to ["--bind_ip_all", "--replSet", "order"]删除entrypoint点并将command设置为["--bind_ip_all", "--replSet", "order"]

Problem问题

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.

相关问题 Docker docker-entrypoint-initdb.d/ 带有 Mongodb 的脚本不起作用 - Docker docker-entrypoint-initdb.d/ scripts with Mongodb aren't working 使用 docker-entrypoint-initdb.d 创建的 Mongodb 用户不会保留在数据库中 - Mongodb user created with docker-entrypoint-initdb.d doesn't persist in the database Docker Compose MongoDB docker-entrypoint-initdb.d 不起作用 - Docker Compose MongoDB docker-entrypoint-initdb.d is not working 重新创建MongoDB容器不会在docker-entrypoint-initdb.d中重新运行config.js文件 - Recreation of MongoDB container doesnt re-run config.js file in docker-entrypoint-initdb.d docker-compose mongodb访问/docker-entrypoint-initdb.d/脚本中的env变量 - docker-compose mongodb access env variables in /docker-entrypoint-initdb.d/ script 如何将环境变量传递给 mongo docker-entrypoint-initdb.d? - How can I pass environment variables to mongo docker-entrypoint-initdb.d? 存储 [main] 在 File::open() 中,::open for '/docker-entrypoint-initdb.d/create_user.js' 失败,权限被拒绝 - STORAGE [main] In File::open(), ::open for '/docker-entrypoint-initdb.d/create_user.js' failed with Permission denied MongoDB docker-entrypoint问题 - MongoDB docker-entrypoint issue podman MongoDB docker-entrypoint.sh 权限被拒绝 - podman MongoDB docker-entrypoint.sh permission denied 运行 mongo 图像时出错 - docker-entrypoint.sh:第 381 行 - Error when running mongo image - docker-entrypoint.sh: line 381
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM