Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

798
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!