Home  >  Q&A  >  body text

docker-compose health check passes, but still unhealthy

condition The compose specification was removed in versions 3.0 to 3.8, but now it's back! With version v3.9 of the compose specification, you can use condition as an option in the depends_on long syntax form.

I use docker compose to start MySQL and Java Web projects, The startup of JavaWeb requires MySQL to create complete data, so I use healthcheck

But there is a problem with mysql's healthcheck, this is my docker-compose

# docker-compose.yml
version: "3.9"  

services:

  mysql:
    build:
      context: ./mysql
    command: [
        'mysqld',
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_unicode_ci',
        '--default-time-zone=+8:00',
        '--lower-case-table-names=1'
    ]
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: fuba-db
      MYSQL_ROOT_PASSWORD: fb123456
    healthcheck:
      test: "mysql -uroot -p$$MYSQL_ROOT_PASSWORD  -e 'SELECT * FROM factor_header' fuba-db "
      interval: 1s
      timeout: 3s
      retries: 3

  redis:
    build:
      ......

  nginx:
    build:
      ......

  fubaquant:
    build:
      context: ./webapps
    ports:
      - "8080:8080"
    volumes:
      - /mnt/java/jar:/home/ruoyi  #jar包
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_started

The wrong statement is:

test: "mysql -uroot -p$$MYSQL_ROOT_PASSWORD  -e 'SELECT * FROM factor_header' fuba-db "

Console output:

pro-mysql-1      | 2022-04-07T08:16:54.203710Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
container for service "mysql" is unhealthy

Due to fubaquant dependents_on mysql healthcheck, fubaquant is not started either

The health check log of the mysql container is:

I checked the health check log of mysql and it is also healthy

thanks for your help

P粉596161915P粉596161915236 days ago342

reply all(2)I'll reply

  • P粉316890884

    P粉3168908842024-02-27 00:28:40

    It appears that you are trying to run the health check using the wrong user and password. You have

    healthcheck:
          test: ["CMD", "mysqladmin", "-u$mysql", "-p$123456", "ping", "-h", "localhost"]
    

    $mysql and $123456 will attempt to resolve the values ​​of these variables. What you want is to use the following.

    healthcheck:
          test: ["CMD", "mysqladmin", "-u$MYSQL_USER", "-p$MYSQL_PASSWORD", "ping", "-h", "localhost"]
    

    This will then try to run mysaqladmin with user mysql and password 123456 (both defined as docker-compose on mysqlService environment variables)

    reply
    0
  • P粉022140576

    P粉0221405762024-02-27 00:26:56

    I configured the health check to only allow 3 retries with 1 second intervals. When compose starts java, the mysql service has not reached a healthy state within this 3 second delay, so compose stops and reports this error. Just increase the number of retries and it will work

    reply
    0
  • Cancelreply