skip to Main Content

condition was removed compose spec in versions 3.0 to 3.8 but is now back! Using version of the compose spec v3.9, you can use condition as an option in long syntax form of depends_on.

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

But there is a problem with the healthcheck of mysql, 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 statement in error is:

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

The console outputs :

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

Since fubaquant depends_on mysql healthcheck, fubaquant is not started either

The health check log of the mysql container is:

docker-compose up

I checked th healthcheck log of mysql and it’s also healthy

mysql  log

thanks for any help

2

Answers


  1. Chosen as BEST ANSWER

    I configured health check to only allow 3 retries, 1 second intervals. While compose is starting java, mysql service has not reached the healthy state within this 3s delay, so compose stops and report this error. It's work after I increased the number of retries


  2. It looks like you are trying to run the health check with wrong user and password. You have

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

    $mysql and $123456 will try to be resolved the value of those variables. What you want is to use the following instead.

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

    This will then try to run mysaqladmin with the user mysql and password 123456 (both defined as env vars for the mysql service on the docker-compose)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search