Search code examples
dockerdocker-volumedocker-container

docker: Check space left on volume from container


I have a volume mounted in a container. The volume is of fixed size and the container writes data to this volume. How can I check the space left on the volume from within the container.

I can run commands on host running docker, but is it possible to do this from inside the container where the volume is mounted?


Solution

  • You can basically get the capacity as you would do in a normal Linux machine: df -h executed inside the container:

    Prerequisite A: create a fixed size volume

    docker volume create --driver local --opt type=tmpfs --opt device=tmpfs --opt o=size=100m,uid=1000 fixed-size-volume
    

    Prerequisite B: Mount the volume in your container wherever you need you mountpoint to be (/var/fixed-mount-point in my example)

    docker run -it --rm --mount source=fixed-size-volume,target=/var/fixed-mount-point  alpine sh
    

    Step 1: List the mounts inside the container and filter by the name of the mount point(I am already with a terminal inside my container since I used -it. To open a terminal you can use docker exec -ti <containerID> sh

    / # df -h | grep fixed
    tmpfs                   100.0M         0    100.0M   0% /var/fixed-mount-point
    

    Step 2: Create a dummy file in the mount point: I used dd to create a 1MB file

    dd if=/dev/zero of=/var/fixed-mount-point/file.txt count=1024 bs=1024
    

    Step 3: Check again the capacity of the volume. Note the change from 0% to 1%

    / # df -h | grep fixed
    tmpfs                   100.0M      1.0M     99.0M   1% /var/fixed-mount-point