Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

799
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda