如何在多阶段 docker 构建中跳过不相关的步骤
How to skip irrelevant steps on multi-stage docker build
我正在尝试 运行 一个包含两个并行步骤(test
和 release
)的多步骤构建过程,并且 运行 解决 [=80] 的问题=] Cloud Build 在 运行 目标多阶段时不会跳过不相关的中间步骤。
Docker 文件
FROM node:18-slim AS base
WORKDIR /app
COPY package.json yarn.lock /app/
ADD src/ /app/src
RUN yarn install
FROM base AS test
RUN yarn test
FROM base AS release
EXPOSE 3000
ENTRYPOINT node src/index.js
cloudbuild.json
{
"steps": [
{
"name": "gcr.io/cloud-builders/docker",
"id": "Install",
"args": [
"build",
"--target",
"base",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-install:${SHORT_SHA}",
"."
]
},
{
"name": "gcr.io/cloud-builders/docker",
"id": "Test",
"args": [
"build",
"--target",
"test",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-test:${SHORT_SHA}",
"."
],
"waitFor": ["Install"]
},
{
"name": "gcr.io/cloud-builders/docker",
"id": "Build",
"args": [
"build",
"--target",
"release",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}:${SHORT_SHA}",
"."
],
"waitFor": ["Install"]
}
]
}
步骤结果:
- 安装:仅运行来自 Dockerfile 的
base
步骤。
- 测试:在利用安装步骤的缓存后,只运行来自Dockerfile的
test
步骤。
- Build:在利用来自 Install 步骤的缓存后,从 Dockerfile 运行
test
和 release
步骤.
我的预期结果是:
- 安装:仅运行来自 Dockerfile 的
base
步骤。
- 测试:在利用安装步骤的缓存后,只运行来自Dockerfile的
test
步骤。
- Build:在利用来自 Install 步骤的缓存后,仅运行来自 Dockerfile 的
release
步骤。
我是否以任何方式配置错误或 Google Cloud Build 不支持跳过步骤作为目标多阶段构建的一部分?
编辑:
由于提供了 Answer:
,以下 cloudbuild 配置有效
{
"steps": [
{
"name": "gcr.io/cloud-builders/docker",
"id": "Install",
"args": [
"build",
"--target",
"base",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-install:${SHORT_SHA}",
"."
],
"env": ["DOCKER_BUILDKIT=1"]
},
{
"name": "gcr.io/cloud-builders/docker",
"id": "Test",
"args": [
"build",
"--target",
"test",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-test:${SHORT_SHA}",
"."
],
"env": ["DOCKER_BUILDKIT=1"],
"waitFor": ["Install"]
},
{
"name": "gcr.io/cloud-builders/docker",
"id": "Build",
"args": [
"build",
"--target",
"release",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}:${SHORT_SHA}",
"."
],
"env": ["DOCKER_BUILDKIT=1"],
"waitFor": ["Install"]
}
]
}
构建阶段的依赖图是buildkit的一个特性。默认情况下,google 的云构建器镜像仍在使用经典的 docker 构建器,但您可以切换到 buildkit:
env:
- "DOCKER_BUILDKIT=1"
在您的构建步骤中。然而,最后检查,upgrading the docker engine is still pending.
我正在尝试 运行 一个包含两个并行步骤(test
和 release
)的多步骤构建过程,并且 运行 解决 [=80] 的问题=] Cloud Build 在 运行 目标多阶段时不会跳过不相关的中间步骤。
Docker 文件
FROM node:18-slim AS base
WORKDIR /app
COPY package.json yarn.lock /app/
ADD src/ /app/src
RUN yarn install
FROM base AS test
RUN yarn test
FROM base AS release
EXPOSE 3000
ENTRYPOINT node src/index.js
cloudbuild.json
{
"steps": [
{
"name": "gcr.io/cloud-builders/docker",
"id": "Install",
"args": [
"build",
"--target",
"base",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-install:${SHORT_SHA}",
"."
]
},
{
"name": "gcr.io/cloud-builders/docker",
"id": "Test",
"args": [
"build",
"--target",
"test",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-test:${SHORT_SHA}",
"."
],
"waitFor": ["Install"]
},
{
"name": "gcr.io/cloud-builders/docker",
"id": "Build",
"args": [
"build",
"--target",
"release",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}:${SHORT_SHA}",
"."
],
"waitFor": ["Install"]
}
]
}
步骤结果:
- 安装:仅运行来自 Dockerfile 的
base
步骤。 - 测试:在利用安装步骤的缓存后,只运行来自Dockerfile的
test
步骤。 - Build:在利用来自 Install 步骤的缓存后,从 Dockerfile 运行
test
和release
步骤.
我的预期结果是:
- 安装:仅运行来自 Dockerfile 的
base
步骤。 - 测试:在利用安装步骤的缓存后,只运行来自Dockerfile的
test
步骤。 - Build:在利用来自 Install 步骤的缓存后,仅运行来自 Dockerfile 的
release
步骤。
我是否以任何方式配置错误或 Google Cloud Build 不支持跳过步骤作为目标多阶段构建的一部分?
编辑: 由于提供了 Answer:
,以下 cloudbuild 配置有效{
"steps": [
{
"name": "gcr.io/cloud-builders/docker",
"id": "Install",
"args": [
"build",
"--target",
"base",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-install:${SHORT_SHA}",
"."
],
"env": ["DOCKER_BUILDKIT=1"]
},
{
"name": "gcr.io/cloud-builders/docker",
"id": "Test",
"args": [
"build",
"--target",
"test",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-test:${SHORT_SHA}",
"."
],
"env": ["DOCKER_BUILDKIT=1"],
"waitFor": ["Install"]
},
{
"name": "gcr.io/cloud-builders/docker",
"id": "Build",
"args": [
"build",
"--target",
"release",
"-t",
"australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}:${SHORT_SHA}",
"."
],
"env": ["DOCKER_BUILDKIT=1"],
"waitFor": ["Install"]
}
]
}
构建阶段的依赖图是buildkit的一个特性。默认情况下,google 的云构建器镜像仍在使用经典的 docker 构建器,但您可以切换到 buildkit:
env:
- "DOCKER_BUILDKIT=1"
在您的构建步骤中。然而,最后检查,upgrading the docker engine is still pending.