0

I have been struggling to configure the execution of scripts via /docker-entrypoint-initdb.d in a Mongo container in Docker.

I have tried to follow the documentation carefully, but without success.

project structure

enter image description here

docker-compose.yml

version: '3.3' services: mongodb: image: mongo:latest container_name: mongo_db environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: rootpass MONGO_INITDB_DATABASE: database volumes: - mongodbdata:/data/db - ./init.d/:/docker-entrypoint-initdb.d/ restart: always volumes: mongodbdata: driver: local 

init-mongo.sh

#!/bin/bash echo 'hello script' 

In the logs the command echo 'hello script' is not executed.

What should I do? Is there something I'm missing?

1 Answer 1

1

Docker only executes scripts inside docker-entrypoint-initdb.d upon creation of the image as this is mostly used for seeding data. If it's not a fresh image and you're just trying to spawn it again with your script then that's what caused it.

What you can try to do is type this in your terminal:

docker stop $(docker ps -aq) && docker rm $(docker ps -aq) && docker rmi mongo:latest -f && docker-compose up

Then check the logs :P

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, I understand what you are saying. I have removed the images. I have also used the command you provided. But still without success. =(
did you try to check within the image if the script actually exists inside the initdb directory?
Ahh I think i know the problem. can you try to switch it to /bin/sh instead of /bin/bash? then use the command again I provided. since most images don't have bash. @Xenetrix
The truth is I've checked everything again, besides applying your suggestions and I still can't find the problem. What I'm looking for is to use the script to initialize my mogodb database, but I can't move forward if I don't solve the execution problem first.
Its strange, sometimes the script works, and sometimes not... but your answer is perfect.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.