Skip to content

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.

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.

Storage inside the writable layer is ephemeral (temporary). It is tightly coupled to the lifecycle of the container instance.

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.

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/).

You explicitly give the volume a name (e.g., postgres-data). Named volumes are easy to identify, backup, and attach to multiple containers.

Terminal window
docker run -v postgres-data:/var/lib/postgresql/data postgres

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).

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.

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.

While Volumes are managed by Docker, Bind Mounts are managed by you.

A Bind Mount explicitly takes an exact directory on your host machine and maps it directly into the container.

Terminal window
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.

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.

  • 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.
Terminal window
docker run --tmpfs /app/temp-cache my-app

Lists all volumes managed by Docker on the host machine.

Explicitly creates a named volume before attaching it to a container.

Terminal window
docker volume create my-database-vol

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/...).

Terminal window
docker volume inspect my-database-vol

Deletes a volume and all the persistent data inside it. (You cannot delete a volume if it is currently mounted to a container).

Terminal window
docker volume rm my-database-vol

Databases are the most common use case for Docker Volumes. Without a volume, a database reset means total data wipe.

Postgres stores its raw data inside /var/lib/postgresql/data. You must map a named volume to this exact directory.

Terminal window
docker run -d \
--name my-postgres \
-e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data \
postgres:14

MySQL stores its raw data inside /var/lib/mysql.

Terminal window
docker run -d \
--name my-mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-v mysqldata:/var/lib/mysql \
mysql:8

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.

Terminal window
docker run --rm \
-v pgdata:/volume \
-v $(pwd):/backup \
ubuntu tar cvf /backup/pgdata_backup.tar /volume

To restore, reverse the process. Unpack a .tar file directly into a named volume.

Terminal window
docker run --rm \
-v pgdata:/volume \
-v $(pwd):/backup \
ubuntu bash -c "cd /volume && tar xvf /backup/pgdata_backup.tar --strip 1"

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.