Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create docker volume with limited size

There is is very limited information regarding create volume with options "https://docs.docker.com/engine/reference/commandline/volume_create/ ".

I just want to create a volume with limited size, I tried,

docker volume create --opt o=size=10m hello-volume

, when run a simple hello world container with “docker run -v hello-volume:/hello -it ubuntu /bin/bash”, I got the error as something like “docker: Error response from daemon: …no such device”…

So I assumed we must give the full options when creating the volume, such as

“docker volume create --driver local --opt type=*** --opt device=*** --opt o=size=10m hello-volume”

? If so, what are the “type” and “device” here? I guess the device actually is a path to any directory? I tried

“docker volume create --driver local --opt type=volume --opt device=/ --opt o=size=10m hello-volume”.

When tried to run the container as “docker run -v hello-volume:/hello -it ubuntu /bin/bash” I got the error as “docker: Error response from daemon: error while mounting volume ‘/var/lib/docker/volumes/hello-volume/_data’: error while mounting volume with options: type=‘volume’ device=’/’ o=‘size=10m’: no such device.”.

I tried

docker volume create --driver local --opt type=tmpfs --opt device=tmpfs --opt o=size=10m hello-volume

which finally works, but the data is in memory which is not persistent. So can any one point out what are the options for “type” and “device” when creating the volume (or what’s the default one if we don’t give any options)?

Update:

Just want to update that it seems there is no way to limit the disk size in local driver with ext4 type (although claimed doable in several answers and other posts). The major reason is there is no such "size" parameter in "ext4" file system.. I created a docker volume:

docker volume create --driver local --opt type=ext4 --opt device=/dev/xvdf --opt o=size=10m hello-volume

Then inspect it (docker volume inspect hello-volume), and mount it with a container, everything looks fine for now.

[
    {
        "CreatedAt": "2018-09-01T04:23:57Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/hello-volume/_data",
        "Name": "hello-volume",
        "Options": {
            "device": "/dev/xvdf",
            "o": "size=10m",
            "type": "ext4"
        },
        "Scope": "local"
    }
]

docker run -v hello-volume:/myfile1 -exec -it ubuntu /bin/bash

But when you write/put a large file (larger than 10m) in the "/myfile1" in that container, no alert/exception is thrown, the write succeeded. What I think is that "o=size=10m" is not recognized by ext4 type file system.

like image 878
batilei Avatar asked Aug 30 '18 04:08

batilei


People also ask

Do docker containers have a size limit?

In the current Docker version, there is a default limitation on the Docker container storage of 10Gb.

Can we specify volume in Dockerfile?

You cannot specify a volume source in the Dockerfile: A common source of confusion when specifying volumes in a Dockerfile is trying to match the runtime syntax of a source and destination at image build time, this will not work. The Dockerfile can only specify the destination of the volume.

What is dangling volume in docker?

A dangling volume is one that exists and is no longer connected to any containers. There is a similar rm command for volumes as well. We can use this along with docker volume ls command with a filter to remove volumes that are dangling.

How do I determine docker volume size?

You can check size of your docker system. If you want to check from where your containers take storage, you can inspect your volume. In my case, my container takes storage from /var/lib/docker/volumes/myvol1/_data and this file is available in my local system. Hope this will clear your doubt.


3 Answers

This is actually possible using dockers local volume driver. This driver accepts options similar to the linux mount options. Check out this excellent answer.

You were close with your attempt. You need to specify a --opt device= as well as a --opt type= option. This will essentially mount a block device on your host into your docker container as a volume. The type option specifies the filesystem type to use. The device option requires you to specify one of the block devices on your host - /dev/sda2 for example. You can see a complete list by running lsblk.

However, it would be a bad idea to map existing drives (unless they are unused). So you will need to create a new block device (using lvm or equivalent), or for testing you can use volatile storage (tmpfs), as you have tried.

like image 112
moebius Avatar answered Nov 09 '22 01:11

moebius


Docker volumes have "drivers" attached to them. The default driver is one called "local" and all it does is creates a directory in /var/lib/docker and mounts it to the container. There's no option to specify (or limit) the volume's size.

like image 21
David Maze Avatar answered Nov 09 '22 03:11

David Maze


I solved issue for limiting space on docker volumes (by binding dir with -v) using xfs_quota

Location where I needed to store data is a XFS volume mounted with prjquota option

#~ mount 
/dev/mapper/centos_ssp--forfiter--07-home on /home type xfs (rw,[..],**prjquota**)

I think that kernel must support it (rootflags=pquota kernel options)

Example from above link:

Enabling project quota on an XFS filesystem (restrict files in log file directories to only using 1 gigabyte of space).
# mount -o prjquota /dev/xvm/var /var
# echo 42:/var/log >> /etc/projects
# echo logfiles:42 >> /etc/projid
# xfs_quota -x -c 'project -s logfiles' /var
# xfs_quota -x -c 'limit -p bhard=1g logfiles' /var

There is also a plugin for docker cirocosta/xfsvol

like image 36
khadish Avatar answered Nov 09 '22 02:11

khadish