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
  • 193
  • 1
  • 1
  • 10

2 Answers2

1

I am trying to implement this exact solution but the health check refuses to work.

Docker File:

version: "3"

services:
  mariadb:
    container_name: maria-db
    image: jsurf/rpi-mariadb
    restart: unless-stopped
    ports:
      - 3306:3306
    secrets:
      - mariadb-root-password
      - mariadb-ha-database
      - mariadb-ha-username
      - mariadb-ha-password
    environment:
      - MYSQL_ROOT_PASSWORD=/run/secrets/mariadb-root-password
      - MYSQL_DATABASE=/run/secrets/mariadb-ha-database
      - MYSQL_USER=/run/secrets/mariadb-ha-username
      - MYSQL_PASSWORD=/run/secrets/mariadb-ha-password
    volumes: 
      - ./mounts/mariadb:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysql", "--user=root", "--password=$$(cat $$MYSQL_ROOT_PASSWORD)", "-e 'show databases'"]
      interval: 10s
      timeout: 2s
      retries: 1

secrets:
  mariadb-root-password:
    file: ./.secrets/mariadb-root-password.txt
  mariadb-ha-database: 
    file: ./.secrets/mariadb-ha-database.txt
  mariadb-ha-username:
    file: ./.secrets/mariadb-ha-username.txt
  mariadb-ha-password: 
    file: ./.secrets/mariadb-ha-password.txt

When running the health check on the container:

root@a288e9ba9f2b:/# mysql --user=root --password=$(cat $MYSQL_ROOT_PASSWORD) -e 'show databases'
+--------------------+
| Database           |
+--------------------+
| homeassistant      |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

However the container is always shown as unhealthy:

Last output ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

I must be missing something super simple, right?

Kristian Williams
  • 2,275
  • 22
  • 25
  • Manged to fix it by changing the health check to: `test: mysql --user=root --password=$$(cat $$MYSQL_ROOT_PASSWORD) -e 'show databases'` not sure why this works and the parameterized approach doesn't, but just happy it worked! – Kristian Williams Jun 10 '22 at 15:02
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,262,500
  • 529
  • 4,410
  • 5,250
  • 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