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 usecondition
as an option in long syntax form ofdepends_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:
I checked th healthcheck log of mysql and it’s also healthy
thanks for any help
2
Answers
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
It looks like you are trying to run the health check with wrong user and password. You have
$mysql
and$123456
will try to be resolved the value of those variables. What you want is to use the following instead.This will then try to run
mysaqladmin
with the usermysql
and password123456
(both defined as env vars for themysql
service on the docker-compose)