在构建过程中如何在Dockerfile中将主机卷装载到docker容器中 [英] How to mount host volumes into docker containers in Dockerfile during build

查看:140
本文介绍了在构建过程中如何在Dockerfile中将主机卷装载到docker容器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始问题:如何在Dockerfile中使用VOLUME指令?

Original question: How to use the VOLUME instruction in Dockerfile?

修改:从下面的答案更新,所以实际的问题我想要解决的是 - 在构建期间如何将主机卷挂载到Dockerfile中的docker容器中,即在<$ c期间让 docker运行-v / export:/ export $ c> docker build 。

Revised: update from the answer below, so the actual question I want to solve is -- how to mount host volumes into docker containers in Dockerfile during build, i.e., having the docker run -v /export:/export capability during docker build.

最新更新:现在有一个解决方案。虽然它不是严格的Docker,但是解决了Dockerfile的所有弱点,所以我认为这是完美的答案。查看我的最新答案详情。

Latest Update: There is a solution now. Although it is not strictly Docker, but "it solves all the weak points of Dockerfile", so I think it is the perfect answer. Check my latest answer for details.

更新:所以答案是不可能。我可以接受它作为答案,因为我知道这个问题已经在 https://github.com/docker/docker /问题/ 3156 。我可以理解,可移植性对于码头开发商来说是至关重要的;但作为码头的用户,我不得不说这个丢失的功能非常失望。让我用上述讨论的引语来闭上我的论据:我想使用Gentoo作为基础图像,但绝对不希望> 1GB的Portage树数据在图像一直处于任何图层如果不是在安装过程中出现在图像中的巨型Portage树,那么可以使用一个很好的紧凑型容器。是的,我可以使用wget或curl下载任何需要的东西,但是事实上,只有一个可移植性的考虑现在迫使我每次建立Gentoo基础图像时才能下载> 1GB的Portage树,既不高效也不方便用户使用。此外,软件包存储库将始终位于/ usr / portage下,因此始终可在Gentoo下运行。再次,我尊重这个决定,但请允许我在同一时间表达我的失望。谢谢。

Update: So the answer is "Not possible". I can accept it as an answer as I know the issue has been extensively discussed at https://github.com/docker/docker/issues/3156. I can understand that portability is a paramount issue for docker developer; but as a docker user, I have to say I'm very disappointed about this missing feature. Let me close my argument with a quote from aforementioned discussion: "I would like to use Gentoo as a base image but definitely don't want > 1GB of Portage tree data to be in any of the layers once the image has been built. You could have some nice a compact containers if it wasn't for the gigantic portage tree having to appear in the image during the install." Yes, I can use wget or curl to download whatever I need, but the fact that merely a portability consideration is now forcing me to download > 1GB of Portage tree each time I build a Gentoo base image is neither efficient nor user friendly. Further more, the package repository WILL ALWAYS be under /usr/portage, thus ALWAYS PORTABLE under Gentoo. Again, I respect the decision, but please allow me expressing my disappointment as well in the mean time. Thanks.

详细的原始问题:

通过卷共享目录

http: //docker.readthedocs.org/en/v0.7.3/use/working_with_volumes/

它说数据卷功能自版本1以来一直可用的Docker Remote API。我的docker是版本1.2.0,但是我发现上面的文章中的例子不起作用:

it says that Data volumes feature "have been available since version 1 of the Docker Remote API". My docker is of version 1.2.0, but I found the example given in above article not working:

# BUILD-USING:        docker build -t data .
# RUN-USING:          docker run -name DATA data
FROM          busybox
VOLUME        ["/var/volume1", "/var/volume2"]
CMD           ["/usr/bin/true"]

Dockerfile中将主机安装的卷装载到docker容器中的正确方法是什么,通过VOLUME命令?

What's the proper way in Dockerfile to mount host-mounted volumes into docker containers, via the VOLUME command?

$ apt-cache policy lxc-docker
lxc-docker:
  Installed: 1.2.0
  Candidate: 1.2.0
  Version table:
 *** 1.2.0 0
        500 https://get.docker.io/ubuntu/ docker/main amd64 Packages
        100 /var/lib/dpkg/status

$ cat Dockerfile 
FROM          debian:sid

VOLUME        ["/export"]
RUN ls -l /export
CMD ls -l /export

$ docker build -t data .
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon 
Step 0 : FROM          debian:sid
 ---> 77e97a48ce6a
Step 1 : VOLUME        ["/export"]
 ---> Using cache
 ---> 59b69b65a074
Step 2 : RUN ls -l /export
 ---> Running in df43c78d74be
total 0
 ---> 9d29a6eb263f
Removing intermediate container df43c78d74be
Step 3 : CMD ls -l /export
 ---> Running in 8e4916d3e390
 ---> d6e7e1c52551
Removing intermediate container 8e4916d3e390
Successfully built d6e7e1c52551

$ docker run data
total 0

$ ls -l /export | wc 
     20     162    1131

$ docker -v
Docker version 1.2.0, build fa7b24f


推荐答案

更新:有些人不会不答应,我非常喜欢这个特别的问题。

UPDATE: Somebody just won't take no as the answer, and I like it, very much, especially to this particular question.

好消息,现在有一种方法 -

GOOD NEWS, There is a way now --

解决方案是Rocker: https://github.com/grammarly/rocker

The solution is Rocker: https://github.com/grammarly/rocker

约翰·亚尼 said
IMO,它解决了Dockerfile的所有弱点,使其适合开发。

https://github.com/grammarly/rocker


通过引入新的命令,Rocker的目标解决以下使用情况,这对于朴素的Docker是很痛苦的:

By introducing new commands, Rocker aims to solve the following use cases, which are painful with plain Docker:


  1. 在构建阶段安装可重用的卷,因此依赖关系管理工具可能会使用缓存在构建之间。

  2. 使用构建(用于拉出私人回馈等)来共享ssh密钥,而不会将它们留在生成的图像中。

  3. 构建并在不同的图像中运行应用程序,可以轻松地将图像从一个图像传递到另一个图像,理想情况下在单个Docker文件中具有这种逻辑。

  4. 从Dockerfiles标签/推送图像


  1. Mount reusable volumes on build stage, so dependency management tools may use cache between builds.
  2. Share ssh keys with build (for pulling private repos, etc.), while not leaving them in the resulting image.
  3. Build and run application in different images, be able to easily pass an artifact from one image to another, ideally have this logic in a single Dockerfile.
  4. Tag/Push images right from Dockerfiles.
  5. Pass variables from shell build command so they can be substituted to a Dockerfile.

还有更多。这些是阻止我们在语法上采用Docker的最关键的问题。

And more. These are the most critical issues that were blocking our adoption of Docker at Grammarly.

这篇关于在构建过程中如何在Dockerfile中将主机卷装载到docker容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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