简体   繁体   English

重新创建MongoDB容器不会在docker-entrypoint-initdb.d中重新运行config.js文件

[英]Recreation of MongoDB container doesnt re-run config.js file in docker-entrypoint-initdb.d

i have a problem with MongoDB. 我有MongoDB的问题。 I am provisioning the file config.js to docker-entrypoint-initdb.d in my docker-compose file: 我在docker-entrypoint-initdb.d docker-compose文件中将文件config.jsdocker-entrypoint-initdb.d

mongo:
  image: mongo:latest
  restart: always
  ports:
    - 27017:27017
  environment:
    MONGO_INITDB_ROOT_USERNAME: root
    MONGO_INITDB_ROOT_PASSWORD: example
    MONGO_INITDB_DATABASE: dev
  volumes:
    - ./config.js:/docker-entrypoint-initdb.d/config.js
    - mongodbdata:/data/db

The config.js file looks like this: config.js文件如下所示:

db.auth('root', 'example');

db = db.getSiblingDB('dev');

db.approver.insert({"email":"some@email.com,"approverType":"APPROVER"});
db.approver.insert({"email":"someother@email.com","approverType":"ACCOUNTANCY"});

When I run docker-compose up -d for the first time, everything is fine, the two entries are inserted into the database. 当我第一次运行docker-compose up -d时,一切都很好,两个条目都插入到数据库中。

But then, I want to add a trird entry, and recreate the container: 但是,我想添加一个trird条目,并重新创建容器:

db.auth('root', 'example');

db = db.getSiblingDB('dev');

db.approver.insert({"email":"some@email.com,"approverType":"APPROVER"});
db.approver.insert({"email":"someother@email.com","approverType":"ACCOUNTANCY"});
db.approver.insert({"email":"another@email.com","approverType":"ACCOUNTANCY"});

I run docker-compose up -d --force-recreate --no-deps mongo nothing happens. 我运行docker-compose up -d --force-recreate --no-deps mongo没有任何反应。 The container is recreated, but the 3rd entry is not there. 容器被重新创建,但第三个条目不存在。

Running docker exec -it dev_mongo_1 mongo docker-entrypoint-initdb.d/config.js returns: 运行docker exec -it dev_mongo_1 mongo docker-entrypoint-initdb.d/config.js返回:

MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d44b8e0a-a32c-4da0-a02b-c3f71d6073dd") }
MongoDB server version: 4.0.10
Error: Authentication failed.

Is there a way to recreate the container so the script is re-runned? 有没有办法重新创建容器,以便重新运行脚本? Or to run a mongo command that will re-run the script in a running container? 或者运行mongo命令,该命令将在正在运行的容器中重新运行脚本?

In mongodb's startup script there is check if initialization should be done: https://github.com/docker-library/mongo/blob/40056ae591c1caca88ffbec2a426e4da07e02d57/3.6/docker-entrypoint.sh#L225 在mongodb的启动脚本中,检查是否应该进行初始化: https//github.com/docker-library/mongo/blob/40056ae591c1caca88ffbec2a426e4da07e02d57/3.6/docker-entrypoint.sh#L225

    # check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
    if [ -n "$shouldPerformInitdb" ]; then
    ...

so probably it's done only once during DB initialization and then, as you keep DB state by using mongodbdata:/data/db , it won't initialize. 所以可能在数据库初始化期间只执行了一次,然后,当你使用mongodbdata:/data/db保持数据库状态时,它不会初始化。

To fix that you can try to type docker-compose down -v what will delete data from your DB and let you run initialization once again. 要解决此问题,您可以尝试键入docker-compose down -v什么将从数据库中删除数据,并让您再次运行初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker Compose MongoDB docker-entrypoint-initdb.d 不起作用 - Docker Compose MongoDB docker-entrypoint-initdb.d is not working Docker docker-entrypoint-initdb.d/ 带有 Mongodb 的脚本不起作用 - Docker docker-entrypoint-initdb.d/ scripts with Mongodb aren't working docker-compose mongodb访问/docker-entrypoint-initdb.d/脚本中的env变量 - docker-compose mongodb access env variables in /docker-entrypoint-initdb.d/ script 存储 [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 使用 docker-entrypoint-initdb.d 创建的 Mongodb 用户不会保留在数据库中 - Mongodb user created with docker-entrypoint-initdb.d doesn't persist in the database 如何将环境变量传递给 mongo docker-entrypoint-initdb.d? - How can I pass environment variables to mongo docker-entrypoint-initdb.d? Docker mongodb 配置文件 - Docker mongodb config file 无法在 docker 中为 windows 使用 ZE206A54E97690CCE50CC872DD42Z 文件系统运行 mongoDb 容器 - unable to run a mongoDb container in docker for windows using linux file system 在没有卷的Docker容器中运行MongoDB - Run MongoDB in a Docker container with no volumes MongoDB docker-entrypoint问题 - MongoDB docker-entrypoint issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM