docker-compose.yaml中healthcheck配置项

作用:可用于检查容器是否正常运行,初始状态为starting,最后一次检查完成后显示sucess或者unhealthy,以下是命令执行返回的状态对应容器状态
0:状态是sucess,容器正常运行
1:状态是unhealthy,容器非正常运行
2:忽略执行命令的状态
包含healthcheck的docker-compose文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: "3.7"   #docker compose的版本
services:
tomcat:
image: tomcat:v10.0.7
ports:
- 18080:8080 # 暴露端口信息 - "宿主机端口:容器暴露端口"
container_name: tomcat_test #设置启动容器的名称,若不设置会默认生成一个
links:
- mysql:db #指定关联的容器
mysql:
image: mysql
ports:
- 13306:3306 # 暴露端口信息 - "宿主机端口:容器暴露端口"
container_name: mysql_test
environment: #设置环境变量
- MYSQL_ROOT_PASSWORD=123456
healthcheck:
test: ["CMD-SHELL","mysql -u root -p123451"] #密码是错误的,检查执行完后,状态会变成unhealthy
interval: 10s
timeout: 1s
retries: 3 #重试次数
start_period: 30s

初始状态时starting,检查完后由于检查不通过变为unhealthy

1
2
3
4
5
6
7
8
root@test:/home/test# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95388eefd716 tomcat:v10.0.7 "./bin/catalina.sh r…" 17 seconds ago Up 12 seconds 0.0.0.0:18080->8080/tcp tomcat_test
468c29ab4ed3 mysql "docker-entrypoint.s…" 20 seconds ago Up 17 seconds (health: starting) 33060/tcp, 0.0.0.0:13306->3306/tcp mysql_test
root@test:/home/test# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95388eefd716 tomcat:v10.0.7 "./bin/catalina.sh r…" About a minute ago Up About a minute 0.0.0.0:18080->8080/tcp tomcat_test
468c29ab4ed3 mysql "docker-entrypoint.s…" About a minute ago Up About a minute (unhealthy) 33060/tcp, 0.0.0.0:13306->3306/tcp mysql_test

docker-compose中控制容器的启动顺序

  • 通过links或者depends-on等选项可以控制容器的启动顺序,但是不能判断到容器是否启动好,只是容器开始运行就会运行下一个容器
  • 检查关联容器是否启动好,启动好了后再启动对应的容器(使用wait-for-it.sh脚本文件)

实例

  • 修改制作tomcat镜像的dockerfile文件如下,并且编译成tomcatone镜像文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FROM daocloud.io/library/centos
MAINTAINER ZD

ADD jdk-8u291-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_291

ADD apache-tomcat-10.0.7.tar.gz /usr/local

RUN rm -f /usr/local/*.tar.gz

COPY wait-for-it.sh /usr/local/apache-tomcat-10.0.7

WORKDIR /usr/local/apache-tomcat-10.0.7
EXPOSE 8080
CMD ["./wait-for-it.sh ","db:3306","--strict"," -- ./bin/catalina.sh run"]

cmd 中的命令是执行wait-for-it脚本,如果可以访问到对应容器db的3306端口,则启动tomcat

  • docker-compose.yaml文件内容修改为如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: "3.7"   #docker compose的版本
services:
tomcat:
image: tomcatone
volumes:
- type: bind
source: /home/test
target: /home
links:
- mysql:db #指定关联的容器
command: ./wait-for-it.sh db:3306 --timeout=60 --strict -- ./bin/catalina.sh run #指定的容器db,3306端口可以访问,设置60s超时,--strict表示访问不到则不执行后面的命令(启动tomcat)
ports:
- 18080:8080 # 暴露端口信息 - "宿主机端口:容器暴露端口"
container_name: tomcat_test #设置启动容器的名称,若不设置会默认生成一个

mysql:
image: mysql
ports:
- 13306:3306 # 暴露端口信息 - "宿主机端口:容器暴露端口"
container_name: mysql_test
environment: #设置环境变量
- MYSQL_ROOT_PASSWORD=123456
  • 通过docker-compose up 启动后,可以看到tomcat和mysql均启动起来了
1
2
3
4
root@test:/home/test# docker ps --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c04a1f7dbb7440894ad4ea3ac827049f4d6d0d432eac12fd3a3ea204bf11e1a8 tomcatone "./wait-for-it.sh db:3306 --strict -- ./bin/catalina.sh run - 18080:8080" 26 seconds ago Up 21 seconds 8080/tcp tomcat_test
3fb9d523bf9f6697d152a627e831504c82087d6dfa1abd0b7551637513c0fffe mysql "docker-entrypoint.sh mysqld" 6 minutes ago Up 26 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp mysql_test
  • 修改docker-compose.yaml文件command命令中的端口改为13306,执行docker-compose up后,由于访问不到对应的13306端口,tomcat没有启动“tomcat_test | wait-for-it.sh: strict mode, refusing to execute subprocess”
1
2
3
4
5
6
7
8
9
root@test:/home/test# docker-compose -f docker-compose.yaml up
Starting mysql_test ... done
Recreating tomcat_test ... done
Attaching to mysql_test, tomcat_test
……
mysql_test | 2021-07-06T16:15:19.030658Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.25' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
tomcat_test | wait-for-it.sh: timeout occurred after waiting 60 seconds for db:13306
tomcat_test | wait-for-it.sh: strict mode, refusing to execute subprocess
tomcat_test exited with code 124