Dockerfile:了解VOLUME指令 [英] Dockerfile: understanding VOLUME instruction

查看:102
本文介绍了Dockerfile:了解VOLUME指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们举个例子。



以下是 VOLUME 指令nginx 图片:

  VOLUME [ / etc / nginx / sites-enabled, / etc /nginx/certs、/etc/nginx/conf.d、/var/log/nginx、/var/www/html] 

这是我的问题:


  1. 启动容器时,这些目录会显示在我的主机上吗?而当我停止容器时,目录将保留吗?


  2. 如果主机中已经存在这些目录中的某些(或全部),那会发生什么?例如,假设映像在容器的 / etc / nginx 目录中带有一个默认配置文件,而我在 / etc / nginx 在我的主机上。当容器启动时,哪个文件将具有优先权?


  3. -v / host / dir:container之间的主要区别是什么? / dir VOLUME


参考:




解决方案

容器的卷只是主机上的目录,无论它们是通过什么方法创建的。如果您未在主机上指定目录,则Docker将为该卷创建一个新目录,通常在 / var / lib / docker / vfs 下。



无论如何创建卷,都可以使用 docker inspect 命令轻松找到其在主机上的位置,例如:

  $ ID = $(docker run -d -v / data debian echo Data container)
$ docker inspect- f {{.Mounts}} $ ID
[{0d7adb21591798357ac1e140735150192903daf3de775105c18149552a26f951 / var / lib / docker / volumes / 0d7adb21591798357ac1e140735150192903daf3de775105c18149552p26b951] p>

我们可以看到Docker在 / var / lib / docker / volumes / 0d7adb21591798357ac1e140735150192903daf3de775105c18149552a26f951 / _data 。



您可以从主机上随意修改/添加/删除该目录中的文件,但请注意,您可能需要使用 sudo 获得权限。



Docker只会在两种情况下删除卷目录:




  • 如果-rm 选项被赋予 docker run ,当容器退出时任何卷都将被删除

  • 如果容器被 docker rm -v CONTAINER ,将删除任何卷。



在两种情况下,仅卷如果没有其他容器引用它们,则将其删除。映射到特定主机目录( -v HOST_DIR:CON_DIR 语法)的卷永远不会被Docker删除。但是,如果删除某个卷的容器,则命名方案意味着您将很难确定包含该卷的目录。



因此,特定问题:


  1. 是的,是,上面有一些警告。

  2. 每个Docker托管卷在目录中都有一个新目录。 host

  3. VOLUME 指令与 -v 相同,但未指定主机目录指定主机目录后,Docker不会为该卷创建任何目录,也不会从映像中复制文件,并且永远不会删除该卷( docker rm -v CONTAINER 不会删除映射到用户指定主机目录的卷。)

此处提供更多信息:



http://container-solutions.com/2014/12 / understanding-volumes-docker /


Let's take an example.

The following is the VOLUME instruction for the nginx image:

VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]

Here are my questions:

  1. When you start the container, will these directories show up on my host? And when I stop my container, the directories will stay?

  2. If some (or all) of these directories already exist in my host, what will happen? For example, let's say the image comes with a default config file within the /etc/nginx directory of the container, and I also have a config file within /etc/nginx on my host. When the container starts, which of these files will get priority?

  3. What's the key difference between -v /host/dir:container/dir and VOLUME?

References:

解决方案

A container's volumes are just directories on the host regardless of what method they are created by. If you don't specify a directory on the host, Docker will create a new directory for the volume, normally under /var/lib/docker/vfs.

However the volume was created, it's easy to find where it is on the host by using the docker inspect command e.g:

$ ID=$(docker run -d -v /data debian echo "Data container")
$ docker inspect -f {{.Mounts}} $ID
[{0d7adb21591798357ac1e140735150192903daf3de775105c18149552a26f951 /var/lib/docker/volumes/0d7adb21591798357ac1e140735150192903daf3de775105c18149552a26f951/_data /data local  true }]

We can see that Docker has created a directory for the volume at /var/lib/docker/volumes/0d7adb21591798357ac1e140735150192903daf3de775105c18149552a26f951/_data.

You are free to modify/add/delete files in this directory from the host, but note that you may need to use sudo for permissions.

Docker will only delete volume directories in two circumstances:

  • If the --rm option is given to docker run, any volumes will be deleted when the container exits
  • If a container is deleted with docker rm -v CONTAINER, any volumes will be removed.

In both cases, volumes will only be deleted if no other containers refer to them. Volumes mapped to specific host directories (the -v HOST_DIR:CON_DIR syntax) are never deleted by Docker. However, if you remove the container for a volume, the naming scheme means you will have a hard time figuring out which directory contains the volume.

So, specific questions:

  1. Yes and yes, with above caveats.
  2. Each Docker managed volume gets a new directory on the host
  3. The VOLUME instruction is identical to -v without specifying the host dir. When the host dir is specified, Docker does not create any directories for the volume, will not copy in files from the image and will never delete the volume (docker rm -v CONTAINER will not delete volumes mapped to user-specified host directories).

More information here:

http://container-solutions.com/2014/12/understanding-volumes-docker/

这篇关于Dockerfile:了解VOLUME指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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