VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
  • 容器技术之Dockerfile(三)(2)

  提示:可以看到基于上面的dockerfile构建的镜像运行为容器,里面默认跑的进程就是我们在dockerfile中指定用户运行的进程;使用USER指定用户运行容器里的进程,需要注意该用户要对运行进程所需资源的所有权限;否则容器运行不起来;

  2、HEALTHCHECK:该指令用于定义如何对容器做健康状态检测;运行为容器后,容器里的进程不挂掉,当然容器也就不会挂掉,但是存在一种情况,容器没有挂掉,容器里的进程无法正常提供服务了,这个时候我们就需要通过一定的手段,第一时间知道容器里的进程是否健康(是否能够正常提供服务);healthcheck指令就是用来定义如果去检测容器内部进程是否健康;语法格式HEALTHCHECK [OPTIONS] CMD command;其中CMD是固定格式,而后面的command是对容器里的进程做健康状态检查的命令;而options是用来指定对容器做健康状态检查的周期时间相关信息;--interval=DURATION (default: 30s),该选项用于指定对容器做健康状态检查的频率,默认是30s一次;--timeout=DURATION (default: 30s),该选项用于指定对容器内部的进程做健康状态检查的超时时长,默认是30秒;--start-period=DURATION (default: 0s)指定对容器中的进程做健康状态检查延迟时间,默认0表示不延迟;这里补充一点,之所以要延迟多少秒做健康状态检查是因为,docker运行为容器以后,会立刻把该容器的状态标记为running状态,而对于有些初始化比较慢的容器,如果马上对它做健康状态检查,可能是不健康的状态,这样一来我们对了解容器是否健康就不是很准确了;如果配合某些工具,很可能存在检测到容器不健康就把该容器删除,然后重新创建,以此重复;这样就会导致我们的容器启动不起来; --retries=N (default: 3)表示指定对容器做健康状态检查的重试次数,默认是3次;也就是说检查到容器不健康的前提或健康的前提,它都会检查3次,如果3次检查都是失败状态那么就标记该容器不健康;而对于我们指定的命令来讲,命令的返回值就决定了容器是否健康,通常命令返回值为0表示我们执行的命令正常退出,也就意味着容器是健康状态;命令返回值为1表示容器不健康;返回值为2我们通常都是保留不使用;HEALTHCHECK NONE就表示不对容器做健康状态检查;

  示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@node1 test]# cat Dockerfile
FROM centos:7
 
LABEL maintainer="qiuhom <qiuhom@linux-1874.com>"
 
LABEL version="1.0"
 
LABEL description="this is test file \ that label-values can span multiple lines."
 
RUN yum install -y httpd
 
ADD ok.html /var/www/html/
 
CMD ["/usr/sbin/httpd","-DFOREGROUND"]
 
HEALTHCHECK --interval=5s --timeout=5s --start-period=5s --retries=2 \
        CMD curl -f http://localhost/ok.html || exit 1
 
[root@node1 test]#

  提示:以上HEALTHCHECK指令表示每5秒检查一次,超时时长为5秒,延迟5秒开始检查,重试2次;如果curl -f http://localhost/ok.html这条命令正常返回0,那么就表示容器健康,否则就返回1,表示容器不健康;

  验证:把以上dockerfile构建成镜像启动为容器,我们把ok.html删除或移动到别的目录,看看容器是否标记为不健康?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
[root@node1 test]# docker build . -t test:v1.1
Sending build context to Docker daemon  1.052MB
Step 1/8 : FROM centos:7
 ---> b5b4d78bc90c
Step 2/8 : LABEL maintainer="qiuhom <qiuhom@linux-1874.com>"
 ---> Using cache
 ---> d31363b96f38
Step 3/8 : LABEL version="1.0"
 ---> Using cache
 ---> 2281f36d7c3c
Step 4/8 : LABEL description="this is test file \ that label-values can span multiple lines."
 ---> Using cache
 ---> 8de872e222fb
Step 5/8 : RUN yum install -y httpd
 ---> Running in 9964718a2c3e
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.bfsu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-93.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-93.el7.centos for package: httpd-2.4.6-93.el7.centos.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-93.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-93.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-93.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-93.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
---> Package httpd-tools.x86_64 0:2.4.6-93.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package             Arch          Version                    Repository   Size
================================================================================
Installing:
 httpd               x86_64        2.4.6-93.el7.centos        base        2.7 M
Installing for dependencies:
 apr                 x86_64        1.4.8-5.el7                base        103 k
 apr-util            x86_64        1.5.2-6.el7                base         92 k
 centos-logos        noarch        70.0.6-3.el7.centos        base         21 M
 httpd-tools         x86_64        2.4.6-93.el7.centos        base         92 k
 mailcap             noarch        2.1.41-2.el7               base         31 k
 
Transaction Summary
================================================================================
Install  1 Package (+5 Dependent packages)
 
Total download size: 24 M
Installed size: 32 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/apr-1.4.8-5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for apr-1.4.8-5.el7.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total                                              2.0 MB/s |  24 MB  00:12    
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-8.2003.0.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                       1/6
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/6
  Installing : httpd-tools-2.4.6-93.el7.centos.x86_64                       3/6
  Installing : centos-logos-70.0.6-3.el7.centos.noarch                      4/6
  Installing : mailcap-2.1.41-2.el7.noarch                                  5/6
  Installing : httpd-2.4.6-93.el7.centos.x86_64                             6/6
  Verifying  : mailcap-2.1.41-2.el7.noarch                                  1/6
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  2/6
  Verifying  : httpd-2.4.6-93.el7.centos.x86_64                             3/6
  Verifying  : apr-1.4.8-5.el7.x86_64                                       4/6
  Verifying  : httpd-tools-2.4.6-93.el7.centos.x86_64                       5/6
  Verifying  : centos-logos-70.0.6-3.el7.centos.noarch                      6/6
 
Installed:
  httpd.x86_64 0:2.4.6-93.el7.centos                                           
 
Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7                                                     
  apr-util.x86_64 0:1.5.2-6.el7                                                
  centos-logos.noarch 0:70.0.6-3.el7.centos                                    
  httpd-tools.x86_64 0:2.4.6-93.el7.centos                                     
  mailcap.noarch 0:2.1.41-2.el7                                                
 
Complete!
Removing intermediate container 9964718a2c3e
 ---> a931e93eea06
Step 6/8 : ADD ok.html /var/www/html/
 ---> 97e61f41911d
Step 7/8 : CMD ["/usr/sbin/httpd","-DFOREGROUND"]
 ---> Running in e91ccdef90c2
Removing intermediate container e91ccdef90c2
 ---> 7c8af9bb7eb3
Step 8/8 : HEALTHCHECK --interval=5s --timeout=5s --start-period=5s --retries=2         CMD curl -f http://localhost/ok.html || exit 1
 ---> Running in 80682ab087d3
Removing intermediate container 80682ab087d3
 ---> aa53cba15046
Successfully built aa53cba15046
Successfully tagged test:v1.1
[root@node1 test]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                v1.1                aa53cba15046        8 seconds ago       312MB
test                v1                  461f6ceabc88        57 minutes ago      204MB
centos              7                   b5b4d78bc90c        4 weeks ago         203MB
[root@node1 test]# docker run --name t1 --rm -d test:v1.1
332590e683fcb29f60a28703548fce7aa83df715cbb840e1283472834867d6a1
[root@node1 test]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
332590e683fc        test:v1.1           "/usr/sbin/httpd -DF…"   3 seconds ago       Up 2 seconds (health: starting)                       t1
[root@node1 test]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMES
332590e683fc        test:v1.1           "/usr/sbin/httpd -DF…"   7 seconds ago       Up 6 seconds (healthy)                       t1
[root@node1 test]#

相关教程