Postgres: Add option to let the container stop gracefully after docker-entrypoint-initdb.d

Created on 25 Mar 2018  路  6Comments  路  Source: docker-library/postgres

Hi,
I have two usage scenarios where imho it would make a lot of sense to have the option to just init a postgres DBMS via scripts in /docker-entrypoint-initdb.d and then have the docker container exit gracefully as soon as those init scripts are run.

This would support my following use cases:

  • on my jenkins, I would like to get an initial pg-dump, then apply some migration scripts and then create and publish a new pg-dump. As soon as that's done, the docker container should stop.
  • in my kubernetes cluster, I would like to use a postgres docker container as a kubernetes init-container to prepare the grounds before the real postgresql container actually starts. Why: the init-container's image contains a lot of stuff that is only needed for database inititalization (in my case curl and a java runtime environment...yeah, better don't ask ;-) )..but it writes the database files into a volume which is then used by the real production postgres container.

wdyt?

Request

Most helpful comment

The last line of the docker-entrypoint.sh seems to be responsible for starting the database.That is

exec "$@"

As a hacky way to accomplish not starting the database, i currently remove the last line of docker-entrypoint.sh using a RUN command with sed inside the Dockerfile like this:

RUN sed -i '$ d' /usr/local/bin/docker-entrypoint.sh

As a more permanent solution, an ENV variable (e.g $DONT_START_DATABASE) and a change to the last lines of the docker-entrypoint.sh to exit the script succesfully before starting the database would be great. Example:

if [ "$DONT_START_DATABASE" = true ]; then
    exit 0;
fi
exec "$@"

As a plus, other people using the hacky solution with sed wouldn't be affected by the change.
What do you think?

All 6 comments

+1 - this would be super useful for seeding data into a container and then recommitting it (think allowing developers to pull a container with seeded data already inside it

The last line of the docker-entrypoint.sh seems to be responsible for starting the database.That is

exec "$@"

As a hacky way to accomplish not starting the database, i currently remove the last line of docker-entrypoint.sh using a RUN command with sed inside the Dockerfile like this:

RUN sed -i '$ d' /usr/local/bin/docker-entrypoint.sh

As a more permanent solution, an ENV variable (e.g $DONT_START_DATABASE) and a change to the last lines of the docker-entrypoint.sh to exit the script succesfully before starting the database would be great. Example:

if [ "$DONT_START_DATABASE" = true ]; then
    exit 0;
fi
exec "$@"

As a plus, other people using the hacky solution with sed wouldn't be affected by the change.
What do you think?

I have a similar kubernetes situation with the following in an init container:

- name: setup
  image: postgres
  args:
  # Trick entrypoint into exiting after setup
  - postgres
  - --version

I think the --version or sed-based solution (https://github.com/docker-library/postgres/issues/424#issuecomment-445552662) is the best we can offer right now, but https://github.com/docker-library/postgres/pull/496 should make this process completely customizable (which should make this a lot easier to accomplish).

This is now possible to accomplish properly/cleanly via #496. :tada: :+1:

I have a similar kubernetes situation with the following in an init container:

- name: setup
  image: postgres
  args:
  # Trick entrypoint into exiting after setup
  - postgres
  - --version

Looks like trick does not work anymore

Was this page helpful?
0 / 5 - 0 ratings