From c6f05d709cf1350176ed9575b34a48fbdebadce0 Mon Sep 17 00:00:00 2001 From: Jonas Hoglin Date: Fri, 2 Sep 2016 08:28:25 +0200 Subject: [PATCH 1/2] Added support for Cron. --- entrypoint.sh | 2 ++ runtime/functions | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index 616c17d..c1c8f08 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -26,6 +26,8 @@ if [[ -z ${1} ]]; then configure_postgresql + create_crond + echo "Starting PostgreSQL ${PG_VERSION}..." exec start-stop-daemon --start --chuid ${PG_USER}:${PG_USER} \ --exec ${PG_BINDIR}/postgres -- -D ${PG_DATADIR} ${EXTRA_ARGS} diff --git a/runtime/functions b/runtime/functions index bc18c4f..eba74ff 100755 --- a/runtime/functions +++ b/runtime/functions @@ -12,6 +12,28 @@ exec_as_postgres() { sudo -HEu ${PG_USER} "$@" } +create_crond() { +# Provide POSTGRES_CRON_* via environment variable +rm -rf /etc/cron.d/${PG_USER} +for item in `env`; do + case "$item" in + POSTGRES_CRON*) + ENVVAR=`echo $item | cut -d \= -f 1` + printenv $ENVVAR >> /etc/cron.d/${PG_USER} + ;; + esac +done + +if [ -f /etc/cron.d/${PG_USER} ]; then + sed -i "s/^session required pam_loginuid.so/#session required pam_loginuid.so/g" /etc/pam.d/cron + echo "Running cron(s) " + echo "***************************************************" + cat /etc/cron.d/${PG_USER} + echo "***************************************************" + cron +fi +} + map_uidgid() { USERMAP_ORIG_UID=$(id -u ${PG_USER}) USERMAP_ORIG_GID=$(id -g ${PG_USER}) From 6dbec791c769abcfe612c3fe1c8805bb92022bb0 Mon Sep 17 00:00:00 2001 From: Jonas Hoglin Date: Fri, 2 Sep 2016 10:11:54 +0200 Subject: [PATCH 2/2] Added some documentation for how cron are added to the container --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index abf8950..9ee1924 100644 --- a/README.md +++ b/README.md @@ -329,6 +329,18 @@ To access the PostgreSQL logs you can use `docker exec`. For example: docker exec -it postgresql tail -f /var/log/postgresql/postgresql-9.4-main.log ``` +## Cron + +To schedule cron jobs in the container add variables named `POSTGRES_CRON_X` : + +```bash +docker run --name postgresql -itd --restart always \ + --publish 5432:5432 \ + --volume /srv/docker/postgresql:/var/lib/postgresql \ + --env "POSTGRES_CRON_1=* * * * * postgres date >> /tmp/crond.log 2>&1" + sameersbn/postgresql:9.5-2 +``` + # UID/GID mapping The files and processes created by the container are owned by the `postgres` user that is internal to the container. In the absense of user namespace in docker the UID and GID of the containers `postgres` user may have different meaning on the host.