0

I want to have a healthcheck for my container which executes a curl command and a check that a environment variable equals a certain value. Only if both exit with 0 the health of that container should be healthy.

I don't really know how to basically use a AND Operator on the exit code of both commands.

Any suggestions?

neoKurt
  • 67
  • 3
  • The environment-variable value will never change after the process is created, so this doesn't quite make sense. What do you have currently? Are you just having trouble constructing a check that does both tests? – David Maze Jan 06 '23 at 14:54
  • The environment variable is set by the software running inside of the container. And the container is healthy once that environment variable is set. – neoKurt Jan 06 '23 at 17:31
  • But yes constructing a check that does both is my issue. – neoKurt Jan 06 '23 at 17:32
  • What does your existing health check look like? How is it able to observe a change in the main process's environment? – David Maze Jan 06 '23 at 21:13
  • Currently it's not checking that at all. It's just a curl command but I want to add that environment variable check. – neoKurt Jan 07 '23 at 00:02
  • Prepare a [mre](https://stackoverflow.com/help/minimal-reproducible-example) and share us – JRichardsz Jan 08 '23 at 00:16

1 Answers1

0

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 "$?""
JRichardsz
  • 14,356
  • 6
  • 59
  • 94