Docker Local Volumes Cheatsheet
Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers. They are directories managed by Docker, separate from the container's lifecycle.
What is a Docker Volume?
A Docker volume is a directory on the host's filesystem. This is where Docker stores the data. On most Linux systems, the default location is /var/lib/docker/volumes.
This directory is managed entirely by Docker. Any data your container writes to the mounted volume is actually saved to this directory on the host, ensuring the data persists even after the container is removed.
Common Volume Commands
| Command | Description |
|---|---|
docker volume create [VOLUME_NAME] | Creates a new named volume. Docker will manage this volume and its data. |
docker volume ls | Lists all volumes on your system. This is a good way to see what data you have persisted. |
docker volume inspect [VOLUME_NAME] | Shows detailed information about a specific volume, including its mount point on the host. |
docker volume rm [VOLUME_NAME] | Removes a specific volume. This will permanently delete any data stored in it. |
docker volume prune | Removes all unused local volumes. This is a powerful command for cleaning up your system. |
Running a Container with a Volume
To use a volume, you mount it to a specific path in your container using the -v flag.
# Create a volume first
docker volume create my-data
# Run a container and mount the volume
docker run -d -v my-data:/app/data --name my-container my-imageIn this example:
-v my-data:/app/data: Mounts the volume namedmy-datato the/app/datadirectory inside the container.- Any data written to
/app/datain the container will be persistently stored in themy-datavolume on the host.