Docker Compose 健康检查失败,但我可以正常登录数据库。我错过了什么?
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc192ff6496f mariadb:10.11.10-ubi9 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes (unhealthy) 0.0.0.0:4306->3306/tcp, [::]:4306->3306/tcp spring-boot-db-1
健康检查命令是:
$ mysql -u spring -P 4306 -pspring spring -Bse 'SELECT 1'
1
从命令行登录:
$ mysql -u spring -P 4306 -pspring spring
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.11.10-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [spring]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| spring |
+--------------------+
2 rows in set (0.001 sec)
还有
compose.yml
services:
db:
image: mariadb:10.11.10-ubi9
volumes:
- mysqldata:/var/lib/mysql
ports:
- 4306:3306
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: spring
MYSQL_PASSWORD: spring
MYSQL_DATABASE: spring
healthcheck:
test: "mysql -u spring -P 4306 -pspring spring -Bse 'SELECT 1'"
start_interval: 3s
start_period: 15s
interval: 2m
retries: 3
timeout: 5s
volumes:
mysqldata:
原来健康检查是从容器内部完成的,所以我需要使用内部端口,而不是映射端口,即
3306
,也不是4306
。
所以健康检查命令应该是
"mysql -u spring -P 3306 -pspring spring -Bse 'SELECT 1'"