且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Dockerfile 中 VOLUME 的目的是什么

更新时间:2022-06-12 05:17:51

卷是存储在 /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!