如何从 docker 镜像复制文件 - dockerfile cmd

Mar*_*ry1 8 mount docker dockerfile

在构建期间,我想将文件从图像(从文件夹/opt/myApp/myFile.xml)复制到我的主机文件夹/opt/temp 在 Dockerfile 中,我使用 --mount 如下,尝试安装到我的本地测试文件夹:

RUN --mount=target=/opt/temp,type=bind,source=test cp /opt/myApp/myFile.xml /opt/temp

我成功构建了图像,但本地test文件夹是空的,有什么想法吗?

Neo*_*son 6

不支持在构建时将文件从映像复制到主机。
\n这可以在运行时使用卷轻松实现。

\n

但是,如果您确实想通过各种方式解决此问题,您可以查看自定义构建输出文档,该文档引入了对此类活动的支持。
\n

\n

这是一个受官方文档启发的简单示例:

\n

Dockerfile

\n
FROM alpine AS stage-a\nRUN mkdir -p /opt/temp/\nRUN touch /opt/temp/file-created-at-build-time\nRUN echo "Content added at build-time" > /opt/temp/file-created-at-build-time\n\nFROM scratch as custom-exporter\nCOPY --from=stage-a /opt/temp/file-created-at-build-time .\n
Run Code Online (Sandbox Code Playgroud)\n

为此,您需要使用以下参数启动构建命令:

\n
DOCKER_BUILDKIT=1 docker build --output out .\n
Run Code Online (Sandbox Code Playgroud)\n

这将在您的主机上除了 Dockerfile 之外创建一个out包含您需要的文件的目录:

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 out\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file-created-at-build-time\n
Run Code Online (Sandbox Code Playgroud)\n
cat out/file-created-at-build-time \nContent added at build-time\n\n
Run Code Online (Sandbox Code Playgroud)\n