简体   繁体   中英

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

i have a problem with MongoDB. I am provisioning the file config.js to docker-entrypoint-initdb.d in my docker-compose file:

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:

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.

But then, I want to add a trird entry, and recreate the container:

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. 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:

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?

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

    # 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM