重建 Docker 镜像需要时间

时间:2021-07-11 05:37:50

标签: docker dockerfile

Dockerfile 有如下内容,

FROM node:16.4.2-alpine3.14

WORKDIR /app

COPY package.json .

COPY . /app

并运行以下构建命令

docker build -t app:0.1 .

花了28.4 秒,下面是终端日志,

[+] Building 28.4s (10/10) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                     0.1s
 => => transferring dockerfile: 124B                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                        0.1s
 => => transferring context: 53B                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/node:16.4.2-alpine3.14                                                                               17.1s
 => [auth] library/node:pull token for registry-1.docker.io                                                                                              0.0s
 => [internal] load build context                                                                                                                        0.1s
 => => transferring context: 2.01MB                                                                                                                      0.0s
 => [1/4] FROM docker.io/library/node:16.4.2-alpine3.14@sha256:fabfca5e7dcb339097f998d6ef11c53dd80a3f99ed5cecc005e93d0ff6d4bda9                          9.9s
 => => resolve docker.io/library/node:16.4.2-alpine3.14@sha256:fabfca5e7dcb339097f998d6ef11c53dd80a3f99ed5cecc005e93d0ff6d4bda9                          0.0s
 => => sha256:fabfca5e7dcb339097f998d6ef11c53dd80a3f99ed5cecc005e93d0ff6d4bda9 1.00kB / 1.00kB                                                           0.0s
 => => sha256:75dec02064547a8ec570f2953e8d68a1674ad3f37730160f1570cce077be9ed0 1.16kB / 1.16kB                                                           0.0s
 => => sha256:40cb916373b08a087466d2e72402d0b3a4587fd3e9135169498cf0db4ff42a88 6.53kB / 6.53kB                                                           0.0s
 => => sha256:5843afab387455b37944e709ee8c78d7520df80f8d01cf7f861aae63beeddb6b 2.81MB / 2.81MB                                                           0.8s
 => => sha256:c118dce16b0057d713fc98e31606a84e4348fa2c967eaf1bb5fd21ba42825956 35.55MB / 35.55MB                                                         7.1s
 => => sha256:aef8e8137ac43c8199343c96874993063af6584260f22b15e99f735cce5de653 2.35MB / 2.35MB                                                           2.6s
 => => extracting sha256:5843afab387455b37944e709ee8c78d7520df80f8d01cf7f861aae63beeddb6b                                                                0.2s
 => => sha256:ad336e0e52b8dfc38c23599663deb060b1ac169d548dec8072ead94712f708be 281B / 281B                                                               2.0s
 => => extracting sha256:c118dce16b0057d713fc98e31606a84e4348fa2c967eaf1bb5fd21ba42825956                                                                2.0s
 => => extracting sha256:aef8e8137ac43c8199343c96874993063af6584260f22b15e99f735cce5de653                                                                0.2s
 => => extracting sha256:ad336e0e52b8dfc38c23599663deb060b1ac169d548dec8072ead94712f708be                                                                0.0s
 => [2/4] WORKDIR /app                                                                                                                                   0.6s
 => [3/4] COPY package.json .                                                                                                                            0.1s
 => [4/4] COPY . /app                                                                                                                                    0.1s
 => exporting to image                                                                                                                                   0.2s
 => => exporting layers                                                                                                                                  0.2s
 => => writing image sha256:91d93eddff55cba6bd8b72144b7320e025de93e9865177ff584c75b94d1bafc1                                                             0.0s
 => => naming to docker.io/library/app:0.1

当我再次运行相同的构建命令时,需要 14.6 秒

但是,如果我拉 node:16.4.2-alpine3.14 使用,

 docker pull node:16.4.2-alpine3.14

然后运行build命令,那么build只需要0.3秒

我认为,当我们构建映像时,也会下载依赖项,这就是为什么花费的时间从 28.4 减少到 14.6 秒的原因。但为什么还要14.6 秒?它应该短至 0.3 秒。

为什么会这样?我错过了什么?

1 个答案:

答案 0 :(得分:1)

以下可能是需要 18.4 秒的原因:

  1. Docker 客户端将整个构建上下文带到 docker 守护进程。构建上下文是 Dockerfile 的整个目录。某些文件或文件夹可能会占用大量空间,例如 {{ 1}}。对此的补救措施是将不需要的文件(例如 .git、node_module、日志文件)添加到 node_modules 文件中,以使 Docker 忽略某些文件。
  2. DNS 解析。使用 .dockerignore 命令检查解决 docker 注册表需要多长时间。
  3. 为您的构建启用构建套件将有助于缩短构建时间。在调用 docker build 命令时设置 dig 环境变量,例如:
<块引用>

DOCKER_BUILDKIT=1

请看这个link for more information