Lesson 4 of 9 · 10 min · Running real applications

Volumes, bind mounts and persistent data

Covers: Docker Engine 29 on Ubuntu 24.04, alpine 3.20. Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • Why data written inside a container is lost
  • Named volumes, bind mounts and tmpfs mounts, and how to choose
  • -v and --mount syntax, read-only mounts
  • File ownership problems on bind mounts
  • Backing up and restoring volume data

In simple words

A container is like a hotel room: when you check out, everything you left inside is cleared away. A database can't live like that, so its data is kept outside the room, in a locker that stays. That locker is a volume. This lesson shows the ways to keep data and how to back it up.

Words to know

  • Volume: storage managed by Docker that stays when the container is removed.
  • Bind mount: a folder from your own machine made visible inside the container.
  • Persistent data: data that must outlive the container, such as a database's files.
  • Read-only mount: storage the container can read but not change.

The container filesystem is temporary

A container's writable layer is deleted when the container is removed. A database that writes to its container filesystem loses all data on docker rm or on a redeploy. We showed this directly:

docker run --name tmpc alpine:3.20 sh -c 'echo gone > /tmp/x'
docker rm tmpc
docker run --rm alpine:3.20 ls /tmp/x
ls: /tmp/x: No such file or directory

For data that must outlive the container, mount storage from outside into the container.

Three kinds of mount

Type Where the data lives Managed by Typical use
Named volume a directory under Docker's own area (/var/lib/docker/volumes/...) Docker databases, application data: the default choice
Bind mount any path you choose on the host you development (live code), config files, reading host data
tmpfs mount memory only (Linux) the kernel scratch space or secrets that must never touch disk

Named volumes

docker volume create demo-data
docker run --rm -v demo-data:/data alpine:3.20 sh -c 'echo "saved at $(date +%T)" > /data/note.txt; ls -l /data'
docker run --rm -v demo-data:/data alpine:3.20 cat /data/note.txt
total 4
-rw-r--r--    1 root     root            18 Nov 12 13:19 note.txt
saved at 13:19:00

The second container, a completely new one, reads what the first wrote. docker volume ls lists volumes, docker volume inspect demo-data shows where Docker keeps it, and docker volume rm demo-data deletes the data. Volumes:

  • are created automatically if missing (-v name:/path),
  • are initialised with the image's content at that path the first time (an empty volume mounted over /var/lib/postgresql/data gets whatever the image had there),
  • keep working across container upgrades, so you replace the container and keep the data,
  • are independent of the host's directory layout, and can use drivers for network or cloud storage.

Bind mounts

A bind mount maps a host path into the container:

docker run --rm -v "$PWD/hostdir:/mnt/host" alpine:3.20 sh -c 'cat /mnt/host/h.txt; echo "written in container" > /mnt/host/c.txt'
cat hostdir/c.txt
from host
written in container

Changes are visible on both sides immediately, which is perfect for development (edit code on the host, see it run in the container) and for supplying a config file. The downsides: the container depends on the host's directory layout, and file ownership needs care (below). In production prefer volumes or baked-in config.

Use an absolute path ($PWD/hostdir, not ./hostdir with -v); with -v, a path that does not start with / or . is treated as a volume name, a common surprise. If the host path does not exist, -v silently creates a directory there (as root), while --mount refuses with an error.

tmpfs

docker run --rm --tmpfs /scratch:size=10m alpine:3.20 df -h /scratch

shows a 10 MB tmpfs filesystem in memory: fast, never written to disk, gone when the container stops.

Read-only mounts and the `--mount` syntax

Add :ro to prevent the container from changing data it only needs to read:

docker run --rm -v "$PWD/hostdir:/mnt/host:ro" alpine:3.20 sh -c 'echo x > /mnt/host/z.txt'
sh: can't create /mnt/host/z.txt: Read-only file system

--mount is the longer, explicit and less error-prone form, and the one to prefer in scripts:

docker run --rm --mount type=volume,src=demo-data,dst=/data,readonly alpine:3.20 ...
docker run --rm --mount type=bind,src="$PWD/hostdir",dst=/mnt/host alpine:3.20 ...
docker run --rm --mount type=tmpfs,dst=/scratch,tmpfs-size=10m alpine:3.20 ...

Combine --read-only (read-only root filesystem for the container) with writable tmpfs or volume mounts for the few paths that need writing: a strong hardening step.

Permissions and ownership on bind mounts

Linux permissions use numeric user ids (Linux course). The container's root (uid 0) is also root on the host as far as file ownership goes: after our container wrote c.txt into the bind-mounted directory, ls -l on the host showed its owner as root. Consequences:

  • Files a root container creates in your project directory are owned by root, and you may need sudo to delete them.
  • A container running as uid 10001 can write to a bind-mounted host directory only if that directory permits it (chown 10001 hostdir, or run with --user $(id -u):$(id -g) in development).
  • For named volumes Docker sets the initial ownership from the image's directory, so make sure the directory exists in the image with the right owner (RUN mkdir /data && chown app:app /data before USER app).

When a service logs "permission denied" writing to its data directory, compare the container user's uid with the mount's owner first.

Volumes in Compose and Kubernetes

Compose declares volumes in the file (next lessons). In Kubernetes the equivalents are PersistentVolumeClaims backed by cloud disks; the idea is identical: storage whose lifecycle is independent of the pod.

Backup and restore of a volume

A volume is just files, so use a throwaway container to archive it:

docker run --rm -v demo-data:/data:ro -v "$PWD:/backup" alpine:3.20 tar czf /backup/demo-data.tgz -C /data .
tar tzf demo-data.tgz
./
./note.txt

Restore by reversing it into an (empty) volume with tar xzf. For databases, copying the files of a running database can give a corrupt backup; use the database's own dump tool (pg_dump, mysqldump) and treat the volume as the live data only. Always test a restore.

Cleanup

docker volume ls -f dangling=true lists volumes no container uses; docker volume prune deletes them with their data. docker rm -v and docker compose down -v also remove volumes. Read before you confirm.

Common mistakes

  • Storing a database in the container filesystem.
  • docker compose down -v or volume prune on data you need.
  • A relative path in -v (becomes a volume name) or a missing host path silently created as root-owned.
  • Ownership mismatch between container uid and host directory.
  • Bind-mounting the Docker socket or / into containers (gives root-equivalent access to the host).
  • Backing up a live database's files instead of a proper dump.

Try it yourself

  1. Run a PostgreSQL container with a named volume, create a table, remove the container, start a new one with the same volume and confirm the table is still there.
  2. Bind-mount a source directory into a container, edit a file on the host and watch the change inside.
  3. Mount a directory read-only and show the write failing; then find the owner of files created by a root container on the host.
  4. Back up and restore a volume with tar.

Check yourself

What is the difference between a named volume and a bind mount?

A named volume is created and managed by Docker in its own storage area and is addressed by name. A bind mount maps an exact host path into the container. Volumes suit persistent application data; bind mounts suit development files and configuration.

Why did the data disappear after removing the container?

It was written to the container's writable layer, which is deleted with the container. Mount a volume at the data path instead.

How do you stop a container from modifying a mounted directory?

Mount it read-only: -v path:/target:ro or --mount ...,readonly.

Files created in a bind-mounted directory are owned by root on the host. Why?

The container process ran as uid 0 and the kernel records numeric ids, so on the host those files belong to uid 0. Run the container with --user set to your uid or fix ownership.

Interview questions

Where should the data of a database container be stored, and why?

In a named volume (or cloud block storage in orchestrators) mounted at the database data directory, so it survives container replacement, upgrades and crashes.

What can go wrong with bind mounts in production?

They tie the container to a specific host path and layout, create ownership and permission problems, and give the container access to host files; volumes or baked-in config are safer and more portable.

How do you share data between two containers?

Mount the same named volume in both (carefully, with one writer), or use a network service (database, object storage, message queue), which scales better.

Summary

  • Container filesystems are temporary; use volumes for persistent data, bind mounts for development and config, tmpfs for memory-only data.
  • Prefer --mount; use :ro/readonly wherever a container only reads.
  • Mind uid/gid ownership on bind mounts; root containers create root-owned files.
  • Back up with a helper container (or the database's dump tool) and test restores; never prune volumes carelessly.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Docker and containers for DevOps Engineer and scores every answer, with what to fix.