Skip to content

Recurse subdirectories for docker-entrypoint-initdb.d functionality #179

Closed
@md5

Description

@md5

Over in postgis/docker-postgis#35, I had a user ask about adding their own script to /docker-entrypoint-initdb.d via docker-compose without overwriting the script I use for initializing PostGIS. My suggestion was to bind-mount individual scripts, but it dawned on me that it might be nice to be able to add scripts to subdirectories of /docker-entrypoint-initdb.d and have them sourced or executed according to the existing logic.

The main drawback I see of making this change is that it's possible that existing users of the postgres image may already have files in subdirectories of /docker-entrypoint-initdb.d that they don't want to have executed directly by the postgres entrypoint. Another minor but solvable issue is maintaining the execution order of the scripts, which I believe I documented when this functionality was originally added:

These initialization files will be executed in sorted name order as defined by the current locale, which defaults to en_US.utf8

Activity

yosifkit

yosifkit commented on Jul 26, 2016

@yosifkit
Member

I agree about the main drawback; I'd rather not publish a breaking change. The sorting of a find should be about the same as globing; we can even make sure that everything within a directory comes before sub-directories. Maybe we hide it behind an environment flag to turn it on? If we were to add it would we limit the depth when finding scripts/sql?

Maybe we just recommend users mount a script and have it run other scripts in their folders.

md5

md5 commented on Jul 26, 2016

@md5
ContributorAuthor

@yosifkit I think your second suggestion sounds better than adding another environment variable

md5

md5 commented on Jul 26, 2016

@md5
ContributorAuthor

Also, I thought find uses readdir order

added
RequestRequest for image modification or feature
on Apr 24, 2018
stepankuzmin

stepankuzmin commented on Jun 21, 2018

@stepankuzmin

My solution is mounting ./initdb.d folder to /initdb.d and ./initdb.sh script to /docker-entrypoint-initdb.d/initdb.sh:

for f in /initdb.d/*.sql; do
  echo "$0: running $f"
  "${psql[@]}" -f "$f"
done
nullhack

nullhack commented on Jul 10, 2019

@nullhack

A simple solution is to create a file (init.sh inside /docker-entrypoint-initdb.d/initdb.sh) with the following content:

#!/usr/bin/env bash
find /docker-entrypoint-initdb.d -mindepth 2 -type f -print0 | while read -d $'\0' f; do
  case "$f" in
    *.sh)
      if [ -x "$f" ]; then
        echo "$0: running $f"
        "$f"
      else
        echo "$0: sourcing $f"
        . "$f"
      fi
      ;;
    *.sql)    echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;;
    *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
    *)        echo "$0: ignoring $f" ;;
  esac
  echo
done

This will recurse all sub-directories inside /docker-entrypoint-initdb.d/ and run the files the same way the original Dockerfile do

yosifkit

yosifkit commented on Dec 18, 2019

@yosifkit
Member

Closing since this is not something we want to add.

Since initdb scripts can be sourced (#452) and everything in the script is functionalized (#496), a custom script in initdb can easily use the docker_process_init_files function to recurse as deeply as desired.

Related issues #179, #605, docker-library/mysql#192, docker-library/mysql#193

2 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RequestRequest for image modification or feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Recurse subdirectories for docker-entrypoint-initdb.d functionality · Issue #179 · docker-library/postgres