/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.*

Tearing my hair out for 7 hours trying to get mongodb deployed in a container on Debian 12. I have done a stack of dev work locally with mongodb in a container and no issues.

I have a bind mounted local directory mounted on /docker-entrypoint-initdb.d/ with two .js files. I scp’d these from my local MacOS machine to the Debian VPS. I used these files in local dev to see the DB, set up a user etc. Now when I start the docker container on a fresh deployment (ie. /data/db non existent, I let the container create it) I get the error in the logs:

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

I have gone into the container and verified the .js files exist:

root@1368a82ab4a5:/# ls -la /docker-entrypoint-initdb.d/
total 16
drwxrwx--- 2 ubuntu 1001 4096 Jun 29 11:21 .
drwxr-xr-x 1 root   root 4096 Jun 29 11:21 ..
-rw-r--r-- 1 ubuntu 1001  309 Jun 29 11:21 01-mongo-init.js
-rw-r--r-- 1 ubuntu 1001  713 Jun 29 11:21 02-tokens.js

The error: /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* cannily come from one code block in /usr/local/bin/docker-entrypoint.sh:

                for f in /docker-entrypoint-initdb.d/*; do
                        case "$f" in
                                *.sh) echo "$0: running $f"; . "$f" ;;
                                *.js) echo "$0: running $f"; "${mongo[@]}" "$MONGO_INITDB_DATABASE" "$f"; echo ;;
                                *)    echo "$0: ignoring $f" ;;
                        esac
                        echo
                done

The error being that the case statement does not find a .js file. However, I am in the container and copy and pasted that same code block (with a small modification for the .js case outcome) and it runs and finds my .js files. Again… this is inside the container.

root@1368a82ab4a5:/# for f in /docker-entrypoint-initdb.d/*; do
                        case "$f" in
                                *.sh) echo "$0: running $f"; . "$f" ;;
                                *.js) echo "$0: running $f";;
                                *)    echo "$0: ignoring $f" ;;
                        esac
                        echo
                done
bash: running /docker-entrypoint-initdb.d/01-mongo-init.js

bash: running /docker-entrypoint-initdb.d/02-tokens.js

root@1368a82ab4a5:/#

This is my docker-compose.yml file:

services:
  mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: unless-stopped
    volumes:
      - ./mongodb/data:/data/db/
      - ./mongodb/initdb.d:/docker-entrypoint-initdb.d/:ro
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
      MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
    ports:
      - "27017:27017"

Not sure what to do from here.

Hi @ilium007

Try granting execute and read to other. chmod o+rx mongodb/initdb.d

I spun up a new mongodb in a new directory new door to this one (same host), used the same .js init scripts and it all worked. I can shut down, delete the /data/db dir and start up the container again and the scripts get applied again. No idea what was happening yesterday.

Doesn’t the container user run as root? I don’t want to give read access to ‘Others’ on .js files that hold user credentials.

Apparently not.

// 01.js
console.log('01.js Hello world');
console.log(`01.js uid: ${process.getuid()}`);
console.log(`01.js euid: ${process.geteuid()}`);
db.getSiblingDB('foo').bar.createIndex({lastUpdated:1});
$ docker compose logs | grep 01.js
mongo-0-a-1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01.js
mongo-0-a-1  | 01.js Hello world
mongo-0-a-1  | 01.js uid: 999
mongo-0-a-1  | 01.js euid: 999
1 Like

That explains it. I’ll find a docker image that can run as a specified user.