Docker在构建时使用卷进行构建 [英] Docker build using volumes at build time

查看:88
本文介绍了Docker在构建时使用卷进行构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在docker映像构建期间是否可以使用外部卷?

Is there a way to use external volumes during the docker image build ?

我有一种情况,我想在docker映像构建期间使用外部卷中的配置docker映像构建时间。

I have a situation where I would like to use a configuration inside a external volume during the docker image build time. Is that possible?

推荐答案

如果通过 docker image build,您的意思是运行一个 docker build ...命令:不,没有办法(至少,在我阅读的最新文档中没有)。但是,没有什么可以阻止您使用直接的docker命令执行需要外部卷的步骤,然后提交容器并像 docker build那样对其进行标记。假设这是构建的最后一步,请将所有其他不需要卷的命令放入Dockerfile,然后执行以下操作:

If by 'docker image build' you mean running a single 'docker build ...' command: no, there is no way to do that (at least, not in the most recent documentation that I have read). However, nothing prevents you from performing the step that needs the external volume using direct docker commands and then commit the container and tag it just as 'docker build' would. Assuming this is the last step in your build, put all other commands (that don't need the volume) into a Dockerfile and then do this:

tmp_img=`docker build .`
tmp_container=`docker run -v $my_ext_volume:$my_mount_path --entrypoint=(your volume-dependent build command here) $tmp_img`
docker commit $tmp_container my_repo/image_tag:latest
docker rm "$tmp_container"

在Dockerfile中具有RUN命令,但具有附加的卷挂载。示例中的 commit 命令也标记了图像。

This does the same as having a RUN command in the Dockerfile, but with the added volume mount. The commit command in the example also tags the image.

如果您需要在依赖于卷的命令之后,还有其他Dockerfile命令,但是在大多数情况下,您可以组合运行命令并重新安排安装,以使最后的手动run-with-volume命令保持简单。

It is a bit more complex if you need to have other Dockerfile commands after the volume-dependent one, but in most cases you can combine run commands and re-arrange your install in a way that leaves the manual run-with-volume command last, to keep things simple.

这篇关于Docker在构建时使用卷进行构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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