1

$ version: "3.7"

services:

db:

image: mysql:5.7

environment:

  MYSQL_ROOT_PASSWORD_FILE: /run/secrets/my_secret

volumes:

  - data-mysql:/var/lib/mysql

ports:

  - 3306:3306

secrets:

  - my_secret

healthcheck:

  test: out=$$(mysqladmin ping -h 111.11.11.11 -P 3306 -u root --

password=$$(cat $${FILE__MYSQL_ROOT_PASSWORD}) 2>&1); echo $$out | grep 'mysqld

is alive' || { echo $$out; exit 1; }

  interval: 10s

  timeout: 5s

  retries: 10

secrets:

my_secret:

file: ./my_file_secret.txt

volumes:

data-mysql:

driver: local

yoram
  • 83
  • 1
  • 7

1 Answers1

0

You can find an example here using docker secret, which you do, but with the syntax (for the test):

healthcheck:
      test: out=$$(mysqladmin ping -h localhost -P 3306 -u root --password=$$(cat $${FILE__MYSQL_ROOT_PASSWORD}) 2>&1); echo $$out | grep 'mysqld is alive' || { echo $$out; exit 1; }

It uses mysqladmin ping, and parsing its output to make sure the MySQL instance is alive.

VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • What about this one: healthcheck: test: ["CMD-SHELL", 'mysqladmin ping'] – yoram Aug 01 '20 at 07:31
  • changed it, healthcheck UNHEALTY – yoram Aug 01 '20 at 08:04
  • @yoram The problem with mysqladmin ping alone is that is can return false positive. grep for "is alive" is safer". – VonC Aug 01 '20 at 09:16
  • If I'm running docker on AWS server, do I need to change the localhost to IP address? – yoram Aug 01 '20 at 10:25
  • and need to use: interval: 10s timeout: 45s retries: 10 as well ? – yoram Aug 01 '20 at 10:25
  • @yoram AWS? You did not mention AWS before. Could you update your question with the exact settings of your execution environment (OS, docker version, kernel version, host service, ...) – VonC Aug 01 '20 at 10:44
  • Docker version 19.03.8, AWS - Ubuntu EC2 server – yoram Aug 01 '20 at 10:49
  • @yoram I meant, update your question, for everyone else to see, not adding a comment that is only for this answer. This is about clarifying what you need, by describing your environment, context and use case. – VonC Aug 01 '20 at 10:50