为什么某些Docker映像没有定义VOLUME? [英] Why do some Docker images have no VOLUME defined?

查看:75
本文介绍了为什么某些Docker映像没有定义VOLUME?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从Docker开始,如果我的问题对您来说太傻了,请原谅我.

I am just starting with Docker, please pardon me if my question is too silly for you.

我看到某些图像(例如 nginx )未定义任何 VOLUME ,而某些图像(例如 mysql )具有 VOLUME 在其Dockerfile中定义.

I see some images, like nginx, does not have any VOLUME defined whereas some images like mysql has VOLUME defined in their Dockerfile.

在没有定义卷的情况下如何管理数据或文件?它们的工作目录是什么?

How the data or files are managed when there is no volume defined, and what would be their working directory?

推荐答案

可以在容器中运行的许多应用程序的首选模式是在本地完全不存储任何状态.如果我有一个Web应用程序,也许它将一个配置文件作为输入,但是它将所有数据存储在一个外部数据库中,则不需要本地存储.反过来,这意味着我可以运行它的多个副本以实现冗余或支持更大的规模,并且如果需要更换容器,则无需担心保留数据.如果我的容器实际上是无状态的,那么我就不需要 VOLUME (或在运行时将卷附加到我的容器上).

A preferred pattern for many applications you can run in a container is to store no state at all locally. If I have a Web application, and perhaps it takes a configuration file as input, but it stores all of its data in an external database, I need no local storage. In turn, that means that I can run several copies of it for redundancy or to support larger scales, and I don't need to worry about preserving data if I need to replace a container. If my containers are in fact stateless, then I have no need for a VOLUME (or to attach volumes to my container at runtime).

即使您的容器确实具有本地状态,您也可能不想要 VOLUME . VOLUME 的实际含义是,当容器运行时,如果没有其他文件安装在命名的文件系统路径上,请创建一个匿名卷并将其安装在该卷上(让Docker从映像内容填充它).实际上,这意味着:

Even if your container does have local state, you probably don't want VOLUME. What VOLUME actually means is, when a container runs, if nothing else is mounted on the named filesystem path, create an anonymous volume and mount it there (letting Docker populate it from image content). In practice, this means:

  • 您可以使用 docker run -v 或Docker Compose volumes:将目录安装在任何容器文件系统路径上,无论是否将其声明为音量.
  • 如果某些内容声明为 VOLUME ,则以后的 RUN 指令将无法更改该目录树.
  • 如果将某些内容声明为 VOLUME ,则永远无法创建在该位置具有不同内容的派生图像(例如,您不能创建派生自 FROM postgresql 中包含预加载的数据).
  • You can use docker run -v or Docker Compose volumes: to mount a directory on any container filesystem path, regardless of whether or not it's declared as a VOLUME.
  • If something is declared as a VOLUME, later RUN instructions can't change that directory tree.
  • If something is declared as a VOLUME, you can never create a derived image that has different contents at that location (for example, you can't create an image derived FROM postgresql with preloaded data).

声明 VOLUME/tmp (在Java Dockerfile中似乎很常见)或在部分应用程序代码中声明 VOLUME 并没有特别的意义.

There's no particular point to declaring VOLUME /tmp (as seems to be common in Java Dockerfiles) or for declaring VOLUME over parts of your application code.

VOLUME 声明与图像的 WORKDIR 无关.

这篇关于为什么某些Docker映像没有定义VOLUME?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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