简体   繁体   English

为什么我要在Dockerfile中使用VOLUME?

[英]Why would I want to to use VOLUME inside a Dockerfile?

To me the VOLUME in a Dockerfile doesn't seam to be doing anything, where -v on the commandline actually make a directory available inside the container. 对我来说,Dockerfile中的VOLUME并没有接缝做任何事情,其中​​命令行上的-v实际上使容器内的目录可用。

When I read the Docker manual for VOLUME , it is not really clear to me, why I ever want to write it in the Dockerfile, and not just on the commandline? 当我阅读VOLUME的Docker手册时,我不太清楚为什么我想在Dockerfile中编写它,而不仅仅是在命令行上?

Defining the volume in the Dockerfile doesn't expose the volumes to the host by default. 默认情况下,在Dockerfile中定义卷不会将卷公开给主机。 Instead it sets up the linked volume to allow other containers to link to the volume(s) in other Docker containers. 相反,它设置链接卷以允许其他容器链接到其他Docker容器中的卷。 This is commonly used in a "Data Container" configuration where you start a container with the sole purpose of persisting data. 这通常用于“数据容器”配置,您可以在其中启动容器,其唯一目的是持久化数据。 Here's a simple example: 这是一个简单的例子:

 docker run -d --name docker_data docker/image1
 docker run -d --volumes-from docker_data --name new_container docker/image2

Notice the --volumes-from flag. 注意--volumes-from标志。

See http://container-solutions.com/understanding-volumes-docker/ for a more thorough explanation. 有关更详细的说明,请参见http://container-solutions.com/understanding-volumes-docker/

In addition to the accepted answer, another consideration for using volumes is performance . 除了接受的答案之外,使用卷的另一个考虑因素是性能 Typically, the layered filesystems used by Docker (typically AUFS or Devicemapper, depending on which Linux distribution you're using) aren't the fastest and may become a bottleneck in high-throughput scenarios (like, for example, databases or caching directories). 通常,Docker使用的分层文件系统(通常是AUFS或Devicemapper,取决于您正在使用的Linux发行版)并不是最快的,并且可能成为高吞吐量方案(例如,数据库或缓存目录)的瓶颈。

Volumes, on the other hand, even if not explicitly mapped to a host directory, are still simple bind mounts to the host file system, allowing a higher throughput when writing data. 另一方面,即使未显式映射到主机目录,卷仍然是对主机文件系统的简单绑定挂载,从而在写入数据时允许更高的吞吐量。

For further reading, there's an interesting paper by IBM on this topic, which contains some interesting conclusions regarding the performance impact of using Docker volumes (emphasis mine): 为了进一步阅读, IBM就此主题发表了一篇有趣的论文 ,其中包含一些关于使用Docker卷(强调我的)的性能影响的有趣结论:

AUFS introduces significant overhead which is not surprising since I/O is going through several layers, [...]. AUFS引入了显着的开销 ,这并不奇怪,因为I / O经历了多个层次,[...]。 Applications that are filesystem or disk intensive should bypass AUFS by using volumes. 文件系统或磁盘密集型应用程序应使用卷绕过AUFS。 [...] Although containers themselves have almost no overhead, Docker is not without performance gotchas. [...]虽然容器本身几乎没有开销,但Docker并非没有性能问题。 Docker volumes have noticeably better performance than files stored in AUFS. Docker卷的性能明显优于 AUFS中存储的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM