gpt4 book ai didi

Docker 将文件从 dockerfile 复制到主机

转载 作者:IT老高 更新时间:2023-10-28 21:24:04 26 4
gpt4 key购买 nike

我正在编写一个 Dockerfile 来构建一个应用程序。我知道您可以使用

手动将文件复制出容器
docker cp <containerId>:/file/path/within/container /host/path/target

(参见 Copying files from Docker container to host ),但我只想运行

docker build

并让它将我的 Dockerfile 创建的构建工件转储到我的主机文件系统的某个位置。

我可以在 Dockerfile 中使用命令从容器中复制到主机中吗?像 COPY 的反义词?

最佳答案

根据 Docker 文档,添加了 API 版本 1.40

Custom build outputs

满足您的需求:

By default, a local container image is created from the build result. The --output (or -o) flag allows you to override this behavior, and a specify a custom exporter. For example, custom exporters allow you to export the build artifacts as files on the local filesystem instead of a Docker image, which can be useful for generating local binaries, code generation etc.

例如

$ docker build --output type=tar,dest=out.tar .

多阶段Dockerfile

FROM golang AS build-stage
RUN go get -u github.com/LK4D4/vndr

FROM scratch AS export-stage
COPY --from=build-stage /go/bin/vndr /

The --output option exports all files from the target stage. A common pattern for exporting only specific files is to do multi-stage builds and to copy the desired files to a new scratch stage with COPY --from.

关于Docker 将文件从 dockerfile 复制到主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49638532/

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