How to get a sizeable and persistent volume?

Hi!
I would like to create a persistent volume after reboot with a specific size using docker-compose. Here is what I have tried.

This way the volume is persistent but no size is specified

volumes: {data}

And if I do this, the volume has the right size but data are not persistent after reboot

volumes: 
  data:
    driver_opts:
      type: none
      device: path
      o: bind, size=50G

I also tried with other types like nfs, tmpfs but I don’t manage to get a persistent volume. What did I not understand?

Thank you for your explanation

Though, you are aware that volumes suppport whatever parameters the mount command is able to use, aren’t you? A bind volume litterely uses a mount --bind to mount a host folder into a container folder… as such a size limitation wouldn’t make sense. The same applies to nfs/cifs shares: when you mount a remote share int o a container, it is still a file level share → so, no size limitation, unless you know how to configure a quota that limits the size for you.

You will need to use a blockdevice level volume driver, like devicemapper (deprecated, maybee still build-in), portworx (thirdparty) or any other docker volume plugin that allows blockdevice level volumes. Not sure if btrfs or zfs volume allow block device size limitation… might be worth a try.

There is one other way at least on linux if you need local volume and don’t want to create partitions.

1.) Create a virtual disk image
2.) Create a filesystem on that image
3.) Mount that image on the host to /mnt/docker/volumes/database_data
3.) Mount database_data to the container

disk_path=/mnt/docker/virtual_disks/database_data.img
volume_path=/mnt/docker/volume_mounts/database_data

mkdir "$volume_path"
dd if=/dev/zero of="$disk_path" bs=1M count=2000
mkfs.ext4 "$disk_path"
version: "3.7"

volumes:
  data:
    driver_opts:
      type: none
      device: /mnt/docker/volume_mounts/database_data
      o: bind

services:
  mysql:
    image: mysql:8.0
    volumes:
      - data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password

It needs some manual settings but works.