0

docker-compose mysql health check inline complex command:

serivces:
  mysql:
    image: mysql:latest
    volumes:
      - ./init-data:/docker-entrypoint-initdb.d
    healthcheck:
      test:
        - CMD-SHELL
        - if [[ -z "$(ls -A /docker-entrypoint-initdb.d)" ]]; then
              mysql -u root -p123456 -e 'select 0'
          else
              mysql -u root -p123456 --database my_db -e 'show tables' | [ $(wc -l) -gt 100 ]
          fi
      interval: 10s
      timeout: 10s
      retries: 50

Mysql not healthy.

It works without if/else in both cases.

eastwater
  • 4,624
  • 9
  • 49
  • 118

1 Answers1

0

With CMD-SHELL test is run as sh -c '<test>', this causes errors if test command contains quotes.

Either escape the quotes in your command to run successfully with sh -c

Or put the complex command in a script file, mount the script file into container volumes and change health check command accordingly

volumes:
  - ./hc.sh:/healthcheck.sh
healthcheck:
  test:
    - CMD-SHELL
    - /healthcheck.sh
Sapan V
  • 271
  • 1
  • 9