VOLUME 命令在 Dockerfile 中的作用是什么? [英] what does VOLUME command do in Dockerfile?

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

问题描述

我很难理解 VOLUME 的附加优势(https://docs.docker.com/engine/reference/builder/#volume) .

I am having it difficulty in understanding added advantage of VOLUME(https://docs.docker.com/engine/reference/builder/#volume) .

在 Dockerfile 中可以使用 mkdir 创建目录.一旦创建了目录,我们就可以处理它.为什么要指定一个 VOLUME(挂载)并分配给该目录?VOLUME 安装有什么优势?我试图在没有 VOLUME 的情况下在这里了解我们会错过什么.

In Dockerfile one can have mkdir to create a directory. Once the directory is created we can have handle to it. why specify a VOLUME (mount)and assign to that directory? What advantage VOLUME mount gives? I am trying to understand here without VOLUME what will we miss.

对我来说它看起来像一个多余的功能,但我可能错了.

to me its looks like a redundant function , however I might be wrong.

推荐答案

VOLUME 命令将指定容器中的挂载点.此挂载点将映射到主机上的位置,该位置在创建容器时指定,如果未指定,则从/var/lib/docker/volumes 中创建的目录中自动选择.

The VOLUME command will specify a mount point in the container. This mount point will be mapped to a location on the host that is either specified when the container is created or if not specified chosen automatically from a directory created in /var/lib/docker/volumes.

如果选择作为挂载点的目录包含任何文件,则这些文件将被复制到此卷中.与 mkdir 相比的优势在于,它会在容器终止后将文件持久保存到主机上的某个位置.

If the directory chosen as the mount point contains any files then these files will be copied into this volume. The advantage over mkdir is that it will persist the files to a location on the host machine after the container is terminated.

似乎有些人质疑您为什么要使用 VOLUME 命令,因为它会创建一个匿名卷.匿名卷不再有太多用处,基本上是 Docker 在可以命名卷之前的早期产物.您通常会在创建容器时指定卷名:

It appears some people have questioned why you would use the VOLUME command since it creates an anonymous volume. Anonymous volumes don't have much use any more and are basically an artifact of the early days of Docker before volumes could be named. You would normally specify the volume name when creating the container:

docker 容器运行 -v my-volume:/data image_tag

在本例中,/data 是容器中的挂载点,my-volume 是本地主机上的卷.如果运行此命令时我的卷不存在,则会在本地主机上创建它.

In this example, /data is the mount point in the container and my-volume is the volume on the local host. If my volume does not exist when this command is run then it is created on the local host.

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

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