Create a docker volume of a specific size in a specific place

I am trying to create a docker volume of a specific size in a specific place. I have been looking at this for some time and can not find the answer.

I do not want the volume to be created in /var/lib/docker/volumes, but in /data/volumes/ where /data is a remote iscsi drive.

I have been able to create the volume in a specific place and access the volume from within the container using:

sudo docker run --detach --name testcontainer -v /data/volumes/testvol:/file-uploads imageName

This works fine. The directory “testvol” is created within /data/volumes/ and I can access “/data/volumes/testvol” from within my container via /file-uploads. However, the new volume is not listed on a “docker volume ls” and I don’t seem to be able to specify a volume size. I think I need something along the lines of the below but I can’t get the syntax:

sudo docker run --detach --name testcontainer -v /data/volumes/testvol:/file-uploads:size=10m imageName

Alternatively I can use volume create:

sudo docker volume create --opt o=source=/data/volumes --opt o=size=100m --opt device=/data/volumes --opt type=volume testvol

This returns no errors, but it always creates the volume in /var/lib/docker/volumes and not in /data/volumes. I am not sure I understand the device switch properly.

I dont really want to be using symbolic links in the /var/lib/docker/volumes directory over to /data/volumes.

Any ideas?

The reason is simple: it is not a volume :wink: It is a bind-mount. Docker litteraly does the same as the command mount --bind $source $target, where $source is the left hand side of your -v paramaneter and $target the right hand side. There is no way to restrict the volume if you just mount a folder inside a different location…

This is the only named volume you actualy created. You are missing parameters to actualy declare a “bind” type volume. They can be created; they are not treated like bind-mounts (even though they are technicaly the same).

This should do the trick:
docker volume create --opt type=none --opt o=bind --opt device=/data/volumes/testvol testvol

This one will be listed under docker volume ls and will have the “copy existing data into the volume” logic. For mount-binds both is not true.

Indeed. Since you already use iscsi, I would suggest to check wether there is a volume plugin for the vendor of your device and use it. Blockdevice based volumes will respect the volume size. The local driver does not respect it, regardless wether you create a volume of type bind, nfs4 or cifs.

Thanks for taking the time to answer the question @meyay. I am working on the problem and will update the post once I have found a resolution.