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 usecondition
as an option in thedepends_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
It appears that you are trying to run the health check using the wrong user and password. You have
$mysql
and$123456
will attempt to resolve the values of these variables. What you want is to use the following.This will then try to run
mysaqladmin
with usermysql
and password123456
(both defined as docker-compose onmysql
Service environment variables)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