为什么 docker-compose 构建 运行 我的步骤两次?
Why does docker-compose build run my steps twice?
我正在使用像这样的 Dockerfile 进行多阶段构建:
#####################################
## Build the client
#####################################
FROM node:12.19.0 as web-client-builder
WORKDIR /workspace
COPY web-client/package*.json ./
# Running npm install before we update our source allows us to take advantage
# of docker layer caching. We are excluding node_modules in .dockerignore
RUN npm ci
COPY web-client/ ./
RUN npm run test:ci
RUN npm run build
#####################################
## Host the client on a static server
#####################################
FROM nginx:1.19 as web-client
COPY --from=web-client-builder /workspace/nginx-templates /etc/nginx/templates/
COPY --from=web-client-builder /workspace/nginx.conf /etc/nginx/nginx.conf
COPY --from=web-client-builder /workspace/build /var/www/
#####################################
## Build the server
#####################################
FROM openjdk:11-jdk-slim as server-builder
WORKDIR /workspace
COPY build.gradle settings.gradle gradlew ./
COPY gradle ./gradle
COPY server/ ./server/
RUN ./gradlew --no-daemon :server:build
#####################################
## Start the server
#####################################
FROM openjdk:11-jdk-slim as server
WORKDIR /app
ARG JAR_FILE=build/libs/*.jar
COPY --from=server-builder /workspace/server/$JAR_FILE ./app.jar
ENTRYPOINT ["java","-jar","/app/app.jar"]
我也有一个这样的docker-compose.yml:
version: "3.8"
services:
server:
restart: always
container_name: server
build:
context: .
dockerfile: Dockerfile
target: server
image: server
ports:
- "8090:8080"
web-client:
restart: always
container_name: web-client
build:
context: .
dockerfile: Dockerfile
target: web-client
image: web-client
environment:
- LISTEN_PORT=80
ports:
- "8091:80"
这里涉及到的两张图片,web-client
和server
是完全独立的。我想利用多阶段构建并行化。
当我 运行 docker-compose build
(我在 docker-compose 1.27.4)时,我得到这样的输出
λ docker-compose build
Building server
Step 1/24 : FROM node:12.19.0 as web-client-builder
---> 1f560ce4ce7e
... etc ...
Step 6/24 : RUN npm run test:ci
---> Running in e9189b2bff1d
... Runs tests ...
... etc ...
Step 24/24 : ENTRYPOINT ["java","-jar","/app/app.jar"]
---> Using cache
---> 2ebe48e3b06e
Successfully built 2ebe48e3b06e
Successfully tagged server:latest
Building web-client
Step 1/11 : FROM node:12.19.0 as web-client-builder
---> 1f560ce4ce7e
... etc ...
Step 6/11 : RUN npm run test:ci
---> Using cache
---> 0f205b9549e0
... etc ...
Step 11/11 : COPY --from=web-client-builder /workspace/build /var/www/
---> Using cache
---> 31c4eac8c06e
Successfully built 31c4eac8c06e
Successfully tagged web-client:latest
请注意我的测试 (npm run test:ci
) 运行 两次(针对服务器目标的步骤 6/24,然后针对 Web 客户端目标的步骤 6/11)。我想了解为什么会发生这种情况,但我想这不是一个大问题,因为至少它在第二次进行测试时已被缓存。
当我尝试 运行 我的并行构建时,这会成为一个更大的问题。现在我得到这样的输出:
λ docker-compose build --parallel
Building server ...
Building web-client ...
Building server
Building web-client
Step 1/11 : FROM node:12.19.0 as web-client-builderStep 1/24 : FROM node:12.19.0 as web-client-builder
---> 1f560ce4ce7e
... etc ...
Step 6/24 : RUN npm run test:ci
---> e96afb9c14bf
Step 6/11 : RUN npm run test:ci
---> Running in c17deba3c318
---> Running in 9b0faf487a7d
> web-client@0.1.0 test:ci /workspace
> react-scripts test --ci --coverage --reporters=default --reporters=jest-junit --watchAll=false
> web-client@0.1.0 test:ci /workspace
> react-scripts test --ci --coverage --reporters=default --reporters=jest-junit --watchAll=false
... Now my tests run in parallel twice, and the output is interleaved for both parallel runs ...
很明显,测试现在 运行ning 两次,因为现在我运行并行构建,它们没有机会缓存。
谁能帮我理解一下?我认为 docker 多阶段构建的优点之一是它们是可并行的,但这种行为对我来说没有意义。我误会了什么?
备注
我还尝试为 docker-compose 启用 BuildKit。我很难理解输出。我不相信它是 运行ning 两次,但我也不确定它是并行化的。我需要深入研究它,但我的主要问题是:我希望理解为什么多阶段构建不会 运行 以我预期的方式并行 without BuildKit.
why multi-stage builds don't run in parallel in the way I expected without BuildKit.
这是 BuildKit 的最高点。
Docker 中多阶段的主要目的是通过仅保留应用程序正常工作所需的内容来生成较小的图像。例如
FROM node as builder
COPY package.json package-lock.json
RUN npm ci
COPY . /app
RUN npm run build
FROM nginx
COPY --from=/app/dist --chown=nginx /app/dist /var/www
构建项目所需的所有开发工具根本没有复制到最终图像中。这转化为更小的最终图像。
编辑:
BuildKit builds are based on a binary intermediate format called LLB that is used for defining the dependency graph for processes running part of your build. tl;dr: LLB is to Dockerfile what LLVM IR is to C.
换句话说,BuildKit 能够评估每个阶段的依赖关系,从而允许并行执行。
您可以将其拆分为两个单独的 Dockerfile。我可能会写一个 web-client/Dockerfile
包含前两个阶段(将相对 COPY
路径更改为 ./
),并留下 root-directory Dockerfile
来构建服务器应用程序.然后您的 docker-compose.yml
文件可以指向这些单独的目录:
services:
server:
build: . # equivalent to {context: ., dockerfile: Dockerfile}
web-client:
build: web-client
正如@Stefano 在 中指出的那样,multi-stage 构建围绕构建单个最终图像进行了更多优化,并且在“经典”构建器中,它们总是从一开始就 运行通过指定的目标阶段,没有任何关于从哪里开始的特定逻辑。
我正在使用像这样的 Dockerfile 进行多阶段构建:
#####################################
## Build the client
#####################################
FROM node:12.19.0 as web-client-builder
WORKDIR /workspace
COPY web-client/package*.json ./
# Running npm install before we update our source allows us to take advantage
# of docker layer caching. We are excluding node_modules in .dockerignore
RUN npm ci
COPY web-client/ ./
RUN npm run test:ci
RUN npm run build
#####################################
## Host the client on a static server
#####################################
FROM nginx:1.19 as web-client
COPY --from=web-client-builder /workspace/nginx-templates /etc/nginx/templates/
COPY --from=web-client-builder /workspace/nginx.conf /etc/nginx/nginx.conf
COPY --from=web-client-builder /workspace/build /var/www/
#####################################
## Build the server
#####################################
FROM openjdk:11-jdk-slim as server-builder
WORKDIR /workspace
COPY build.gradle settings.gradle gradlew ./
COPY gradle ./gradle
COPY server/ ./server/
RUN ./gradlew --no-daemon :server:build
#####################################
## Start the server
#####################################
FROM openjdk:11-jdk-slim as server
WORKDIR /app
ARG JAR_FILE=build/libs/*.jar
COPY --from=server-builder /workspace/server/$JAR_FILE ./app.jar
ENTRYPOINT ["java","-jar","/app/app.jar"]
我也有一个这样的docker-compose.yml:
version: "3.8"
services:
server:
restart: always
container_name: server
build:
context: .
dockerfile: Dockerfile
target: server
image: server
ports:
- "8090:8080"
web-client:
restart: always
container_name: web-client
build:
context: .
dockerfile: Dockerfile
target: web-client
image: web-client
environment:
- LISTEN_PORT=80
ports:
- "8091:80"
这里涉及到的两张图片,web-client
和server
是完全独立的。我想利用多阶段构建并行化。
当我 运行 docker-compose build
(我在 docker-compose 1.27.4)时,我得到这样的输出
λ docker-compose build
Building server
Step 1/24 : FROM node:12.19.0 as web-client-builder
---> 1f560ce4ce7e
... etc ...
Step 6/24 : RUN npm run test:ci
---> Running in e9189b2bff1d
... Runs tests ...
... etc ...
Step 24/24 : ENTRYPOINT ["java","-jar","/app/app.jar"]
---> Using cache
---> 2ebe48e3b06e
Successfully built 2ebe48e3b06e
Successfully tagged server:latest
Building web-client
Step 1/11 : FROM node:12.19.0 as web-client-builder
---> 1f560ce4ce7e
... etc ...
Step 6/11 : RUN npm run test:ci
---> Using cache
---> 0f205b9549e0
... etc ...
Step 11/11 : COPY --from=web-client-builder /workspace/build /var/www/
---> Using cache
---> 31c4eac8c06e
Successfully built 31c4eac8c06e
Successfully tagged web-client:latest
请注意我的测试 (npm run test:ci
) 运行 两次(针对服务器目标的步骤 6/24,然后针对 Web 客户端目标的步骤 6/11)。我想了解为什么会发生这种情况,但我想这不是一个大问题,因为至少它在第二次进行测试时已被缓存。
当我尝试 运行 我的并行构建时,这会成为一个更大的问题。现在我得到这样的输出:
λ docker-compose build --parallel
Building server ...
Building web-client ...
Building server
Building web-client
Step 1/11 : FROM node:12.19.0 as web-client-builderStep 1/24 : FROM node:12.19.0 as web-client-builder
---> 1f560ce4ce7e
... etc ...
Step 6/24 : RUN npm run test:ci
---> e96afb9c14bf
Step 6/11 : RUN npm run test:ci
---> Running in c17deba3c318
---> Running in 9b0faf487a7d
> web-client@0.1.0 test:ci /workspace
> react-scripts test --ci --coverage --reporters=default --reporters=jest-junit --watchAll=false
> web-client@0.1.0 test:ci /workspace
> react-scripts test --ci --coverage --reporters=default --reporters=jest-junit --watchAll=false
... Now my tests run in parallel twice, and the output is interleaved for both parallel runs ...
很明显,测试现在 运行ning 两次,因为现在我运行并行构建,它们没有机会缓存。
谁能帮我理解一下?我认为 docker 多阶段构建的优点之一是它们是可并行的,但这种行为对我来说没有意义。我误会了什么?
备注 我还尝试为 docker-compose 启用 BuildKit。我很难理解输出。我不相信它是 运行ning 两次,但我也不确定它是并行化的。我需要深入研究它,但我的主要问题是:我希望理解为什么多阶段构建不会 运行 以我预期的方式并行 without BuildKit.
why multi-stage builds don't run in parallel in the way I expected without BuildKit.
这是 BuildKit 的最高点。
Docker 中多阶段的主要目的是通过仅保留应用程序正常工作所需的内容来生成较小的图像。例如
FROM node as builder
COPY package.json package-lock.json
RUN npm ci
COPY . /app
RUN npm run build
FROM nginx
COPY --from=/app/dist --chown=nginx /app/dist /var/www
构建项目所需的所有开发工具根本没有复制到最终图像中。这转化为更小的最终图像。
编辑:
BuildKit builds are based on a binary intermediate format called LLB that is used for defining the dependency graph for processes running part of your build. tl;dr: LLB is to Dockerfile what LLVM IR is to C.
换句话说,BuildKit 能够评估每个阶段的依赖关系,从而允许并行执行。
您可以将其拆分为两个单独的 Dockerfile。我可能会写一个 web-client/Dockerfile
包含前两个阶段(将相对 COPY
路径更改为 ./
),并留下 root-directory Dockerfile
来构建服务器应用程序.然后您的 docker-compose.yml
文件可以指向这些单独的目录:
services:
server:
build: . # equivalent to {context: ., dockerfile: Dockerfile}
web-client:
build: web-client
正如@Stefano 在