Fei
Fei

Reputation: 495

Docker Compose healthcheck mysql

I'm using the following setup: 1. Docker Engine: 18.02.0-ce-rc1, 2. Compose 1.18.0, 3. Mac

  mysql:
    image: mysql:5.6
    container_name: app-db
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    healthcheck:
      test: /usr/bin/mysql --host=127.0.0.1 --user=user --password=password --silent --execute \"SELECT 1;\"
      interval: 30s
      timeout: 10s
      retries: 5
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - app
    restart: always

But I always get the following error fairly quick (within seconds)

docker-compose up
Creating network "compose_app" with the default driver
Creating app-db ... done
Creating app-db ... 

ERROR: for app  Container "bf94292aa4fa" is unhealthy.
ERROR: Encountered errors while bringing up the project.

Please help. Thanks

Upvotes: 2

Views: 8062

Answers (2)

Ramya Musunuru
Ramya Musunuru

Reputation: 471

Some times testing in cmd shell works

Configuring this in docker-compose.yml worked for me,

environment:
    MYSQL_ROOT_USER: ${MYSQL_ROOT_USER}
    MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
restart: on-failure
healthcheck:
  test: ["CMD-SHELL", "mysql -h localhost -u root -p$$MYSQL_ROOT_PASSWORD" ]
  interval: 30s
  timeout: 20s
  retries: 10

I have set ${MYSQL_ROOT_PASSWORD} value in .env folder.

Another way to perform health check is

healthcheck:
      test: ["CMD", "mysql -h ${MYSQL_HOST} -u  ${MYSQL_ROOT_USER} -p$$MYSQL_ROOT_PASSWORD" ]
      interval: 30s
      timeout: 20s
      retries: 10

Upvotes: 2

rokpoto.com
rokpoto.com

Reputation: 10784

mysql healthcheck should probably be as follows:

    healthcheck:
      test: mysql ${MYSQL_DATABASE} --user=${MYSQL_USER} --password='${MYSQL_PASSWORD}' --silent --execute "SELECT 1;"
      interval: 30s
      timeout: 10s
      retries: 5      

Upvotes: 6

Related Questions