Docker Cheatsheet
Volumes
Use this Docker reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Volume Types
| Type | Syntax | Use case |
|---|---|---|
| Named volume | -v mydata:/app/data | Managed by Docker; survive container removal |
| Bind mount | -v /host/path:/ctr/path | Dev: sync host source into container |
| Anonymous volume | -v /app/data | Throwaway; removed with --rm |
| tmpfs mount | --tmpfs /run | In-memory; not persisted, not shared |
Named Volumes
docker volume create mydata # create docker volume ls # list docker volume inspect mydata # full metadata + mountpoint docker volume rm mydata # remove (must not be in use) docker volume prune # remove all unused volumes docker volume prune --filter "label!=keep" # Use in docker run docker run -d -v mydata:/app/data postgres:17
Bind Mounts
# Absolute path required docker run -d -v /home/user/app:/app node:22 npm start # Current directory shorthand docker run -it -v "$(pwd)":/app -w /app node:22 bash # Read-only bind mount docker run -v "$(pwd)/config":/etc/app/config:ro myapp
tmpfs Mounts
docker run --tmpfs /run:rw,noexec,nosuid,size=64m myapp
Compose Volume Syntax
services:
db:
image: postgres:17
volumes:
- pgdata:/var/lib/postgresql/data # named volume
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro # bind mount
app:
image: myapp
volumes:
- .:/app # dev bind mount
- /app/node_modules # anonymous (exclude node_modules)
volumes:
pgdata: # Docker-managed named volume
# External volume (created outside Compose)
external_vol:
external: true
# NFS volume
nfs_data:
driver: local
driver_opts:
type: nfs
o: addr=192.168.1.10,rw
device: ":/exports/data"Backup and Restore
# Backup a volume to a tar archive docker run --rm \ -v mydata:/data \ -v "$(pwd)":/backup \ alpine tar czf /backup/mydata.tar.gz -C /data . # Restore from archive docker run --rm \ -v mydata:/data \ -v "$(pwd)":/backup \ alpine tar xzf /backup/mydata.tar.gz -C /data
Inspecting Volume Contents
# Spin up a throwaway container to browse docker run --rm -it -v mydata:/data alpine sh # Or copy out a file docker run --rm -v mydata:/data alpine cat /data/file.txt
Volume Drivers
# AWS EFS (via rexray or native plugin) docker volume create \ --driver rexray/efs \ --opt size=10 \ efsvolume docker plugin install rexray/s3fs S3FS_REGION=us-east-1
For production stateful workloads, prefer named volumes over bind mounts — they are portable across host paths and manageable via
docker volumecommands.