How to define a disk quota for docker containers?
One way to solve this is to put the diff directory /var/lib/docker/aufs/diff/$CONTAINER_ID
into a sparse loopback mounted ext4 directory. This effectively limits the amount of data a user can store/modify in a container. This is the bash code I use:
do_enable_quota() { local ID=$1 local QUOTA_MB=$2 local LOOPBACK=/var/lib/docker/aufs/diff/$ID-loopback local LOOPBACK_MOUNT=/var/lib/docker/aufs/diff/$ID-loopback-mount local DIFF=/var/lib/docker/aufs/diff/$ID docker stop -t=0 $ID sudo dd of=$LOOPBACK bs=1M seek=$QUOTA_MB count=0 sudo mkfs.ext4 -F $LOOPBACK sudo mkdir -p $LOOPBACK_MOUNT sudo mount -t ext4 -n -o loop,rw $LOOPBACK $LOOPBACK_MOUNT sudo rsync -rtv $DIFF/ $LOOPBACK_MOUNT/ sudo rm -rf $DIFF sudo mkdir -p $DIFF sudo umount $LOOPBACK_MOUNT sudo rm -rf $LOOPBACK_MOUNT sudo mount -t ext4 -n -o loop,rw $LOOPBACK $DIFF docker start $ID }
This approach works perfectly for me but the drawback is that I need to wrap the "start", "stop" and "rm" commands to take the mount into account.
ZFS is also a great deal: https://docs.docker.com/engine/userguide/storagedriver/zfs-driver/
This way you can manage your disk pools with 'zpool' command line.
For example, to create 'just a bunch of vdisk':
[root@localhost /]# mkdir /dsk[root@localhost /]# dd if=/dev/zero of=/dsk/disk1 bs=1M count=750[root@localhost /]# dd if=/dev/zero of=/dsk/disk2 bs=1M count=750[root@localhost /]# dd if=/dev/zero of=/dsk/disk3 bs=1M count=750[root@localhost /]# dd if=/dev/zero of=/dsk/disk4 bs=1M count=750
Ok, we have four disks of 750M each one. Now create one single ZFS pool:
[root@localhost /]# zpool create CIALINUX /dsk/disk{1,2,3,4}
Now we have a volume named CIALINUX, with almost 3GB automatically mounted in our / directory:
[root@localhost /]# df -h |grep CIALINUXCIALINUX 2,9G 18K 2,9G 1% /CIALINUX
Other commands you can explore by yourself are:
# zpool list# zpool status# zpool status -x# zpool destroy CIALINUX
Important: This last one 'destroys' your pool. Please pay attention to it.
Once you have your zfs pools, just place your files attached as docker volumes inside /zpool-mounted-directory for each container.
Hope this help community.