如何将文件从 dockerfile 复制到主机? [英] How to copy files from dockerfile to host?

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

问题描述

我想在 dockerfile 构建成功后获取一些文件,但它不会将文件从容器复制到主机.

这意味着当我构建 dockerfile 时,文件已经是主机.

解决方案

支持将文件从 Dockerfile"复制到主机.Dockerfile 只是指定如何构建映像的配方.

构建时,您有机会将文件从主机复制到您正在构建的映像(使用 COPY 指令<代码>添加)

您还可以使用 docker cp(实际上cp也可以从宿主机复制到容器)

如果您想返回您的主机,一些可能在构建期间生成的文件(例如 调用脚本 生成 ssl),您可以运行容器,从主机挂载文件夹并执行 cp 命令.

例如查看这个 getcrt 脚本.

docker run -u root --entrypoint=/bin/sh --rm -i -v ${HOME}/b2d/apache:/apache apache <<<命令密码cp crt/apachecp键/apacheecho 将所有者从 $(id -u):$(id -g) 更改为 $(id -u):$(id -u)chown -R $(id -u):$(id -u)/apache/crtchown -R $(id -u):$(id -u)/apache/key命令

COMMANDS 之间的所有内容都是在容器上执行的命令,包括 cp 在主机上复制的命令 ${HOME}/b2d/apache 文件夹,使用 -v ${HOME}/b2d/apache:/apache 作为 /apache 安装在容器中.

这意味着每次你在容器中的 /apache 上复制任何内容时,实际上都是在主机上的 ${HOME}/b2d/apache 中复制!p>

I want to get some files when dockerfile built successfully, but it doesn't copy files from container to host.

That means when I have built dockerfile, the files already be host.

解决方案

Copying files "from the Dockerfile" to the host is not supported. The Dockerfile is just a recipe specifying how to build an image.

When you build, you have the opportunity to copy files from host to the image you are building (with the COPY directive or ADD)

You can also copy files from a container (an image that has been docker run'd) to the host with docker cp (atually, the cp can copy from the host to the container as well)

If you want to get back to your host some files that might have been generated during the build (like for example calling a script that generates ssl), you can run a container, mounting a folder from your host and executing cp commands.

See for example this getcrt script.

docker run -u root --entrypoint=/bin/sh --rm -i -v ${HOME}/b2d/apache:/apache apache << COMMANDS
pwd
cp crt /apache
cp key /apache
echo Changing owner from $(id -u):$(id -g) to $(id -u):$(id -u)
chown -R $(id -u):$(id -u) /apache/crt
chown -R $(id -u):$(id -u) /apache/key
COMMANDS

Everything between COMMANDS are commands executed on the container, including cp ones which are copying on the host ${HOME}/b2d/apache folder, mounted within the container as /apache with -v ${HOME}/b2d/apache:/apache.

That means each time you copy anything on /apache in the container, you are actually copying in ${HOME}/b2d/apache on the host!

这篇关于如何将文件从 dockerfile 复制到主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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