Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new "docker-ensure-initdb.sh" script #1150

Merged
merged 1 commit into from
Dec 13, 2023

Conversation

tianon
Copy link
Member

@tianon tianon commented Nov 30, 2023

This mimics the behavior of docker-entrypoint.sh before it starts the PostgreSQL server.

It has three main goals/uses:

  1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior

  2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution (no-op if database is already initialized)

  3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use (error if database is already initialized)

Closes #1141 (alternative to)
Closes #1113
Closes #731

This is something I wrote more than a year ago, but wasn't sure we wanted to actually commit to maintaining over time (and I'm still not 100% sold, but it does seem useful as an example of how to use docker-entrypoint.sh correctly, per #496).

@LaurentGoderre
Copy link
Member

Im probably using this wrong but it gives me:

/usr/local/bin/docker-ensure-initdb.sh: line 34: DATABASE_ALREADY_EXISTS: unbound variable

I think that's because that variable is defined inside a function in the entrypoint.

if [ "$#" -eq 0 ] || [ "$1" != 'postgres' ]; then
set -- postgres "$@"
fi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+declare -g DATABASE_ALREADY_EXISTS=

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's weird - docker_setup_env already includes declare -g DATABASE_ALREADY_EXISTS which should make this global even from within a function, and we don't try to read that variable until after we've invoked that function. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH! I think it's because this new script includes set -u where the entrypoint is still just # TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables), and we don't explicitly set the variable to empty in that function. 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, updated (and re-tested successfully 😇).

@LaurentGoderre
Copy link
Member

LaurentGoderre commented Nov 30, 2023

Beside the minor code issue, I confirm this meets the use case in #1141 using:

services:
  db-init:
    build:
      context: ../alpine3.18
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - pgdata:/var/lib/postgresql/data/
    user: postgres
    command: docker-ensure-initdb.sh

@tianon tianon force-pushed the docker-ensure-initdb branch from a9c98df to 56fba9d Compare November 30, 2023 18:30
This mimics the behavior of `docker-entrypoint.sh` before it starts the PostgreSQL server.

It has three main goals/uses:

  1. (most importantly) as an example of how to use "docker-entrypoint.sh" to extend/reuse the initialization behavior

  2. ("docker-ensure-initdb.sh") as a Kubernetes "init container" to ensure the provided database directory is initialized; see also "startup probes" for an alternative solution
       (no-op if database is already initialized)

  3. ("docker-enforce-initdb.sh") as part of CI to ensure the database is fully initialized before use
       (error if database is already initialized)
@tianon tianon force-pushed the docker-ensure-initdb branch from 56fba9d to c86568a Compare December 11, 2023 18:59
@tianon
Copy link
Member Author

tianon commented Dec 11, 2023

(rebased for merge conflicts)

@yosifkit yosifkit merged commit 31aed10 into docker-library:master Dec 13, 2023
32 checks passed
@yosifkit yosifkit deleted the docker-ensure-initdb branch December 13, 2023 22:17
docker-library-bot added a commit to docker-library-bot/official-images that referenced this pull request Dec 14, 2023
Changes:

- docker-library/postgres@31aed10: Merge pull request docker-library/postgres#1150 from infosiftr/docker-ensure-initdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants