Docker将文件从dockerfile中复制到主机 [英] Docker copy file to host from within dockerfile

查看:141
本文介绍了Docker将文件从dockerfile中复制到主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a Dockerfile to build an application. I know that you can copy files out of the container manually with

docker cp <containerId>:/file/path/within/container /host/path/target

(请参阅将文件从Docker容器复制到主机),但我只想运行

(see Copying files from Docker container to host), but I would like to just run

docker build

并将其由我的 Dockerfile 创建的构建工件转储到主机文件系统上的某个地方.

and have it dump the build artifacts created by my Dockerfile somewhere on my host file system.

在Dockerfile中是否可以使用命令将其复制出容器并复制到主机中?就像 COPY 的反面一样?

Is there a command I can use within the Dockerfile to copy out of the container and into the host? Like the opposite of COPY?

推荐答案

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

As per Docker docs, API version 1.40 added

自定义构建输出

针对您的需求:

默认情况下,将从生成结果中创建本地容器映像.--output(或-o)标志可让您覆盖此行为,并使用指定一个自定义导出器.例如,自定义导出器允许您将构建工件导出为本地文件系统上的文件,而不是Docker映像,这对于生成本地二进制文件非常有用,代码生成等.

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 /

--output选项可导出目标阶段中的所有文件.普通的仅导出特定文件的模式是进行多阶段构建并使用COPY --from将所需文件复制到新的暂存阶段.

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中复制到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆