Skip to content

Add healthcheck #421

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 10/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ ENV PGDATA /var/lib/postgresql/data
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values)
VOLUME /var/lib/postgresql/data

COPY docker-entrypoint.sh /usr/local/bin/
COPY docker-*.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
HEALTHCHECK CMD ["docker-healthcheck.sh"]

EXPOSE 5432
CMD ["postgres"]
3 changes: 2 additions & 1 deletion 10/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ ENV PGDATA /var/lib/postgresql/data
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values)
VOLUME /var/lib/postgresql/data

COPY docker-entrypoint.sh /usr/local/bin/
COPY docker-*.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
HEALTHCHECK CMD ["docker-healthcheck.sh"]

EXPOSE 5432
CMD ["postgres"]
25 changes: 25 additions & 0 deletions 10/alpine/docker-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
LOCK_PATH=/tmp/docker-entrypoint.lock

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

26 changes: 5 additions & 21 deletions 10/alpine/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
#!/usr/bin/env bash
set -e
source /usr/local/bin/docker-common.sh

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
if [ "$(id -u)" != '0' ]; then
touch ${LOCK_PATH}
fi

if [ "${1:0:1}" = '-' ]; then
set -- postgres "$@"
Expand Down Expand Up @@ -142,4 +125,5 @@ if [ "$1" = 'postgres' ]; then
fi
fi

rm -f ${LOCK_PATH} > /dev/null || :
exec "$@"
50 changes: 50 additions & 0 deletions 10/alpine/docker-healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
source /usr/local/bin/docker-common.sh

# docker-entrypoint starts an postgres temporarily
# ensure the entrypoint script is over
if [ -e ${LOCK_PATH} ]; then
echo "Entrypoint is still running"
exit 1
fi

echo "Entrypoint has finished"

file_env 'POSTGRES_USER' 'postgres'
file_env 'POSTGRES_DB' "$POSTGRES_USER"
file_env 'POSTGRES_PASSWORD'
file_env 'POSTGRES_HEALTH_QUERY' "SELECT 'uptime: ' || now() - pg_postmaster_start_time();"

pg_isready=(pg_isready)

if [ "${POSTGRES_USER}" != "" ]; then
pg_isready+=(--username "${POSTGRES_USER}")
fi

if [ "${POSTGRES_DB}" != "" ]; then
pg_isready+=(--dbname "${POSTGRES_DB}")
fi

${pg_isready[@]} || exit 1

echo "Postgres accepts connections"

if [ "${POSTGRES_HEALTH_QUERY}" != "" ]; then
health=(psql -t -v ON_ERROR_STOP=1)

if [ "${POSTGRES_USER}" != "" ]; then
health+=(--username "${POSTGRES_USER}")
fi

if [ "${POSTGRES_PASSWORD}" != "" ]; then
export PGPASWORD=${POSTGRES_PASSWORD}
fi

if [ "${POSTGRES_DB}" != "" ]; then
health+=(--dbname "${POSTGRES_DB}")
fi
echo ${POSTGRES_HEALTH_QUERY} | ${health[@]} || exit 1
echo "Health query succeed"
fi

exit 0
25 changes: 25 additions & 0 deletions 10/docker-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
LOCK_PATH=/tmp/docker-entrypoint.lock

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

26 changes: 5 additions & 21 deletions 10/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
#!/usr/bin/env bash
set -e
source /usr/local/bin/docker-common.sh

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
if [ "$(id -u)" != '0' ]; then
touch ${LOCK_PATH}
fi

if [ "${1:0:1}" = '-' ]; then
set -- postgres "$@"
Expand Down Expand Up @@ -142,4 +125,5 @@ if [ "$1" = 'postgres' ]; then
fi
fi

rm -f ${LOCK_PATH} > /dev/null || :
exec "$@"
50 changes: 50 additions & 0 deletions 10/docker-healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
source /usr/local/bin/docker-common.sh

# docker-entrypoint starts an postgres temporarily
# ensure the entrypoint script is over
if [ -e ${LOCK_PATH} ]; then
echo "Entrypoint is still running"
exit 1
fi

echo "Entrypoint has finished"

file_env 'POSTGRES_USER' 'postgres'
file_env 'POSTGRES_DB' "$POSTGRES_USER"
file_env 'POSTGRES_PASSWORD'
file_env 'POSTGRES_HEALTH_QUERY' "SELECT 'uptime: ' || now() - pg_postmaster_start_time();"

pg_isready=(pg_isready)

if [ "${POSTGRES_USER}" != "" ]; then
pg_isready+=(--username "${POSTGRES_USER}")
fi

if [ "${POSTGRES_DB}" != "" ]; then
pg_isready+=(--dbname "${POSTGRES_DB}")
fi

${pg_isready[@]} || exit 1

echo "Postgres accepts connections"

if [ "${POSTGRES_HEALTH_QUERY}" != "" ]; then
health=(psql -t -v ON_ERROR_STOP=1)

if [ "${POSTGRES_USER}" != "" ]; then
health+=(--username "${POSTGRES_USER}")
fi

if [ "${POSTGRES_PASSWORD}" != "" ]; then
export PGPASWORD=${POSTGRES_PASSWORD}
fi

if [ "${POSTGRES_DB}" != "" ]; then
health+=(--dbname "${POSTGRES_DB}")
fi
echo ${POSTGRES_HEALTH_QUERY} | ${health[@]} || exit 1
echo "Health query succeed"
fi

exit 0
3 changes: 2 additions & 1 deletion 9.3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ ENV PGDATA /var/lib/postgresql/data
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values)
VOLUME /var/lib/postgresql/data

COPY docker-entrypoint.sh /usr/local/bin/
COPY docker-*.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
HEALTHCHECK CMD ["docker-healthcheck.sh"]

EXPOSE 5432
CMD ["postgres"]
Binary file added 9.3/alpine/.Dockerfile.swp
Binary file not shown.
3 changes: 2 additions & 1 deletion 9.3/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ ENV PGDATA /var/lib/postgresql/data
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values)
VOLUME /var/lib/postgresql/data

COPY docker-entrypoint.sh /usr/local/bin/
COPY docker-*.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
HEALTHCHECK CMD ["docker-healthcheck.sh"]

EXPOSE 5432
CMD ["postgres"]
25 changes: 25 additions & 0 deletions 9.3/alpine/docker-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
LOCK_PATH=/tmp/docker-entrypoint.lock

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}

26 changes: 5 additions & 21 deletions 9.3/alpine/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
#!/usr/bin/env bash
set -e
source /usr/local/bin/docker-common.sh

# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
if [ "$(id -u)" != '0' ]; then
touch ${LOCK_PATH}
fi

if [ "${1:0:1}" = '-' ]; then
set -- postgres "$@"
Expand Down Expand Up @@ -142,4 +125,5 @@ if [ "$1" = 'postgres' ]; then
fi
fi

rm -f ${LOCK_PATH} > /dev/null || :
exec "$@"
50 changes: 50 additions & 0 deletions 9.3/alpine/docker-healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
source /usr/local/bin/docker-common.sh

# docker-entrypoint starts an postgres temporarily
# ensure the entrypoint script is over
if [ -e ${LOCK_PATH} ]; then
echo "Entrypoint is still running"
exit 1
fi

echo "Entrypoint has finished"

file_env 'POSTGRES_USER' 'postgres'
file_env 'POSTGRES_DB' "$POSTGRES_USER"
file_env 'POSTGRES_PASSWORD'
file_env 'POSTGRES_HEALTH_QUERY' "SELECT 'uptime: ' || now() - pg_postmaster_start_time();"

pg_isready=(pg_isready)

if [ "${POSTGRES_USER}" != "" ]; then
pg_isready+=(--username "${POSTGRES_USER}")
fi

if [ "${POSTGRES_DB}" != "" ]; then
pg_isready+=(--dbname "${POSTGRES_DB}")
fi

${pg_isready[@]} || exit 1

echo "Postgres accepts connections"

if [ "${POSTGRES_HEALTH_QUERY}" != "" ]; then
health=(psql -t -v ON_ERROR_STOP=1)

if [ "${POSTGRES_USER}" != "" ]; then
health+=(--username "${POSTGRES_USER}")
fi

if [ "${POSTGRES_PASSWORD}" != "" ]; then
export PGPASWORD=${POSTGRES_PASSWORD}
fi

if [ "${POSTGRES_DB}" != "" ]; then
health+=(--dbname "${POSTGRES_DB}")
fi
echo ${POSTGRES_HEALTH_QUERY} | ${health[@]} || exit 1
echo "Health query succeed"
fi

exit 0
Loading