3. Storage & Volumes

📂 Docker Storage – Volumes

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.


🔹 Types of Storage

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.


🔹 Volume Commands

To list all existing volumes on the system:

docker volume ls
docker volume ls

To create a new Docker-managed volume:

docker volume create ak
docker volume create ak

To view detailed information about a volume:

docker volume inspect ak
docker volume inspect ak

👉 Example: Mount a Volume Inside a Container

docker 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/bash

In 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.


🔹 Volume Drivers

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.


👉 Example: Create a tmpfs Volume

docker volume create --driver local \
  -o type=tmpfs -o device=tmpfs myvol
docker volume create --driver local \
  -o type=tmpfs -o device=tmpfs myvol

This creates a RAM-based volume, which is fast but non-persistent.


List All Volumes

docker volume ls
docker volume ls

Shows all volumes available on the Docker host.


Create a Volume

docker volume create ak
docker volume create ak

Creates a new Docker-managed volume named ak.


Create a Volume with Driver Options

docker volume create --driver local ak
docker volume create --driver local ak

Creates a volume using a specific driver.


Inspect a Volume

docker volume inspect ak
docker volume inspect ak

Displays detailed information such as mount point, driver, and options.


Remove a Volume

docker volume rm ak
docker volume rm ak

Deletes the specified volume (only if not in use).


Remove Multiple Volumes

docker volume rm vol1 vol2 vol3
docker volume rm vol1 vol2 vol3

Deletes multiple volumes at once.


Remove All Unused Volumes

docker volume prune
docker volume prune

Removes all volumes that are not currently used by any container.


Use Volume While Running a Container (Mount)

docker run -d \
  --mount source=ak,destination=/app/data ubuntu
docker run -d \
  --mount source=ak,destination=/app/data ubuntu

Attaches volume ak to a container.


Use Volume with Short Syntax

docker run -d -v ak:/app/data ubuntu
docker run -d -v ak:/app/data ubuntu

Alternative shorthand for mounting a volume.


Bind Mount (Host Directory)

docker run -d -v /host/path:/container/path ubuntu
docker run -d -v /host/path:/container/path ubuntu

Maps a host directory directly into the container.


tmpfs Mount (RAM Storage)

docker run -d \
  --tmpfs /app/cache:rw,size=64m ubuntu
docker run -d \
  --tmpfs /app/cache:rw,size=64m ubuntu

Creates a temporary in-memory filesystem.


Create tmpfs Volume

docker volume create --driver local \
  -o type=tmpfs -o device=tmpfs myvol
docker volume create --driver local \
  -o type=tmpfs -o device=tmpfs myvol

Creates a RAM-based Docker volume.


Check Mounted Volumes Inside Container

docker exec -it <container_id> df -h
docker exec -it <container_id> df -h

Shows storage and mounted volumes inside a container.


View Volume Data on Host

ls /var/lib/docker/volumes
ls /var/lib/docker/volumes

Lists Docker volume directories on the host system.


Remove Container Without Deleting Volume

docker rm <container_id>
docker rm <container_id>

Deletes the container but keeps the attached volumes.


Remove Container and Its Anonymous Volumes

docker rm -v <container_id>
docker rm -v <container_id>

Deletes the container along with its anonymous volumes.