08. Volumes & Storage
By design, containers are meant to be ephemeral and disposable. However, databases, user uploads, and application logs require persistent storage that survives container crashes and restarts. Understanding Docker’s storage mechanisms is essential for stateful applications.
8.1 Container Storage
Section titled “8.1 Container Storage”Writable Container Layer
Section titled “Writable Container Layer”When Docker starts a container from an image, it mounts a thin “read-write” layer on top of the immutable image layers. Any files created or modified by the application (e.g., writing a log file to /var/log/app.log) happen exclusively inside this thin layer.
Ephemeral Storage
Section titled “Ephemeral Storage”Storage inside the writable layer is ephemeral (temporary). It is tightly coupled to the lifecycle of the container instance.
Container Deletion and Data Loss
Section titled “Container Deletion and Data Loss”If a container crashes and is removed (or if you run docker rm), the writable layer is completely destroyed. All data inside it is permanently lost. You must never store database data or important user uploads in the writable layer.
8.2 Volumes
Section titled “8.2 Volumes”Volumes are the preferred and safest mechanism for persisting data in Docker. A volume is a dedicated storage area managed directly by Docker on the host machine (usually in /var/lib/docker/volumes/).
Named Volumes
Section titled “Named Volumes”You explicitly give the volume a name (e.g., postgres-data). Named volumes are easy to identify, backup, and attach to multiple containers.
docker run -v postgres-data:/var/lib/postgresql/data postgresAnonymous Volumes
Section titled “Anonymous Volumes”If you don’t provide a name, Docker generates a massive, random hash for the volume name. These are notoriously difficult to track and clean up. (Avoid using anonymous volumes in production).
Volume Lifecycle
Section titled “Volume Lifecycle”Volumes have a completely separate lifecycle from containers. If you delete a container that is using a named volume, the volume and all its data remain perfectly intact on the host. If you spin up a new container and attach the same named volume, it instantly picks up right where the old container left off.
Volume Permissions
Section titled “Volume Permissions”Because Docker manages the volume directory on the host, file permissions are generally handled automatically for the container user, reducing the “Permission Denied” errors commonly seen with Bind Mounts.
8.3 Bind Mounts
Section titled “8.3 Bind Mounts”While Volumes are managed by Docker, Bind Mounts are managed by you.
Host Directory ↓ Container Directory
Section titled “Host Directory ↓ Container Directory”A Bind Mount explicitly takes an exact directory on your host machine and maps it directly into the container.
docker run -v /Users/syn606/project/code:/app my-node-app- Whatever happens on the Host immediately reflects in the Container.
- Whatever the Container does immediately modifies files on the Host.
Use Cases:
- Local Development: Developers use bind mounts to mount their source code into a container so the application can hot-reload changes without rebuilding the image every time.
- Warning: Do not use Bind Mounts for databases in production. File permission conflicts (host user vs container user) and OS file-system differences (macOS/Windows vs Linux) can severely impact performance and stability.
8.4 tmpfs Mounts
Section titled “8.4 tmpfs Mounts”Temporary Memory-Backed Storage
Section titled “Temporary Memory-Backed Storage”Unlike Volumes and Bind Mounts, tmpfs mounts are never written to the host’s hard drive. They are stored purely in the host’s RAM.
Use Cases
Section titled “Use Cases”- Security: Storing highly sensitive API keys or temporary cryptographic secrets that should never be written to a physical disk.
- Performance: High-speed temporary scratch space for an application that needs to rapidly write and delete massive amounts of non-essential state data.
docker run --tmpfs /app/temp-cache my-app8.5 Volume Commands
Section titled “8.5 Volume Commands”docker volume ls
Section titled “docker volume ls”Lists all volumes managed by Docker on the host machine.
docker volume create
Section titled “docker volume create”Explicitly creates a named volume before attaching it to a container.
docker volume create my-database-voldocker volume inspect
Section titled “docker volume inspect”Provides detailed JSON metadata about a volume, most importantly the Mountpoint, which tells you exactly where the data physically lives on the Linux host (e.g., /var/lib/docker/volumes/...).
docker volume inspect my-database-voldocker volume rm
Section titled “docker volume rm”Deletes a volume and all the persistent data inside it. (You cannot delete a volume if it is currently mounted to a container).
docker volume rm my-database-vol8.6 Database Storage
Section titled “8.6 Database Storage”Databases are the most common use case for Docker Volumes. Without a volume, a database reset means total data wipe.
PostgreSQL Volume
Section titled “PostgreSQL Volume”Postgres stores its raw data inside /var/lib/postgresql/data. You must map a named volume to this exact directory.
docker run -d \ --name my-postgres \ -e POSTGRES_PASSWORD=secret \ -v pgdata:/var/lib/postgresql/data \ postgres:14MySQL Volume
Section titled “MySQL Volume”MySQL stores its raw data inside /var/lib/mysql.
docker run -d \ --name my-mysql \ -e MYSQL_ROOT_PASSWORD=secret \ -v mysqldata:/var/lib/mysql \ mysql:8Backup
Section titled “Backup”To backup a volume, you can temporarily mount it to a utility container (like ubuntu), tar the contents, and bind mount it out to your host.
docker run --rm \ -v pgdata:/volume \ -v $(pwd):/backup \ ubuntu tar cvf /backup/pgdata_backup.tar /volumeRestore
Section titled “Restore”To restore, reverse the process. Unpack a .tar file directly into a named volume.
docker run --rm \ -v pgdata:/volume \ -v $(pwd):/backup \ ubuntu bash -c "cd /volume && tar xvf /backup/pgdata_backup.tar --strip 1"Persistence
Section titled “Persistence”As long as the named volume pgdata exists, you can safely docker stop, docker rm, and docker pull newer versions of the Postgres image, and your data will remain completely safe.