gpt4 book ai didi

docker - 是否可以在不运行所有先前构建阶段的情况下在 docker 中运行目标构建阶段

转载 作者:行者123 更新时间:2023-12-02 17:48:59 24 4
gpt4 key购买 nike

我是 docker 的新手,正在尝试探索多阶段构建。我想在 docker 上运行特定阶段 docker build -t build-stage-tag --target build
我希望它运行以下阶段 dependencies --> compile --> build并跳过 test .但碰巧它也运行测试阶段。

让我知道我对多阶段构建的理解 --target是错误的,或者我的 docker 文件中有一些错误。

我想做的是运行 build不运行 test 的阶段反之亦然。

这是我的 Dockerfile 的外观:

# Pull base image
FROM openjdk:8u171 as dependencies

# Install Scala
## Piping curl directly in tar
// do some stuff

# Copy source into container
COPY . /usr/src/app

FROM dependencies as compile
WORKDIR /usr/src/app
# Test and build the jar in the same step to save time
RUN sbt -Dsbt.log.noformat=true compile
RUN sbt -Dsbt.log.noformat=true assembly

FROM compile as test
WORKDIR /usr/src/app
RUN sbt -Dsbt.log.noformat=true -Dtest_db_user=root -Dtest_db_password=password -Dtest_db_host=localhost coverage test coverageReport

FROM compile as build

# Define working directory
WORKDIR /root

COPY --from=compile /usr/src/push/target/scala-2.12/app-assembly-?*.?*.?*.jar ./push.jar
COPY --from=compile /usr/src/push/config/jvm.config ./jvm.config
COPY --from=compile /usr/src/push/entrypoint.sh /bin/entrypoint.sh

RUN chmod +x /bin/entrypoint.sh
ENTRYPOINT ["/bin/entrypoint.sh"]
CMD ["docker", "blah"]

最佳答案

套装DOCKER_BUILDKIT=1使用 buildkit 的环境变量,如下所示:

DOCKER_BUILDKIT=1 docker build -t build-stage-tag --target build -<<EOF
FROM alpine as base
RUN echo "running BASE commands"

FROM base AS test
RUN echo "running TEST commands"

FROM base AS build
RUN echo "running BUILD commands"
EOF

输出:
[+] Building 4.4s (7/7) FINISHED
=> [internal] load .dockerignore 0.5s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.3s
=> => transferring dockerfile: 204B 0.0s
=> [internal] load metadata for docker.io/library/alpine:latest 0.0s
=> [base 1/2] FROM docker.io/library/alpine 0.1s
=> => resolve docker.io/library/alpine:latest 0.0s
=> [base 2/2] RUN echo "running BASE commands" 1.4s
=> [build 1/1] RUN echo "running BUILD commands" 1.5s
=> exporting to image 0.7s
=> => exporting layers 0.6s
=> => writing image sha256:c6958c8bb64b1c6d5a975d8fa4b68c713ee5b374ba9a9fa00f8a0b9b5b314d5e 0.0s
=> => naming to docker.io/library/build-stage-tag 0.0s

关于docker - 是否可以在不运行所有先前构建阶段的情况下在 docker 中运行目标构建阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52615254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com