# devops / docker / data

Persistent Data with Storage & Volumes

Containers are temporary by nature. A volume is a storage mechanism that lives outside of containers, ensuring your data remains completely safe even if the container is stopped, deleted, or recreated.

$ docker volume ls
DRIVERVOLUME NAME
localak
localmyvol
StatusData securely persisted
/var/lib
Host Path
Safe
State
# storage-types

Ways to Store Data

Docker provides multiple mechanisms to manage data. Choosing the right one depends on whether you want Docker to manage the storage, or if you need direct access from the host OS.

managed

Docker Volume

Fully managed by Docker and stored securely under /var/lib/docker/volumes. This is the best choice for persisting database files or app data.

host mapping

Bind Mount

Maps a specific host directory directly to a directory inside the container. Great for local development when you want code changes to reflect instantly.

in-memory

tmpfs

Stores data directly in RAM, making it blazing fast but completely temporary. Once the container stops, the data vanishes. Perfect for sensitive secrets or cache.

# drivers

Volume Drivers

Available Drivers

  • local

    The default. Stores files natively on the local host machine.

  • NFS

    Uses remote Network File System storage, allowing data to be shared across multiple Docker hosts.

  • type=tmpfs

    Forces the storage into RAM for high-speed, non-persistent access.

  • type=none

    Used explicitly for bind mounts, signaling that Docker does not manage the lifecycle of this storage.

Creating a tmpfs VolumeBASH
1 2
docker volume create --driver local \
  -o type=tmpfs -o device=tmpfs myvol
# commands

Storage Commands Cheat Sheet

Master these commands to create, mount, inspect, and clean up your Docker volumes.

terminal
# Volume Management
$ docker volume ls → List all volumes
$ docker volume create ak → Create a Docker-managed volume
$ docker volume inspect ak → Show mount point, driver, etc.
$ docker volume rm ak → Delete a specific volume
$ docker volume prune → Remove ALL unused volumes
 
# Mounting Volumes (Run)
$ docker run -d --mount source=ak,destination=/app/data ubuntu
↳ Attaches volume 'ak' inside the container
$ docker run -d -v ak:/app/data ubuntu
↳ Shorthand syntax for mounting volumes
$ docker run -d -v /host/path:/container/path ubuntu
↳ Bind mount a direct host directory
$ docker run -d --tmpfs /app/cache:rw,size=64m ubuntu
↳ Create a temporary RAM-based filesystem
 
# Inspection & Cleanup
$ docker exec -it <id> df -h → Check mounted space inside container
$ ls /var/lib/docker/volumes → View raw volume data on the host
$ docker rm -v <id> → Delete container + its anonymous volumes