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

查看:82
本文介绍了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卷创建命令

  • docker run -v / path :/ host_path

  • Dockerfile VOLUME 项$ c>文件

  • 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中声明它,这意味着每次从映像启动容器时,都会创建卷( empty ),即使您没有任何容器 -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.

您可以在运行时声明 docker run -v [host-dir:]容器目录

将两者( VOLUME + docker run -v )意味着您可以将主机文件夹的内容挂载到<$ c中由容器保留的卷中$ c> / 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天全站免登陆