brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: healthcheck.sh in the MariaDB image checks that the database is really ready

Database
~/tips/tip-database-mariadb-healthcheck-sh

services:

Uses the check script that ships in the MariaDB image instead of a hand-made mysqladmin line.

A container that is running is not yet a database that accepts queries. The official MariaDB image ships a script that checks the real thing, so you don’t need a hand-made mysqladmin line.

services:
  db:
    image: mariadb:10.11
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 15s
      retries: 6

--connect checks that the server accepts a TCP connection, which only happens once the entrypoint has finished its setup. --innodb_initialized checks that InnoDB crash recovery is done. The script’s own comments recommend combining the two, because the InnoDB test alone can pass too early during the first start.

It logs in as a dedicated healthcheck user that the entrypoint creates, with its credentials in .my-healthcheck.cnf in the data directory. So there’s no root password in your compose file, which is the part I like most.

The catch is that the user only exists for data directories made by a recent image. An old volume has no .my-healthcheck.cnf, and the check keeps failing. Setting MARIADB_AUTO_UPGRADE=1 once makes the entrypoint create it. Run docker exec db healthcheck.sh --connect by hand and check the exit code when the status stays unhealthy.

Further reading: Using healthcheck.sh

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link