Docker-compose with MySQL/MariaDB and healthcheck

I have a few old Django projects still running MySQL/MariaDB. Getting docker-compose to work with MySQL images is a little tricky, as the database needs too much time to start up. Below is a configuration that starts one application host and one database host.

/usr/bin/mysql is used for MariaDB because older versions have no mariadb executable. The /usr/local path is necessary for MySQL docker images.

The health check ensures the database is up before the application tries to access it. I'm sure there is a more efficient test though.

version: '2.1'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: bash -c "/usr/bin/true"  # I run tests from here
    container_name: foo_web
    depends_on:
      db:
        condition: service_healthy
  db:
    image: mariadb:latest
    container_name: foo_db
    restart: always
    healthcheck:
      test: "/usr/bin/mysql --user=foo --password=foo --execute \"SHOW DATABASES;\""
      # test: "/usr/local/mysql/bin/mysql --user=foo --password=foo --execute \"SHOW DATABASES;\""
      interval: 3s
      timeout: 1s
      retries: 5
    environment:
      MYSQL_DATABASE: foo
      MYSQL_USER: foo
      MYSQL_PASSWORD: foo

0 comments

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.