DK

Volumes & Storage

Docker · 7 entries

Named Volumes

syntax
docker volume create <name>
docker run -v <name>:<container_path> <image>
example
docker volume create pgdata
docker run -d -v pgdata:/var/lib/postgresql/data postgres:16
output
pgdata

Note Named volumes are managed by Docker and stored under /var/lib/docker/volumes/ on Linux. They persist independently of containers and survive docker rm. This is the recommended way to persist database data.

Bind Mounts

syntax
docker run -v <host_path>:<container_path>[:<options>] <image>
docker run --mount type=bind,source=<path>,target=<path> <image>
example
docker run -v $(pwd)/src:/app/src myapp:1.0
docker run --mount type=bind,source=$(pwd)/config,target=/app/config,readonly myapp:1.0

Note Bind mounts give the container direct access to host files — changes are reflected immediately in both directions. On macOS and Windows, bind mount performance can be noticeably slow for large node_modules directories.

tmpfs Mounts (RAM Only)

syntax
docker run --tmpfs <container_path>[:<options>] <image>
docker run --mount type=tmpfs,target=<path> <image>
example
docker run -d --tmpfs /tmp:rw,noexec,size=100m myapp:1.0
docker run -d --mount type=tmpfs,target=/app/cache,tmpfs-size=67108864 myapp:1.0

Note Data is stored in memory and disappears when the container stops. Use for sensitive data (secrets, tokens) that should never be written to disk, or for scratch space where speed matters more than persistence.

Inspect a Volume

syntax
docker volume inspect <name>
example
docker volume inspect pgdata
output
[
  {
    "Name": "pgdata",
    "Driver": "local",
    "Mountpoint": "/var/lib/docker/volumes/pgdata/_data",
    "CreatedAt": "2025-11-15T10:30:00Z"
  }
]

Note The Mountpoint shows where the data lives on the host filesystem. On Docker Desktop (macOS/Windows), this path is inside the Linux VM, not directly accessible from your host.

List & Remove Volumes

syntax
docker volume ls
docker volume rm <name>
docker volume prune
example
docker volume ls
docker volume rm pgdata
docker volume prune
output
DRIVER    VOLUME NAME
local     pgdata
local     redis-data
local     a1b2c3d4e5f6...

Note docker volume prune removes ALL volumes not used by any container. This destroys data permanently with no undo. The long hex-named volumes are anonymous volumes — usually safe to remove, but verify first.

Backup a Volume

syntax
docker run --rm -v <volume>:/source -v $(pwd):/backup <image> tar czf /backup/<file>.tar.gz -C /source .
example
docker run --rm \
  -v pgdata:/source:ro \
  -v $(pwd):/backup \
  alpine:3.19 \
  tar czf /backup/pgdata-backup.tar.gz -C /source .

Note This spins up a temporary Alpine container that mounts the volume read-only and your current directory, then creates a compressed tar archive. To restore, reverse the process by extracting the tar into a new volume.

Anonymous Volumes

syntax
docker run -v <container_path> <image>
# or VOLUME in Dockerfile
example
docker run -d -v /var/cache myapp:1.0
# Creates an anonymous volume with a random hex name

Note Anonymous volumes get a random hash name and are easy to lose track of. They accumulate over time and waste disk space. Prefer named volumes so you can identify and manage your data. Use docker volume prune periodically to clean up orphans.