Docker - 如何将文件从图像复制到主机? [英] Docker - how can I copy a file from an image to a host?

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

问题描述

我的问题与关于从以下位置复制文件的这个问题有关容器到主机;我有一个 Dockerfile,它获取依赖项,从源代码编译构建工件,并运行一个可执行文件.我还想复制构建工件(在我的情况下,它是由 sbt dist 在 '../target/` 中生成的 .zip,但我认为这个问题也适用到罐子、二进制文件等.

My question is related to this question on copying files from containers to hosts; I have a Dockerfile that fetches dependencies, compiles a build artifact from source, and runs an executable. I also want to copy the build artifact (in my case it's a .zip produced by sbt dist in '../target/`, but I think this question also applies to jars, binaries, etc.

docker cp 适用于容器,而不是图像;我是否需要启动一个容器才能从中取出文件?在脚本中,我尝试在后台以交互模式运行 /bin/bash,将文件复制出来,然后杀死容器,但这似乎很笨拙.有没有更好的办法?

docker cp works on containers, not images; do I need to start a container just to get a file out of it? In a script, I tried running /bin/bash in interactive mode in the background, copying the file out, and then killing the container, but this seems kludgey. Is there a better way?

另一方面,我想避免在运行 docker save $IMAGENAME 后解压 .tar 文件只是为了取出一个文件(但这似乎是最简单的,如果最慢,现在的选项).

On the other hand, I would like to avoid unpacking a .tar file after running docker save $IMAGENAME just to get one file out (but that seems like the simplest, if slowest, option right now).

我会使用 docker 卷,例如:

I would use docker volumes, e.g.:

docker run -v hostdir:out $IMAGENAME /bin/cp/../blah.zip /out

但我在 OSX 中运行 boot2docker 并且我不知道如何直接写入我的 mac 主机文件系统(读写卷安装在我的 boot2docker VM 中,这意味着我可以't 与他人轻松共享从图像中提取 blah.zip 的脚本.有想法吗?

but I'm running boot2docker in OSX and I don't know how to directly write to my mac host filesystem (read-write volumes are mounting inside my boot2docker VM, which means I can't easily share a script to extract blah.zip from an image with others. Thoughts?

推荐答案

要从图像复制文件,请创建一个临时容器,从中复制文件,然后将其删除:

To copy a file from an image, create a temporary container, copy the file from it and then delete it:

id=$(docker create image-name)
docker cp $id:path - > local-tar-file
docker rm -v $id

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

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