diff --git a/Dockerfile b/Dockerfile index 8ebc547..8281d8a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,20 +3,21 @@ MAINTAINER toughiq@gmail.com RUN apt-get update && apt-get upgrade -y \ && rm -rf /var/lib/apt/lists/* - -COPY ["scripts/", "/docker-entrypoint-initdb.d/"] -COPY ["bin/initialize.sh", "bin/preflight.sh", "bin/persistence-cleanup.sh", "/usr/local/bin/"] -RUN chmod 755 /usr/local/bin/persistence-cleanup.sh; \ + +COPY ["scripts/", "/docker-entrypoint-initdb.d/"] +COPY ["bin/initialize.sh", "bin/preflight.sh", "bin/docker-healthcheck", "bin/persistence-cleanup.sh", "/usr/local/bin/"] +RUN chmod 755 /usr/local/bin/docker-healthcheck; \ + chmod 755 /usr/local/bin/persistence-cleanup.sh; \ chmod 755 /usr/local/bin/initialize.sh; \ - chmod 755 /usr/local/bin/preflight.sh; \ + chmod 755 /usr/local/bin/preflight.sh; \ sed -i '/bin\/bash/a /usr/local/bin/initialize.sh' /usr/local/bin/docker-entrypoint.sh; \ - sed -i '/exec "$@"/i /usr/local/bin/preflight.sh' /usr/local/bin/docker-entrypoint.sh + sed -i '/exec "$@"/i /usr/local/bin/preflight.sh' /usr/local/bin/docker-entrypoint.sh # we need to touch and chown config files, since we cant write as mysql user RUN touch /etc/mysql/conf.d/galera.cnf \ touch /etc/mysql/conf.d/cust.cnf \ && chown mysql.mysql /etc/mysql/conf.d/galera.cnf \ - && chown mysql.mysql /etc/mysql/conf.d/cust.cnf \ + && chown mysql.mysql /etc/mysql/conf.d/cust.cnf \ && chown mysql.mysql /docker-entrypoint-initdb.d/*.sql # we expose all Cluster related Ports @@ -30,6 +31,8 @@ EXPOSE 3306 4444 4567 4568 ENV GALERA_USER=galera \ GALERA_PASS=galerapass \ MAXSCALE_USER=maxscale \ - MAXSCALE_PASS=maxscalepass \ + MAXSCALE_PASS=maxscalepass \ CLUSTER_NAME=docker_cluster \ - MYSQL_ALLOW_EMPTY_PASSWORD=1 \ No newline at end of file + MYSQL_ALLOW_EMPTY_PASSWORD=1 + +HEALTHCHECK CMD ["docker-healthcheck"] \ No newline at end of file diff --git a/bin/docker-healthcheck b/bin/docker-healthcheck new file mode 100644 index 0000000..0e3f102 --- /dev/null +++ b/bin/docker-healthcheck @@ -0,0 +1,27 @@ +#!/bin/bash +set -eo pipefail + +if [ "$MYSQL_RANDOM_ROOT_PASSWORD" ] && [ -z "$MYSQL_USER" ] && [ -z "$MYSQL_PASSWORD" ]; then + # there's no way we can guess what the random MySQL password was + echo >&2 'healthcheck error: cannot determine random root password (and MYSQL_USER and MYSQL_PASSWORD were not set)' + exit 0 +fi + +host="$(hostname --ip-address || echo '127.0.0.1')" +user="${MYSQL_USER:-root}" +export MYSQL_PWD="${MYSQL_PASSWORD:-$MYSQL_ROOT_PASSWORD}" + +args=( + # force mysql to not use the local "mysqld.sock" (test "external" connectibility) + -h"$host" + -u"$user" + --silent +) + +if select="$(echo 'SELECT 1' | mysql "${args[@]}")" && [ "$select" = '1' ]; then + exit 0 +fi + +# If the probe returns 2 ("starting") when the container has already moved out of the "starting" state then it is treated as "unhealthy" instead. +# https://github.com/docker/docker/blob/dcc65376bac8e73bb5930fce4cddc2350bb7baa2/docs/reference/builder.md#healthcheck +exit 2 \ No newline at end of file