A volume is a storage mechanism that lives outside of containers, which means your data remains safe even if the container is deleted.
Containers are temporary by nature, but volumes make data persistent.
Bind Mount
Maps a specific host directory directly to a directory inside the container.
Docker Volume
Fully managed by Docker and stored under:
/var/lib/docker/volumes
tmpfs
Stores data in RAM, making it very fast but temporary.
Once the container stops, the data is lost.
To list all existing volumes on the system:
docker volume ls
docker volume lsTo create a new Docker-managed volume:
docker volume create ak
docker volume create akTo view detailed information about a volume:
docker volume inspect ak
docker volume inspect akdocker run -d -it --name ubuntu \ --mount source=ak,destination=/var/app/data ubuntu /bin/bash
docker run -d -it --name ubuntu \
--mount source=ak,destination=/var/app/data ubuntu /bin/bashIn this example, the Docker volume named ak is mounted inside the container at /var/app/data.
Any data written to this path is stored outside the container and remains available even after the container is removed.
local
Stores files on the local host machine.
NFS
Uses remote storage, allowing data to be shared across multiple systems.
type=tmpfs
Stores data in RAM for high-speed access.
type=none
Used for bind mounts, where Docker does not manage the storage.
docker volume create --driver local \ -o type=tmpfs -o device=tmpfs myvol
docker volume create --driver local \
-o type=tmpfs -o device=tmpfs myvolThis creates a RAM-based volume, which is fast but non-persistent.
docker volume ls
docker volume lsShows all volumes available on the Docker host.
docker volume create ak
docker volume create akCreates a new Docker-managed volume named ak.
docker volume create --driver local ak
docker volume create --driver local akCreates a volume using a specific driver.
docker volume inspect ak
docker volume inspect akDisplays detailed information such as mount point, driver, and options.
docker volume rm ak
docker volume rm akDeletes the specified volume (only if not in use).
docker volume rm vol1 vol2 vol3
docker volume rm vol1 vol2 vol3Deletes multiple volumes at once.
docker volume prune
docker volume pruneRemoves all volumes that are not currently used by any container.
docker run -d \ --mount source=ak,destination=/app/data ubuntu
docker run -d \
--mount source=ak,destination=/app/data ubuntuAttaches volume ak to a container.
docker run -d -v ak:/app/data ubuntu
docker run -d -v ak:/app/data ubuntuAlternative shorthand for mounting a volume.
docker run -d -v /host/path:/container/path ubuntu
docker run -d -v /host/path:/container/path ubuntuMaps a host directory directly into the container.
docker run -d \ --tmpfs /app/cache:rw,size=64m ubuntu
docker run -d \
--tmpfs /app/cache:rw,size=64m ubuntuCreates a temporary in-memory filesystem.
docker volume create --driver local \ -o type=tmpfs -o device=tmpfs myvol
docker volume create --driver local \
-o type=tmpfs -o device=tmpfs myvolCreates a RAM-based Docker volume.
docker exec -it <container_id> df -h
docker exec -it <container_id> df -hShows storage and mounted volumes inside a container.
ls /var/lib/docker/volumes
ls /var/lib/docker/volumesLists Docker volume directories on the host system.
docker rm <container_id>
docker rm <container_id>Deletes the container but keeps the attached volumes.
docker rm -v <container_id>
docker rm -v <container_id>Deletes the container along with its anonymous volumes.