Skip to content

Commit 9cc375d

Browse files
committed
20211002 MariaDB health check - experimental branch - PR 3 of 3
Follows on from suggestion in [Issue 415](#415) to add health-check to more containers. See also [PR 406](dbb6217). Changes: * Adds `iotstack_healthcheck.sh` script to template. * Moves Dockerfile into `buildFiles` directory, and adds commands to copy the health-check script into the local image and activate health-checking on launch. Does not change any documentation on experimental branch.
1 parent 3ab3adf commit 9cc375d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

.internal/templates/services/mariadb/Dockerfile renamed to .internal/templates/services/mariadb/buildFiles/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@ RUN sed -i.bak \
1010
-e "s/^read_buffer_size/# read_buffer_size/" \
1111
/defaults/my.cnf
1212

13+
# copy the health-check script into place
14+
ENV HEALTHCHECK_SCRIPT "iotstack_healthcheck.sh"
15+
COPY ${HEALTHCHECK_SCRIPT} /usr/local/bin/${HEALTHCHECK_SCRIPT}
16+
17+
# define the health check
18+
HEALTHCHECK \
19+
--start-period=30s \
20+
--interval=30s \
21+
--timeout=10s \
22+
--retries=3 \
23+
CMD ${HEALTHCHECK_SCRIPT} || exit 1
24+
1325
# EOF
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env sh
2+
3+
# set a default for the port
4+
# (refer https://mariadb.com/kb/en/mariadb-environment-variables/ )
5+
HEALTHCHECK_PORT="${MYSQL_TCP_PORT:-3306}"
6+
7+
# the expected response is?
8+
EXPECTED="mysqld is alive"
9+
10+
# run the check
11+
RESPONSE=$(mysqladmin -p${MYSQL_ROOT_PASSWORD} ping -h localhost)
12+
13+
# did the mysqladmin command succeed?
14+
if [ $? -eq 0 ] ; then
15+
16+
# yes! is the response as expected?
17+
if [ "$RESPONSE" = "$EXPECTED" ] ; then
18+
19+
# yes! this could still be a false positive so probe the port
20+
if nc -w 1 localhost $HEALTHCHECK_PORT >/dev/null 2>&1; then
21+
22+
# port responding - that defines healthy
23+
exit 0
24+
25+
fi
26+
27+
fi
28+
29+
fi
30+
31+
# otherwise the check fails
32+
exit 1

0 commit comments

Comments
 (0)