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
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
.
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 --user=$root --password=$$MYSQL_ROOT_PASSWORD -e 'USE fuba-db;SELECT * FROM factor_header'
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 --user=$root --password=$$MYSQL_ROOT_PASSWORD -e 'USE fuba-db;SELECT * FROM factor_header'
The console outputs :
pro2-mysql-1 | 2022-04-07T01:56:19.982954Z 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 javaweb depends_on mysql health check, javaweb is not started either
The health check log of the mysql container is:
docker inspect pro2-mysql-1
"Health": {
"Status": "healthy",
"FailingStreak": 0,
"Log": [
{
"Start": "2022-04-07T09:56:37.609128403 08:00",
"End": "2022-04-07T09:56:37.680402225 08:00",
"ExitCode": 0,
"Output": "factor_header_id\tfactor_header_name\tsort\tisdelete\tisdouble\n100\t????\t1\tN\t1\n200\t????\t2\tN\t1\n300\t????\t3\tN\t1\n400\t????\t4\tN\t1\n500\t????\t5\tN\t1\n600\t????\t6\tN\t1\n700\t????\t7\tN\t1\n9000\t?????\t8\tN\t1\nmysql: [Warning] Using a password on the command line interface can be insecure.\n"
}
I see that the health check executes successfully and without errors (although there is a warning, which I don't think changes the result of the health check).I don't understand why the health check passes and the health status is still unhealthy.
CodePudding user response:
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)
CodePudding user response:
I solved the problem, in fact all the errors were because of the test statement
test: mysql --user=$root --password=$$MYSQL_ROOT_PASSWORD -e 'USE fuba-db;SELECT * FROM factor_header'
it's work
test: "mysql -uroot -p$$MYSQL_ROOT_PASSWORD -e 'SELECT * FROM factor_header' fuba-db "