初识 Dockerfile
之前我们学到如何从容器镜像,本节我们学习如何使用 Dockerfile 从头构建镜像。
Dockerfile 是 Docker 中用于定义镜像化构建流程的,在 Dockerfile 中,包含了构建镜像过程中需要执行的命令和其他操作。它可以明确设定 Docker 镜像的制作过程,帮助我们在容器体系下能够完成构建。
我们在本地新建目录,编辑名为 Dockerfile 的,注意名大小写。
mkdir -p ~/docker//
cd ~/docker/
vim Dockerfile
# 构建基于ubuntu的docker定制镜像
# 基础镜像
FROM ubuntu
# 镜像作者
MAINTAINER my_name myemail@domain.com
# 执行命令
## 换成国内的软件源
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
RUN sed -i 's/s.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
## 安装
RUN apt update >/dev/null 2>&1
RUN apt install -y >/dev/null 2>&1
# 暴露对外端口
EXPOSE 80
将保存下来,尝试构建镜像。 在当前 Dockerfile 所在目录执行如下命令:
docker build --network=host -t ubuntu-:v1 .
--network=host
使用宿主机的网络连接代理容器的网络(在一些情况下,容器可能无法顺畅地连接外网)。
-t ubuntu-:v1
指定生产的镜像为 ubuntu- ,版本号为 v1。
我们执行 docker build --network=host -t ubuntu-:v1 .
命令,看看在构建过程中做了哪些操作:
[user@centos8 ]$ docker build --network=host -t ubuntu-:v1 .
# 将上下文求发送给Docker引擎
Sending build context to Docker daemon 2.56kB
# 下载依赖的镜像
Step 1/7 : FROM ubuntu
latest: Pulling from library/ubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Status: Downloaded newer image for ubuntu:latest
# 镜像 1d622ef86b13
---> 1d622ef86b13
Step 2/7 : MAINTAINER my_name myemail@domain.com
# 运行容器 4eec6e3094f0,在容器内运行上面的这个命令,维护者信息
---> Running in 4eec6e3094f0
# 移除临时容器 4eec6e3094f0
Removing intermediate container 4eec6e3094f0
# 镜像 6679d1c204e3
---> 6679d1c204e3
Step 3/7 : RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# 运行容器84d38c20d8c4,在容器内运行上面的这个命令,更换软件源记录
---> Running in 84d38c20d8c4
# 移除临时容器 84d38c20d8c4
Removing intermediate container 84d38c20d8c4
# 镜像 83f29f7b055a
---> 83f29f7b055a
Step 4/7 : RUN sed -i 's/s.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# 运行容器 763e4493d93f, 在容器内运行上面的这个命令,更换软件源记录
---> Running in 763e4493d93f
# 移除临时容器 763e4493d93f
Removing intermediate container 763e4493d93f
# 镜像 6297f20605d9
---> 6297f20605d9
Step 5/7 : RUN apt update >/dev/null 2>&1
# 运行容器 2665a7e5a2e9,在容器内运行上面的这个命令, 更新软件源缓存
---> Running in 2665a7e5a2e9
# 移除临时容器 2665a7e5a2e9
Removing intermediate container 2665a7e5a2e9
# 镜像 fdfed940ca4d
---> fdfed940ca4d
Step 6/7 : RUN apt install -y >/dev/null 2>&1
# 运行 容器 722a9a544643,在容器内运行上面的这个命令, 安装
---> Running in 722a9a544643
# 移除临时容器 722a9a544643
Removing intermediate container 722a9a544643
# 镜像 6ee76f7df9e5
---> 6ee76f7df9e5
Step 7/7 : EXPOSE 80
# 运行容器 a12ed3216ee0,在容器内运行上面的这个命令, 暴露80端口
---> Running in a12ed3216ee0
# 移除临时容器 a12ed3216ee0
Removing intermediate container a12ed3216ee0
# 最终的镜像 7cf64279ba98
---> 7cf64279ba98
Successfully built 7cf64279ba98
# 将这个镜像命名 ubuntu- 版本号v1
Successfully tagged ubuntu-:v1
结合之前讲到的 docker commit 命令,不难理解 Dockerfile 就是将我们在中书写的构建指令,一层一层从 FROM 指定的基础镜像使用临时容器过渡,逐层叠加起来最终目标镜像。
使用 docker history ubuntu-:v1
命令可以验证我们的想法:
[user@centos8 ]$ docker history ubuntu-:v1
IMAGE CREATED CREATED BY SIZE COMMENT
7cf64279ba98 21 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
6ee76f7df9e5 21 minutes ago /bin/sh -c apt install -y >/dev/null 2… 59.2MB
fdfed940ca4d 21 minutes ago /bin/sh -c apt update >/dev/null 2>&1 21.4MB
6297f20605d9 21 minutes ago /bin/sh -c sed -i 's/s.ubuntu.com/mir… 2.76kB
83f29f7b055a 21 minutes ago /bin/sh -c sed -i 's/archive.ubuntu.com/mirr… 2.76kB
6679d1c204e3 21 minutes ago /bin/sh -c #(nop) MAINTAINER my_name myemai… 0B
1d622ef86b13 2 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 weeks ago /bin/sh -c mkdir -p /run/syd && echo 'do… 7B
<missing> 2 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
<missing> 2 weeks ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 1.01MB
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:a58c8b447951f9e30… 72.8MB
Dockerfile 主要包含四部分:
我们可以将 Dockerfile 理解为由上往下执行指令的脚本。 当我们构建命令,通过我们给出的 Dockerfile 构建镜像时,Docker 按照 指令顺序进行对应的操作。
Dockerfile 是熟练掌握 Docker 的必备技能,它的优点有很多:
Dockerfile 的体积小,容易进行部署;
环境构建流程记录了 Dockerfile 中,能够直观的看到镜像构建的顺序和逻辑。
下一节,我们将深入学习 Dockerfile 常用的构建指令。