docker运行-v还是Dockerfile VOLUME优先? [英] Does docker run -v or Dockerfile VOLUME take precedence?

查看:260
本文介绍了docker运行-v还是Dockerfile VOLUME优先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dockerfile



  FROM nginx:1.7 

#在运行前仔细声明VOLUME
VOLUME / var / tmp

RUN touch /var/tmp/test.txt



< h3>观察

现在我相信我了解在
之前声明 VOLUME 语句的含义, code> test.txt - 在运行时公开的卷 / var / tmp 将基于$ $之前的中间容器c $ c> test.txt 文件被创建,因此它将为空(希望此观察结果正确)



code> docker run 不显示 test.txt



docker run kz / test-volume:0.1



但是,我尝试在运行时提供卷,如下所示: p>

docker run -v / var / tmp kz / test-volume:0.1



问题



结果是一样的。那么这是什么意思?将docker运行命令映射中的 -v / var / tmp 映射到空的 / var / tmp 目录Dockerfile中的 VOLUME 命令,而不是 / var / tmp 目录中的测试。 txt 在最新的图像?



感觉有点困惑。

解决方案

没有包含 /var/tmp/test.txt 的图像。在创建文件之前声明卷的效果是 RUN 指令在具有自己卷的临时容器中运行。 卷绕过联盟文件系统,所以当该构建会保存中间容器,卷的内容不会保存,因此它们不会保留在图像层中。



您从该映像创建的每个容器将具有自己的卷, -v 选项不会更改,除非您使用它将卷映射到主机路径。 / p>

使用您的Docker文件,您可以通过检查两个容器来看到。第一个没有 -v 选项:

 >码头运行-d temp 
c3c4f7de411f166b3a67397ff1221552fe5b94c46bc100725a50a57231da427b

> docker inspect -f'{{.Mounts}}'c3c
[
{67267d2eeb57373f76a9dd50c25f744b7d99d0a75647bf06aa0d17b70807cf71 / var / lib / docker / volumes / 67267d2eeb57373f76a9dd50c25f744b7d99d0a75647bf06aa0d17b70807cf71 / _data / var / cache / nginx local true}
{ 91490ea73e3a9d42df9f00e32a91fe571d2143f54248071959765d5d55c23d46 / var / lib / docker / volumes / 91490ea73e3a9d42df9f00e32a91fe571d2143f54248071959765d5d55c23d46 / _data / var / tmp local true
]

这里有两个卷,从主机上的 / var / lib / docker 安装。一个来自 nginx 基本图像,一个来自您的图像。使用明确的 -v

 >码头运行-d -v / var / tmp temp 
6fa1a8713b2d6638675a3d048669943419bc7a3924ed98371771100bcfde3954

> docker inspect -f'{{.Mounts}}'6fa
[
{9adf6954ed3e826f23a914cbbd768753e6dec1f176eed10e03c2d5503287d101 / var / lib / docker / volumes / 9adf6954ed3e826f23a914cbbd768753e6dec1f176eed10e03c2d5503287d101 / _data / var / cache / nginx local true}
{ 7dd8be71ce88017ffbeef249837bdd1c96c071b802a2f43b18fd406983e1076a / var / lib / docker / volumes / 7dd8be71ce88017ffbeef249837bdd1c96c071b802a2f43b18fd406983e1076a / _data / var / tmp local true}
]

相同的结果,但不同的主机路径,因为每个容器都有自己的卷。


Dockerfile

FROM nginx:1.7

# Deliberately declaring VOLUME before RUN
VOLUME /var/tmp

RUN  touch /var/tmp/test.txt

Observation

Now I believe I understand the implications of declaring the VOLUME statement before creating test.txt - the volume /var/tmp exposed at runtime will be based on the intermediary container prior to the test.txt file is created so it will be empty (hope this observation is correct)

So as expected the following docker run does not show test.txt:

docker run kz/test-volume:0.1

But then I tried supplying the volume at runtime as below:

docker run -v /var/tmp kz/test-volume:0.1

Question

The outcome was the same. So what does this mean? does the -v /var/tmp in the docker run command map to the empty /var/tmp dir exposed by the VOLUME command in the Dockerfile and not the /var/tmp directory with the test.txt in the latest image?

Feeling a tad bit confused.

解决方案

There is no image which contains /var/tmp/test.txt. The effect of declaring the volume before creating the file is that the RUN instruction runs in a temporary container which has its own volume. Volumes bypass the Union File System so when the build saves that intermediate container, the contents of the volume are not saved, so they don't get persisted in the image layer.

Every container you create from that image will have its own volume, the -v option doesn't change that, unless you use it to map the volume to a host path.

Using your Dockerfile, you can see that by inspecting two containers. The first without the -v option:

> docker run -d temp                                                                                              
c3c4f7de411f166b3a67397ff1221552fe5b94c46bc100725a50a57231da427b                                                  

> docker inspect -f '{{ .Mounts }}' c3c                                                                           
[
    {67267d2eeb57373f76a9dd50c25f744b7d99d0a75647bf06aa0d17b70807cf71/var/lib/docker/volumes/67267d2eeb57373f76a9dd50c25f744b7d99d0a75647bf06aa0d17b70807cf71/_data /var/cache/nginx local  true }
    {91490ea73e3a9d42df9f00e32a91fe571d2143f54248071959765d5d55c23d46/var/lib/docker/volumes/91490ea73e3a9d42df9f00e32a91fe571d2143f54248071959765d5d55c23d46/_data /var/tmp local  true }
]  

Here there are two volumes, mounted from /var/lib/docker on the host. One is from the nginx base image, and one from your image. With the explicit -v:

> docker run -d -v /var/tmp temp                                                                                  
6fa1a8713b2d6638675a3d048669943419bc7a3924ed98371771100bcfde3954 

> docker inspect -f '{{ .Mounts }}' 6fa                                                                           
[
    {9adf6954ed3e826f23a914cbbd768753e6dec1f176eed10e03c2d5503287d101/var/lib/docker/volumes/9adf6954ed3e826f23a914cbbd768753e6dec1f176eed10e03c2d5503287d101/_data /var/cache/nginx local  true } 
    {7dd8be71ce88017ffbeef249837bdd1c96c071b802a2f43b18fd406983e1076a/var/lib/docker/volumes/7dd8be71ce88017ffbeef249837bdd1c96c071b802a2f43b18fd406983e1076a/_data /var/tmp local  true }
]  

Same outcome, but different host paths, because each container has its own volume.

这篇关于docker运行-v还是Dockerfile VOLUME优先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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