上一篇文章介绍了使用docker commit命令来构建自己的镜像。
本篇文章将使用Dockerfile实现上篇文章中的需求。

1 构建自己的镜像

此处我们打算,给一个centos:6.8容器安装nginx服务器。
并将其状态保留,以便不用每次启动新容器都要再次安装nginx。

1.1 构建Dockerfile上下文

先来随便找个位置建个目录当做Dockerfile的上下文。
此处本人在/root/workspace/docker/建立目录my-first-Dockerfile,如下所示:

1
2
3
4
5
6
7
8
9
[root@h1 my-first-Dockerfile]# pwd
/root/workspace/docker/my-first-Dockerfile
[root@h1 my-first-Dockerfile]# tree
.
├── Dockerfile #Dockerfile文件
└── nginx.repo #安装nginx用的yum源
0 directories, 2 files
[root@h1 my-first-Dockerfile]#

文件内容如下:

1
2
3
4
5
6
7
# nginx.repo文件内容
[root@h1 my-first-Dockerfile]# cat nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

1.2 Dockerfile文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Dockerfile内容
[root@h1 my-first-Dockerfile]# cat Dockerfile
# version 0.0.1-snapshot
# 从一个基础镜像centos:6.8开始构建
FROM centos:6.8
# 维护者信息
MAINTAINER hylexus "hylexus@163.com"
# 将Dockerfile上下文中的nginx.repo复制到容器中的yum源位置
COPY ./nginx.repo /etc/yum.repos.d/nginx.repo
RUN yum makecache
# 安装nginx
RUN yum install -y nginx
# 修改nginx首页信息
RUN echo "home page of container niginx server" > /usr/share/nginx/html/index.html
# 暴露80端口
EXPOSE 80
[root@h1 my-first-Dockerfile]#

1.3 构建镜像

执行命令开始构建:

1
docker build -t="hylexus/nginx-1" .

过程如下:

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
# 命令最后的点表示Dockerfile所在目录
[root@h1 my-first-Dockerfile]# docker build -t="hylexus/nginx-1" .
Sending build context to Docker daemon 4.096 kB
Sending build context to Docker daemon
Step 0 : FROM centos:6.8
---> 80e46367f846
Step 1 : MAINTAINER hylexus "hylexus@163.com"
---> Using cache
---> c518397fc23e
Step 2 : COPY ./nginx.repo /etc/yum.repos.d/nginx.repo
---> 5c6e01981678
Removing intermediate container c6e430804af3
Step 3 : RUN yum makecache
---> Running in d17bef44ff52
Loaded plugins: fastestmirror, ovl
Metadata Cache Created
---> 25e01b8498fc
Removing intermediate container d17bef44ff52
Step 4 : RUN yum install -y nginx
---> Running in 3054e6a7d381
Loaded plugins: fastestmirror, ovl
Setting up Install Process
Determining fastest mirrors
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.10.1-1.el6.ngx will be installed
# 此处省略N行.....
--> Running transaction check
---> Package dbus-glib.x86_64 0:0.86-6.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nginx x86_64 1.10.1-1.el6.ngx nginx 821 k
Installing for dependencies:
# 此处省略N行...
util-linux-ng x86_64 2.17.2-12.24.el6 base 1.6 M
Transaction Summary
================================================================================
Install 15 Package(s)
Total download size: 21 M
Installed size: 41 M
Downloading Packages:
--------------------------------------------------------------------------------
Total 356 kB/s | 21 MB 01:01
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
Package: centos-release-6-8.el6.centos.12.3.x86_64 (@CentOS/6.8)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : hwdata-0.233-16.1.el6.noarch 1/15
# 此处省略N行……
Installing : nginx-1.10.1-1.el6.ngx.x86_64 15/15
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
Verifying : iptables-1.4.7-16.el6.x86_64 1/15
# 此处省略N行
Verifying : hwdata-0.233-16.1.el6.noarch 15/15
Installed:
nginx.x86_64 0:1.10.1-1.el6.ngx
Dependency Installed:
#此处省略N行……
util-linux-ng.x86_64 0:2.17.2-12.24.el6
Complete!
---> 8741ff7c796b
Removing intermediate container 3054e6a7d381
Step 5 : RUN echo "home page of container niginx server" > /usr/share/nginx/html/index.html
---> Running in 81627cb51b65
---> 0d610e7a0000
Removing intermediate container 81627cb51b65
Step 6 : EXPOSE 80
---> Running in 778468b1dcea
---> 9f18a04cafcb
Removing intermediate container 778468b1dcea
Successfully built 9f18a04cafcb
[root@h1 my-first-Dockerfile]#

1.4 启动容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查看刚才构建的镜像
[root@h1 my-first-Dockerfile]# docker images hylexus/nginx-1
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hylexus/nginx-1 latest 9f18a04cafcb 38 minutes ago 564.4 MB
# 启动容器
[root@h1 my-first-Dockerfile]# docker run -d -p 80 --name my-nginx-server hylexus/nginx-1 nginx -g "daemon off;"
8d6d810448fb7c80825f62058494f77eb991ee0e43ef82fa9367b96dd343f617
# 查看随机生成的端口映射
[root@h1 my-first-Dockerfile]# docker port 8d6d810448fb7c80825f62058494f77eb991ee0e43ef82fa9367b96dd343f617
80/tcp -> 0.0.0.0:32768
[root@h1 my-first-Dockerfile]#
#####################################################
#####################################################
# 当然也可以在启动的时候指定端口映射
[root@h1 my-first-Dockerfile]# docker run -dit -p 8080:80 --name my-nginx-server hylexus/nginx-1 nginx -g "daemon off;"
726ef73f47d1bd419f9aebbc2aaecae494170a3733a59678a0fc6e5a52b502a6
# 查看端口映射
[root@h1 my-first-Dockerfile]# docker port 726ef73f47d1bd419f9aebbc2aaecae494170a3733a59678a0fc6e5a52b502a6
80/tcp -> 0.0.0.0:8080

之后就可以使用浏览器访问测试了

1.5 将镜像推送到DockerHub

就像github一样的版本控制一样。自己的docker镜像也可以提交到DockerHub。

1
2
3
# 要先登录DockerHub
# 此处的hylexus/nginx-1即是镜像名称:<user-name>/<image-name>
docker push hylexus/nginx-1

2 构建过程中的几个问题

2.1 Dockerfile大致流程

从构建时的输出,可以看出每条RUN指令都会生成一个新的镜像层。
并且默认情况下RUN指令都会以/bin/sh -c包装执行。

大致流程如下:

  • Dockerfile的第一条指令一般都是FROM,表示从一个基础镜像开始构建
  • 执行一条命令对镜像做出修改
  • 提交更新
  • 基于本次更新,运行新的容器
  • 继续执行下一条命令
  • 如此反复执行……

2.2 缓存

在构建过程中每次生成一层新的镜像的时候这个镜像就会被缓存。即使是后面的某个步骤导致构建失败,再次构建的时候就会从失败的那层镜像的前一条指令继续往下执行。
如果不想使用这种缓存功能,可以在构建的时候加上--no-cache选项:

1
docker build --no-cache -t="hylexus/nginx-1" .

下一篇将介绍Dockerfile常用指令。