Dockerfile 中 VOLUME 的目的是什么 [英] What is the purpose of VOLUME in Dockerfile

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

问题描述

我正在尝试更深入地了解 Docker 的容量,但我很难弄清楚以下内容的差异/用例:

I'm trying to go deeper in my understanding of Docker's volume, and I'm having an hard time to figure out the differences / use-case of:

  • docker volume create 命令
  • docker run -v/path:/host_path
  • Dockerfile 文件中的 VOLUME 条目
  • The docker volume create command
  • The docker run -v /path:/host_path
  • The VOLUME entry in the Dockerfile file

我特别不明白如果将 VOLUME 条目与 -v 标志结合会发生什么.

I particularly don't understand what happens if you combine the VOLUME entry with the -v flag.

推荐答案

卷是存储在 /var/lib/docker/volumes/...

  • 您可以在 Dockerfile 中声明它,这意味着每次从镜像启动容器时,都会创建该卷(),即使您没有任何-v 选项.

  • You can either declare it in a Dockerfile, which means each time a container is started from the image, the volume is created (empty), even if you don't have any -v option.

你可以在运行时声明它dockerrun -v [host-dir:]container-dir.
将两者结合起来 (VOLUME + docker run -v) 意味着您可以将主机文件夹的内容挂载到容器在 /var/中持久化的卷中lib/docker/volumes/...

You can declare it on runtime docker run -v [host-dir:]container-dir.
combining the two (VOLUME + docker run -v) means that you can mount the content of a host folder into your volume persisted by the container in /var/lib/docker/volumes/...

docker volume create 无需定义即可创建卷一个 Dockerfile 并构建一个镜像并运行一个容器.它用于快速允许其他容器挂载该卷.

docker volume create creates a volume without having to define a Dockerfile and build an image and run a container. It is used to quickly allow other containers to mount said volume.

如果您在卷中保留了一些内容,但此后删除了容器(默认情况下不会删除其关联的卷,除非您使用 docker rm -v),您可以将所述卷重新附加到新的容器(声明相同的卷).

If you had persisted some content in a volume, but since then deleted the container (which by default does not deleted its associated volume, unless you are using docker rm -v), you can re-attach said volume to a new container (declaring the same volume).

请参阅Docker - 如何访问未附加到容器的卷?".
使用 docker volume create,可以轻松地将命名卷重新附加到容器.

See "Docker - How to access a volume not attached to a container?".
With docker volume create, this is easy to reattached a named volume to a container.

docker volume create --name aname
docker run -v aname:/apath --name acontainer
...
# modify data in /apath
...
docker rm acontainer

# let's mount aname volume again
docker run -v aname:/apath --name acontainer
ls /apath
# you find your data back!

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

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