Lesson 09 intermediate

Docker Volumes

Keep data alive across container restarts with named volumes, use bind mounts for development, back up and restore volumes, and avoid permission surprises.

A container’s writable layer disappears when the container is removed. That is perfect for stateless apps and fatal for databases. Docker gives you three ways to store data outside that layer, and this lesson shows when to use each.

Prove the problem first

$ docker run --name temp alpine sh -c 'echo "important" > /data.txt'
$ docker rm temp
temp
$ docker run --rm alpine cat /data.txt
cat: can't open '/data.txt': No such file or directory

The file lived in the first container’s writable layer and went away with it.

Three kinds of mounts

Named volumeBind mounttmpfs
Where the data livesManaged by Docker under /var/lib/docker/volumesAny host path you chooseHost memory only
Survives container removalYesYes (it is your folder)No
Best forDatabases and app data in productionSource code and config during developmentSecrets and scratch files that must not touch disk
Syntax-v pgdata:/var/lib/postgresql/data-v "$PWD":/app--tmpfs /tmp

Named volumes

Create a volume, then mount it where the application writes its data:

$ docker volume create pgdata
pgdata
$ docker run -d --name db \
  -e POSTGRES_PASSWORD=example \
  -v pgdata:/var/lib/postgresql/data \
  postgres:17
$ docker exec db psql -U postgres -c "CREATE TABLE notes (body text);"
CREATE TABLE

Now destroy the container and start a fresh one with the same volume. The table is still there:

$ docker rm -f db
db
$ docker run -d --name db -e POSTGRES_PASSWORD=example -v pgdata:/var/lib/postgresql/data postgres:17
$ docker exec db psql -U postgres -c "\dt"
         List of relations
 Schema | Name  | Type  |  Owner
--------+-------+-------+----------
 public | notes | table | postgres

This is exactly how you upgrade a containerized service: replace the container, keep the volume.

$ docker volume ls
DRIVER    VOLUME NAME
local     pgdata
$ docker volume inspect --format '{{.Mountpoint}}' pgdata
/var/lib/docker/volumes/pgdata/_data

Tip: if you mount an empty named volume over a folder that already has files in the image, Docker copies those files into the volume first. Bind mounts never do this: they hide whatever the image had at that path.

Bind mounts for development

A bind mount maps a folder from your machine into the container. Edit code in your editor and the container sees it instantly:

$ docker run --rm -it -p 3000:3000 \
  -v "$PWD":/app -w /app \
  node:22-alpine sh -c "npm install && npm run dev"

Add :ro to make a mount read-only, which is ideal for configuration files:

$ docker run -d -p 8080:80 -v "$PWD/nginx.conf":/etc/nginx/nginx.conf:ro nginx:1.27

Volumes in Compose

Compose declares volumes next to the services that use them, so the whole stack is described in one place:

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - pgdata:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro

volumes:
  pgdata:

docker compose down keeps named volumes; docker compose down -v deletes them too, so use it with care.

Back up and restore a volume

A volume is just files, so the simplest portable backup is a temporary container that mounts the volume and writes a tar archive to your current folder:

$ docker run --rm -v pgdata:/data:ro -v "$PWD":/backup alpine \
  tar czf /backup/pgdata-2026-10-28.tgz -C /data .
$ ls -lh pgdata-*.tgz
-rw-r--r-- 1 root root 6.1M Oct 28 15:40 pgdata-2026-10-28.tgz

Restoring is the same command in reverse, into a new, empty volume:

$ docker volume create pgdata-restored
$ docker run --rm -v pgdata-restored:/data -v "$PWD":/backup alpine \
  tar xzf /backup/pgdata-2026-10-28.tgz -C /data

Important: stop the database (or use its own dump tool, such as pg_dump) before copying its files, otherwise the backup may be inconsistent. File-level copies are best for data that is not being written to.

Permissions

Files in a volume keep their numeric owner. If an image runs as user 10001 and the volume’s files belong to root, the app gets “permission denied”. Fix it once from a helper container, or set the owner in the Dockerfile before switching USER:

$ docker run --rm -v appdata:/data alpine chown -R 10001:10001 /data

Clean up

$ docker volume rm pgdata-restored
pgdata-restored
$ docker volume prune
WARNING! This will remove anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 240MB

Exercise

How do you back up the named volume pgdata into a tar file in your current folder?

  1. Copy /var/lib/docker to another disk while Docker is running
  2. Run docker volume export pgdata
  3. Run docker commit on the database container
  4. Start a temporary container that mounts the volume and the current folder, and tar one into the other
Show answer

Answer: D. For example docker run --rm -v pgdata:/data -v "$PWD":/backup alpine tar czf /backup/pgdata.tgz -C /data .. docker commit never includes volume data, and there is no volume export command.

Key takeaways

  • Anything written only inside a container is lost when the container is removed.
  • Use named volumes for persistent data, bind mounts for development and config, tmpfs for in-memory scratch.
  • Replace containers freely; keep the volume, and the data survives upgrades.
  • Back up volumes with a helper container and tar, or the database’s own dump tool.

What’s next

You have finished the Docker tutorial: you can build images, run and debug containers and keep their data safe. When you are ready to run containers across many servers, keep the Kubernetes Cheatsheet open and start deploying them to a cluster.

FAQ

Where are Docker volumes stored on disk?

On Linux, under /var/lib/docker/volumes/<name>/_data. On Docker Desktop they live inside its Linux VM, so use docker commands rather than browsing the host filesystem.

What is an anonymous volume?

A volume Docker creates with a random name, for example for a VOLUME instruction in an image when you do not mount anything there. They are easy to lose track of, so prefer named volumes.

Can two containers share one volume?

Yes, mount the same volume in both. It works well for read-only sharing; for two writers, the applications must coordinate, since Docker does not lock files for them.

↑ ↓ navigate Enter open Esc close