如何限制容器可用的 Docker 文件系统 space

How to limit Docker filesystem space available to container(s)

一般情况是我们有一个服务器集群,我们希望使用 Docker 在该集群之上设置虚拟集群。

为此,我们为不同的服务(Hadoop、Spark 等)创建了 Docker 文件。

然而,关于 Hadoop HDFS 服务,我们的情况是 docker 容器可用的磁盘 space 等于服务器可用的磁盘 space。我们想在每个容器的基础上限制可用磁盘 space 以便我们可以动态地生成一个具有一定存储大小的额外数据节点来为 HDFS 文件系统做出贡献。

我们想到了使用 ext4 格式的环回文件并将它们挂载到我们用作 docker 容器中的卷的目录中。但是,这意味着很大的性能损失。

我在 SO (Limit disk size and bandwidth of a Docker container) 上发现了另一个问题,但答案已经有将近 1.5 年的历史了——关于 docker 的发展速度——是古老的。

哪种方式或存储后端允许我们

您可以在内存和 CPU 上指定 runtime constraints,但不能在磁盘上指定 space。

已请求在磁盘 space 上设置约束的能力 (issue 12462, issue 3804),但尚未实现,因为它取决于底层文件系统驱动程序。

This feature is going to be added at some point, but not right away. It's a bit more difficult to add this functionality right now because a lot of chunks of code are moving from one place to another. After this work is done, it should be much easier to implement this functionality.

Please keep in mind that quota support can't be added as a hack to devicemapper, it has to be implemented for as many storage backends as possible, so it has to be implemented in a way which makes it easy to add quota support for other storage backends.


2016 年 8 月更新:如下所示,issue 3804 comment, PR 24771 and PR 24807 have been merged since then. docker run 现在允许为每个容器设置存储驱动程序选项

$ docker run -it --storage-opt size=120G fedora /bin/bash

This (size) will allow to set the container rootfs size to 120G at creation time.
This option is only available for the devicemapper, btrfs, overlay2, windowsfilter and zfs graph drivers

文档:docker run/#Set storage driver options per container.