bash script
For complex health check validations you could use a script
test: 'bash /tmp/healthcheck.sh'
docker-compose.yml
version: '3.7'
services:
rosariosis_db:
image: mysql:5.7
container_name: rosariosis_db
ports:
- "3306:3306"
volumes:
- ./healthcheck.sh:/tmp/healthcheck.sh
environment:
MYSQL_ROOT_PASSWORD: changeme
MYSQL_USER: rosariosis_user
MYSQL_PASSWORD: changeme
MYSQL_DATABASE: rosariosis
healthcheck:
test: 'bash /tmp/healthcheck.sh'
timeout: 5s
retries: 1
healthcheck.sh
#!/bin/bash
exit 0;
You could put any logic , just make sure to return exit 0
for success and exit 1
for error
#!/bin/bash
# https://superuser.com/a/442395
http_validation=$(curl -s -o /dev/null -w "%{http_code}" http://www.example.org)
if [[ $http_validation == 200 && $foo == "foo" ]]
then
exit 0
else
exit 1
fi
inline validation
According to this you could use a complex conditional in the healthcheck:
test: ["CMD-SHELL", "if [ \"`echo \\\"SELECT ACCOUNT_STATUS FROM DBA_USERS WHERE USERNAME = 'ANONYMOUS' AND ACCOUNT_STATUS = 'EXPIRED';\\\"|/u01/app/oracle/product/12.1.0/xe/bin/sqlplus -S sys/oracle as sysdba|grep ACCOUNT_STATUS`\" = \"ACCOUNT_STATUS\" ];then true;else false;fi"]
docker don't support $?
If docker health check supported $?
we could use
mysqladmin ping -h localhost ; echo $? > /tmp/exit_1 ; mysqladmin ping -h localhost ; echo $? > /tmp/exit_2; [[ $(cat /tmp/exit_1) == 0 && $(cat /tmp/exit_2) == 0 ]]
Basically I store the exit status in a file and then I compare the values.
But since docker don't support that, I got this error:
Error: No such container: rosariosis_db
ERROR: Invalid interpolation format for "healthcheck" option in service "rosariosis_db": "mysqladmin ping -h localhost ; echo "$?""