To check the size of a Docker volume, you can use the docker system df
command to view the disk usage by Docker objects, including volumes, but this provides a high-level overview and not the size of individual volumes. For detailed information on a specific volume, you can use the following methods:
docker volume inspect
with Custom CommandsFor specific volume size, Docker does not directly provide a command, but you can inspect a volume and use system commands to check its size if you know where Docker stores its volumes on the host system. By default, Docker volumes are stored in /var/lib/docker/volumes
on Linux. You can use this knowledge to check the size of a specific volume:
bashdocker volume inspect [VOLUME_NAME] --format '{{ .Mountpoint }}'
Then, use the du
(disk usage) command to check the size of the volume's directory:
bashsudo du -sh /var/lib/docker/volumes/[VOLUME_NAME]/_data
Replace [VOLUME_NAME]
with the name of your Docker volume.
If you don't have direct access to the host's file system or you're using Docker for Mac/Windows, you can still check a volume's size by creating a temporary container that mounts the volume and then using the du
command within that container:
bashdocker run --rm -v [VOLUME_NAME]:/data alpine du -sh /data
This command runs an Alpine container, mounts the volume [VOLUME_NAME]
to /data
inside the container, and executes du -sh /data
to show the size of the data within the volume. The container is removed after the command runs due to the --rm
flag.
If you're using a volume plugin, some plugins may provide their own mechanisms or commands to check the volume size. You'll need to refer to the specific plugin's documentation for instructions.
du
command provides the actual disk usage of files and directories. It may differ from the allocated space reported by some file systems.sudo
or running as a user with appropriate permissions to access Docker's internal storage or execute commands in containers.These methods are generally applicable for most Docker installations, but details may vary depending on your Docker setup and the host operating system.
To create a script that lists all Docker volumes and their respective sizes, you can combine shell commands with Docker commands. The script will:
du
command to get the size of each volume.Here's a basic script that does this. Note that this script assumes you're running it on a Linux host and have permissions to access Docker's storage and execute the necessary commands. You might need to run this script with sudo
or as a user with Docker privileges.
bash#!/bin/bash
# List all docker volumes, extract their names, and iterate over them
docker volume ls --format "{{.Name}}" | while read volume_name; do
# Get the mount point of the volume
mount_point=$(docker volume inspect "$volume_name" --format '{{ .Mountpoint }}')
# Use 'du' to calculate the volume's size and print it with the volume name
echo "Volume: $volume_name"
echo "Size: $(sudo du -sh "$mount_point" | cut -f1)"
echo "-------------------------------------"
done
To use this script:
list_docker_volumes_sizes.sh
.chmod +x list_docker_volumes_sizes.sh
../list_docker_volumes_sizes.sh
. You might need to run it as root or with sudo
depending on your Docker setup and permissions, like so: sudo ./list_docker_volumes_sizes.sh
.This script will print the name and size of each volume, followed by a separator line for clarity. Note that the size reported is the actual disk usage, not the allocated space, which might be different especially for sparse files or volumes with filesystems that support compression or deduplication.
To enhance the script for listing Docker volumes and their sizes, incorporating the set -Eeuo pipefail
option will make it more robust by handling errors more strictly. Also, allowing a size unit argument similar to the du
command improves its flexibility. Here’s how you can modify the script:
set -Eeuo pipefail
ensures that the script exits immediately if a command fails, if an undefined variable is used, or if a command in a pipeline fails.-sh
(human-readable sizes, summarizing).Here's the revised script:
bash#!/bin/bash
# Make script exit on any error, unset variable usage, and fail on pipe errors
set -Eeuo pipefail
# Check if a size unit argument is provided; default to '-sh' if not
size_unit="${1:--sh}"
# Function to print volume name and its size using specified size unit
list_volume_sizes() {
docker volume ls --format "{{.Name}}" | while read -r volume_name; do
# Get the mount point of the volume
mount_point=$(docker volume inspect "$volume_name" --format '{{ .Mountpoint }}')
# Calculate the volume's size with the specified unit
volume_size=$(sudo du $size_unit "$mount_point" | cut -f1)
# Print size and volume name in the desired format
printf "%s\t%s\n" "$volume_size" "$volume_name"
done
}
# Execute the function
list_volume_sizes
list_docker_volumes_sizes.sh
.chmod +x list_docker_volumes_sizes.sh
../list_docker_volumes_sizes.sh
or with sudo
if required../list_docker_volumes_sizes.sh -sm
or another valid du
option, e.g., -sk
for kilobytes. Use sudo
if necessary.This script will now print each volume's size in the specified unit (or human-readable format by default) followed by the volume name, separated by a tab, making it more readable and allowing for easy parsing or redirection into a file or another command.