Stage 5 - Data Persistence

Containers are disposable by design. That is good for deployment, but dangerous for data. This stage explains where Docker stores state and when you must move it out of the container.

Docker Data Persistence

First question: will the data be needed later?

Every container has a writable layer. It is convenient for temporary files, but it belongs to that container. Remove the container, and the writable layer is gone. This is why a database without a volume loses data after docker rm.

Temporary file -> writable layer
Database data -> named volume
Source code -> bind mount
Production data -> external service or managed volume

Volumes and bind mounts

A named volume is Docker-managed storage with a stable name. It is the usual local choice for PostgreSQL, MySQL, Redis snapshots, and any state that should survive container replacement. Anonymous volumes also persist, but they are hard to recognize later.

A bind mount connects a specific host path to a container path. It is excellent for source code during development because edits on the host immediately appear inside the container. It is less portable because host paths and permissions differ between machines.

Database containers are still databases

Running PostgreSQL in Docker does not change how databases think about durability. PostgreSQL still writes data files, uses write-ahead logs, and needs a consistent backup strategy. Docker only decides where those files live from the container point of view.

For local development, a named volume is usually enough. For production, you need a stronger plan: managed database, tested backups, restore procedure, monitoring, and clear ownership. A Docker volume alone is not a backup.

The important distinction is between persistence and recoverability. A volume can persist data on one host. A backup lets you recover data after host loss, accidental deletion, or corruption.

Bind mounts are not bad

Bind mounts are sometimes described as less safe than volumes, but they solve a different problem. They are useful when the host file must be visible inside the container: source code, configuration files, certificates in development, or generated reports.

The tradeoff is portability. A bind mount depends on the host path and host permissions. A path that works on macOS may not exist on Linux. A UID that can write on one machine may fail on another. This is why bind mounts are common in development and more carefully controlled in production.

Where Docker data lives

Volumes are stored on the Docker host. On Linux this is usually inside Docker’s data directory. On Docker Desktop, the volume is inside Docker Desktop’s Linux virtual machine, not directly next to your project files.

This explains a common confusion: a named volume survives container removal, but the user may not see it in the project folder. Docker manages it. To inspect it, use Docker commands, not Finder or Explorer.

On a VPS, volumes live on that VPS disk. If the VPS is deleted, the volume is gone too unless you have backups or external storage. Persistence on one host is not the same as disaster recovery.

Commands to run and inspect

Run these commands in a terminal on the machine where Docker is installed. For local learning, that is your laptop terminal. For a VPS, first connect to the server with SSH, then run the commands there. Do not paste commands blindly: run one command, inspect what changed, then run the next one.

docker volume create pgdata
docker run -d --name pg -e POSTGRES_PASSWORD=secret -v pgdata:/var/lib/postgresql/data postgres:16
docker volume inspect pgdata
docker exec pg pg_dump -U postgres postgres > backup.sql

Reference table

Topic What it means Practical consequence
Writable layer Temporary container state Do not store important data
Named volume Docker-managed durable data Good default for local databases
Bind mount Host path mounted into container Great for source code, less portable

Storage decision rule

Ask one question before choosing storage: should this data survive container replacement? If no, the writable layer may be acceptable. If yes, use a named volume, bind mount, external service, or explicit backup strategy.

For databases, prefer database-native backups such as dumps. Copying raw files without considering consistency can produce backups that look valid but fail during restore.

Glossary
container
An isolated runtime environment that packages an application process with its dependencies and filesystem.
image
A read-only template used to create containers.
Dockerfile
A file with instructions for building a Docker image.
layer
A filesystem change in a Docker image that can be cached and reused.
registry
A server or service that stores and distributes container images.
tag
A human-readable image label such as postgres:16 that points to an image version.
digest
A content-based identifier that points to an exact image or artifact version.
volume
Docker-managed persistent storage that can survive container replacement.
bind mount
A mount that connects a specific host path into a container.
network
The communication layer that lets containers, hosts, and services reach each other.
port mapping
A Docker configuration that exposes a container port through a host port.
environment variable
A named runtime configuration value provided to a process.
Docker Compose
A Docker tool for defining and running multi-container applications from a YAML file.
service
A logical application component, often one process or deployable unit, such as an API or database.
entrypoint
The main executable command configured for a container image.
CMD
Dockerfile instruction that provides default command arguments or a default command.
build context
The set of files sent to Docker when building an image.