为什么 Google Cloud Build 创建中间容器?

Why Google Cloud Build creates intermediate containers?

有人可以向我解释为什么 Google Cloud Build 会为 运行 命令创建中间容器吗?

Step #0 - "Build": Step 2/25 : ARG NODE_ENV
Step #0 - "Build":  ---> Running in 17281bea0e29
Step #0 - "Build": Removing intermediate container 17281bea0e29
Step #0 - "Build":  ---> a43229632036
Step #0 - "Build": Step 3/25 : ARG DB_NAME
Step #0 - "Build":  ---> Running in 04a199971761
Step #0 - "Build": Removing intermediate container 04a199971761
Step #0 - "Build":  ---> 3f15b2ad5662
Step #0 - "Build": Step 4/25 : ARG DB_USER
Step #0 - "Build":  ---> Running in 4fb95096aab1
Step #0 - "Build": Removing intermediate container 4fb95096aab1
Step #0 - "Build":  ---> 97c619b21472
Step #0 - "Build": Step 5/25 : ARG DB_PASSWORD
Step #0 - "Build":  ---> Running in 2280e3095a20
Step #0 - "Build": Removing intermediate container 2280e3095a20
Step #0 - "Build":  ---> a79acad9a411
Step #0 - "Build": Step 6/25 : ARG CLOUD_SQL_CONNECTION_NAME
Step #0 - "Build":  ---> Running in 1f63bde588f0
Step #0 - "Build": Removing intermediate container 1f63bde588f0
Step #0 - "Build":  ---> 8e22be5c6191
Step #0 - "Build": Step 7/25 : ENV NODE_ENV ${NODE_ENV}
Step #0 - "Build":  ---> Running in 6f5495791d72
Step #0 - "Build": Removing intermediate container 6f5495791d72
Step #0 - "Build":  ---> 2413826fa5f6
Step #0 - "Build": Step 8/25 : ENV DB_NAME ${DB_NAME}
Step #0 - "Build":  ---> Running in e3099226a900
Step #0 - "Build": Removing intermediate container e3099226a900
Step #0 - "Build":  ---> 68f11d7cee19
Step #0 - "Build": Step 9/25 : ENV DB_USER ${DB_USER}
Step #0 - "Build":  ---> Running in c1c3dc6ca115
Step #0 - "Build": Removing intermediate container c1c3dc6ca115
Step #0 - "Build":  ---> 5fd5610b2de5
Step #0 - "Build": Step 10/25 : ENV DB_PASSWORD ${DB_PASSWORD}
Step #0 - "Build":  ---> Running in 67c6ed7a56e7
Step #0 - "Build": Removing intermediate container 67c6ed7a56e7
Step #0 - "Build":  ---> 7c8ecb080c59
Step #0 - "Build": Step 11/25 : ENV CLOUD_SQL_CONNECTION_NAME ${CLOUD_SQL_CONNECTION_NAME}
Step #0 - "Build":  ---> Running in 8dddcddd80c0
Step #0 - "Build": Removing intermediate container 8dddcddd80c0
Step #0 - "Build":  ---> 789cc6e25b55
Step #0 - "Build": Step 12/25 : WORKDIR /usr/src/app
Step #0 - "Build":  ---> Running in 2c5e9083a8a5
Step #0 - "Build": Removing intermediate container 2c5e9083a8a5

我对每个步骤后的 Removing intermediate container ... 感兴趣。它会影响构建的性能吗?

cloudbuild.yml

steps:
  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - '--build-arg'
      - NODE_ENV=$_NODE_ENV
      - '--build-arg'
      - DB_NAME=$_DB_NAME
      - '--build-arg'
      - DB_USER=$_DB_USER
      - '--build-arg'
      - DB_PASSWORD=$$DB_PASSWORD
      - '--build-arg'
      - CLOUD_SQL_CONNECTION_NAME=$_CLOUD_SQL_CONNECTION_NAME
      - '--no-cache'
      - '-t'
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - .
      - '-f'
      - Dockerfile
    id: Build
    secretEnv:
      - DB_PASSWORD
  - name: gcr.io/cloud-builders/docker
...

Cloud Build 使用 Docker 执行构建。要了解 Cloud Build 为什么创建中间容器,首先您必须了解 Docker 构建过程。

对于每个构建步骤,Cloud Build 都会执行一个 Docker 容器作为 docker run 的实例。每个步骤都在一个中间容器中处理。

“那些中间容器可以成功也可以失败。如果成功,则将中间容器与上次成功构建步骤的图像合并,然后删除中间容器。”

从性能的角度来看,删除直接容器是构建过程的一部分,它有助于减小容器映像的大小。

已经有一些现有文章进一步解释了 Docker 构建过程。这里有一些有趣的链接: