Skip to content

Commit 369b5fc

Browse files
author
Dave Barnum
committed
Add healthcheck.
1 parent a9df67d commit 369b5fc

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

Dockerfile

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ MAINTAINER [email protected]
33

44
RUN apt-get update && apt-get upgrade -y \
55
&& rm -rf /var/lib/apt/lists/*
6-
7-
COPY ["scripts/", "/docker-entrypoint-initdb.d/"]
8-
COPY ["bin/initialize.sh", "bin/preflight.sh", "bin/persistence-cleanup.sh", "/usr/local/bin/"]
9-
RUN chmod 755 /usr/local/bin/persistence-cleanup.sh; \
6+
7+
COPY ["scripts/", "/docker-entrypoint-initdb.d/"]
8+
COPY ["bin/initialize.sh", "bin/preflight.sh", "bin/docker-healthcheck", "bin/persistence-cleanup.sh", "/usr/local/bin/"]
9+
RUN chmod 755 /usr/local/bin/docker-healthcheck; \
10+
chmod 755 /usr/local/bin/persistence-cleanup.sh; \
1011
chmod 755 /usr/local/bin/initialize.sh; \
11-
chmod 755 /usr/local/bin/preflight.sh; \
12+
chmod 755 /usr/local/bin/preflight.sh; \
1213
sed -i '/bin\/bash/a /usr/local/bin/initialize.sh' /usr/local/bin/docker-entrypoint.sh; \
13-
sed -i '/exec "$@"/i /usr/local/bin/preflight.sh' /usr/local/bin/docker-entrypoint.sh
14+
sed -i '/exec "$@"/i /usr/local/bin/preflight.sh' /usr/local/bin/docker-entrypoint.sh
1415

1516
# we need to touch and chown config files, since we cant write as mysql user
1617
RUN touch /etc/mysql/conf.d/galera.cnf \
1718
touch /etc/mysql/conf.d/cust.cnf \
1819
&& chown mysql.mysql /etc/mysql/conf.d/galera.cnf \
19-
&& chown mysql.mysql /etc/mysql/conf.d/cust.cnf \
20+
&& chown mysql.mysql /etc/mysql/conf.d/cust.cnf \
2021
&& chown mysql.mysql /docker-entrypoint-initdb.d/*.sql
2122

2223
# we expose all Cluster related Ports
@@ -30,6 +31,8 @@ EXPOSE 3306 4444 4567 4568
3031
ENV GALERA_USER=galera \
3132
GALERA_PASS=galerapass \
3233
MAXSCALE_USER=maxscale \
33-
MAXSCALE_PASS=maxscalepass \
34+
MAXSCALE_PASS=maxscalepass \
3435
CLUSTER_NAME=docker_cluster \
35-
MYSQL_ALLOW_EMPTY_PASSWORD=1
36+
MYSQL_ALLOW_EMPTY_PASSWORD=1
37+
38+
HEALTHCHECK CMD ["docker-healthcheck"]

bin/docker-healthcheck

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
4+
if [ "$MYSQL_RANDOM_ROOT_PASSWORD" ] && [ -z "$MYSQL_USER" ] && [ -z "$MYSQL_PASSWORD" ]; then
5+
# there's no way we can guess what the random MySQL password was
6+
echo >&2 'healthcheck error: cannot determine random root password (and MYSQL_USER and MYSQL_PASSWORD were not set)'
7+
exit 0
8+
fi
9+
10+
host="$(hostname --ip-address || echo '127.0.0.1')"
11+
user="${MYSQL_USER:-root}"
12+
export MYSQL_PWD="${MYSQL_PASSWORD:-$MYSQL_ROOT_PASSWORD}"
13+
14+
args=(
15+
# force mysql to not use the local "mysqld.sock" (test "external" connectibility)
16+
-h"$host"
17+
-u"$user"
18+
--silent
19+
)
20+
21+
if select="$(echo 'SELECT 1' | mysql "${args[@]}")" && [ "$select" = '1' ]; then
22+
exit 0
23+
fi
24+
25+
# If the probe returns 2 ("starting") when the container has already moved out of the "starting" state then it is treated as "unhealthy" instead.
26+
# https://github.com/docker/docker/blob/dcc65376bac8e73bb5930fce4cddc2350bb7baa2/docs/reference/builder.md#healthcheck
27+
exit 2

0 commit comments

Comments
 (0)