Limit Docker volume size
How to easily create docker volumes with limited disk allocation.

Recently I've faced the problem - how to create Docker volumes that will be limited in size? For example, a want 50GB volume for my web application and 25GB volume for ElasticSearch container. Turns out that it’s not so easy. Docker documentation provides a few examples of advanced volumes usage with custom storage drivers here, but it looks like only tmpfs
mounts support disk usage limitations.
So how to solve this problem?
TL;DR: Use Convoy plugin with devicemapper driver, but take into consideration that this project is not actively maintained or just switch to Kubernetes.
If you’re brave enough to use software that’s explicitly marked as DEPRECATED — you’re welcome!
First of all, you have to install Convoy on your machine:
Installation instructions are also available here.
Then we have to download and execute a script that will help us with setting up Device Mapper driver. $DISK_MOUNTPOINT
variable should be replaced with the path to the disk that will be formatted and partitioned for Convoy to operate.
More info about using Convoy with devicemapper driver can be found here.
Now it’s time to run Convoy daemon (btw — don’t forget to unmount your disk if it was mounted to the system):
Also, it’s a good idea to turn convoy daemon into a service, here’s an example on the service file:
Update service file with your values if needed, place it into systemd services folder, enable and start:
After this check out convoy service status by running service convoy status
.
Voilà, now you should be able to create docker volumes with predefined size:
It’s that simple! Now you can check out newly created partitions by running fdisk -l
command.
Conclusion
As we can see it’s possible to create docker volumes with a predefined size and do it from Docker API which is especially useful if you’re creating new volumes from some container with mounted docker socket and don’t have access to the host.
Limiting other machine resources in Docker is much simpler and could be done just by using built-in Docker capabilities:
You can learn more about Rancher Convoy in their GitHub repo:
All steps from the tutorial also available as a single gist.
If this article was helpful — clap and share 🙃. Thanks for reading!