Storage: volumes and bind mounts

Where container data lives, named volumes, bind mounts with SELinux labels, file ownership for rootless containers, backup and restore, and data rules for production.
PodmanVolumesBind mountsSELinuxRootlessCore hour

Hour overview

The sample service counts its visits in /data/visits.txt. In this hour you find out what happens to that count when the container is removed, and you fix it. You learn when to use a named volume and when a bind mount is right, how SELinux labels affect a mount, why a rootless container may not be able to write to a host folder, and how to back up and restore a volume.

Time allocation. Storage types 8, named volumes 10, bind mounts 10, rootless ownership 12, backup and restore 12, production rules 8.

Objectives. By the end of the hour you can:

  • say which data a container loses when it is removed, and which data survives
  • create a named volume, mount it, and show that its data outlives the container
  • mount a host folder read-only, and read the SELinux label option you need
  • explain how user namespaces map file owners, and fix a permission error in two ways
  • export a volume to a tar file, import it into a new volume, and prove the restore worked

Prerequisite. The image from hour 2. The lab uses port 8006 on 127.0.0.1.

Modules

Writable layer, volumes and bind mounts

Hour 4 · 00–08 · 8 min

A container has three places where data can live:

container
  /data  <- writable layer: lost when the container is removed
  /data  <- named volume: managed by Podman, survives the container
  /data  <- bind mount: a folder on the host, survives the container

Run the sample without a volume, count two visits, remove the container, and start a new one:

podman run -d --name store1 -p 127.0.0.1:8006:8000 localhost/podman-app:dev
curl -s http://127.0.0.1:8006/
curl -s http://127.0.0.1:8006/
podman rm -f store1

podman run -d --name store2 -p 127.0.0.1:8006:8000 localhost/podman-app:dev
curl -s http://127.0.0.1:8006/
podman rm -f store2

Expected: the first container counts visits: 1 and then visits: 2. The second container starts again at visits: 1. The count was in the container's writable layer, and removing the container removed it.

Data written to the writable layer is lost when the container is removed. A database, an upload folder or a queue must not live there.

Choosing. Use a named volume for data that Podman should own, such as a database directory or application state. Use a bind mount for a file or folder you edit on the host, such as configuration or source code during development. Do not use a bind mount just to keep data from a container; a named volume is easier to back up and move.

Named volumes

Hour 4 · 08–18 · 10 min

Create a volume, then mount it into a container. The count now survives the container:

podman volume create appdata
podman run -d --name vol1 -p 127.0.0.1:8006:8000 -v appdata:/data localhost/podman-app:dev
curl -s http://127.0.0.1:8006/
curl -s http://127.0.0.1:8006/
podman rm -f vol1

podman run -d --name vol2 -p 127.0.0.1:8006:8000 -v appdata:/data localhost/podman-app:dev
curl -s http://127.0.0.1:8006/
podman rm -f vol2

Expected: the first container counts 1 and 2. After the second container starts, the next visit prints visits: 3. The data survived the container.

The same mount in the long syntax, which states the type and the names explicitly:

podman run -d --name vol3 -p 127.0.0.1:8007:8000 \
  --mount type=volume,source=appdata,target=/data \
  localhost/podman-app:dev
podman rm -f vol3

Inspect and list volumes:

podman volume ls
podman volume inspect appdata

Expected: ls lists appdata. inspect shows the volume's name, driver and mount point on the host. Do not write to the mount point by hand. Use the container to change the data.

Ownership of a new volume. The sample's Containerfile creates /data and gives it to uid 10001 before the volume exists. A new named volume copies its initial owner and content from that directory. That is why the first container can write to it. Hour 4, module 4 shows what happens when the owner is wrong.

Common errors.

  • volume ... is in use when you remove it: a container still mounts it. Remove the container first.
  • The count did not survive: the container mounted no volume, or it mounted a different volume name. Check podman inspect --format '{{json .Mounts}}' NAME.

Production note. Name volumes after the service and the data they hold, such as orders-db-data. Keep a list of which volume belongs to which service.

Bind mounts and SELinux labels

Hour 4 · 18–28 · 10 min

A bind mount maps a host path into the container. Add :ro to make it read-only inside the container. The lab mounts the sample's own app.py read-only and proves that the container cannot change it:

podman run -d --name bind1 -p 127.0.0.1:8008:8000 \
  -v "$PWD/app.py:/app/app.py:ro" localhost/podman-app:dev
curl -s http://127.0.0.1:8008/health
podman exec bind1 sh -c 'echo "# changed" >> /app/app.py'
podman rm -f bind1

Expected: the health call prints {"status": "ok"}. The echo fails with Read-only file system. The host file is unchanged, and so is the file inside the image.

SELinux labels. On a host where SELinux is enforcing (Fedora and Red Hat family systems), the container can be denied access to a host folder even when the Unix permissions look right. Check the mode first:

getenforce

If the output is Enforcing, add a label option to the mount:

  • :Z relabels the folder for this one container. Use it for a folder that only one container needs.
  • :z relabels the folder so several containers can share it.
mkdir -p data-demo
podman run --rm -v "$PWD/data-demo:/data:Z" docker.io/library/alpine:3.20 ls -ld /data

Expected: the folder is listed inside the container. On a system without SELinux the option is accepted and has no effect.

Do not use :Z or :z on a system folder or on a whole home directory. The relabel changes the label of every file in the folder. Mount a dedicated folder instead.

Common errors.

  • Read-only file system on write: the mount has :ro. That is correct for config and code, and wrong for data.
  • Permission denied on an SELinux host: add :Z to a dedicated folder. Check getenforce and the labels with ls -Z.

Production note. Mount configuration read-only. Mount data as a named volume. Use bind mounts for data only when a tool outside the container must read the same files.

Rootless ownership: keep-id and unshare

Hour 4 · 28–40 · 12 min

A rootless container runs inside a user namespace. Inside it, the container's root is your own user. The sample runs as uid 10001 inside, which maps to a subordinate ID outside, from your range in /etc/subuid. Files owned by your user on the host do not belong to uid 10001 as the container sees it, so the service may be unable to write there.

Look at the mapping:

podman unshare cat /proc/self/uid_map

Expected: lines that show container IDs starting at 0 mapped to your user ID, and a range mapped to the subordinate IDs from your /etc/subuid entry.

Reproduce the problem with a host folder owned by you:

mkdir -p hostdata
podman run -d --name perm1 -p 127.0.0.1:8009:8000 -v "$PWD/hostdata:/data" localhost/podman-app:dev
curl -s http://127.0.0.1:8009/
podman rm -f perm1

Expected: the call returns an error such as Permission denied: '/data/visits.txt'. The service runs, but it cannot write its counter. Fix it in one of two ways.

Fix 1: map your own user to uid 10001. --userns=keep-id maps your host user ID to a chosen ID inside the container. Here your ID maps to 10001, so the process inside owns what you own outside:

podman run -d --name perm2 -p 127.0.0.1:8009:8000 \
  --userns=keep-id:uid=10001,gid=10001 \
  -v "$PWD/hostdata:/data" localhost/podman-app:dev
curl -s http://127.0.0.1:8009/
podman rm -f perm2

Expected: visits: 1.

Fix 2: change the owner inside the user namespace. podman unshare runs a command in your user namespace, so chown sets the owner as the container will see it:

podman unshare chown -R 10001:10001 hostdata
podman run -d --name perm3 -p 127.0.0.1:8009:8000 -v "$PWD/hostdata:/data" localhost/podman-app:dev
curl -s http://127.0.0.1:8009/
podman rm -f perm3

Expected: visits continues from the value already written, because the counter file now exists.

When a bind-mounted folder works for one user and fails for another, check the mapping with podman unshare cat /proc/self/uid_map before you change file modes. Changing modes with chmod 777 hides the problem and widens access for every user on the host.

Common errors.

  • newuidmap errors: the uidmap package is missing, or your user has no /etc/subuid entry. Hour 7 covers this.
  • Ownership still wrong after chown: the folder was written by a different run. Check with ls -ln hostdata, which shows numeric owners.

Production note. Run as a fixed non-root ID, and give that ID ownership of its data directories when you create them. Keep the mapping explained in the runbook, so the next person does not reach for chmod 777.

Backup and restore

Hour 4 · 40–52 · 12 min

A volume is data. Back it up on a schedule, and test the restore before you need it. Podman exports a volume to a tar archive and imports it into another volume:

podman volume export appdata --output appdata-backup.tar
podman volume create appdata-restore
podman volume import appdata-restore appdata-backup.tar
podman run --rm -v appdata-restore:/data localhost/podman-app:dev sh -c 'cat /data/visits.txt'

Expected: export writes the archive. import copies its contents into the existing volume. The last command prints the count stored in appdata, the same number that appdata holds.

Rules for the import. The target volume must exist before you import, and the import merges the archive into what the volume already holds. Restore into an empty volume, then switch the service to it.

Restore drill. Practise this once on a lab machine:

podman volume rm appdata-restore
podman volume create appdata-restore
podman volume import appdata-restore appdata-backup.tar
podman run --rm -v appdata-restore:/data localhost/podman-app:dev sh -c 'cat /data/visits.txt'

Expected: the same count again. A restore you have not tested is not a backup.

An export captures the data, not the container. Keep the image tag, the run command or Quadlet file and the environment variables in version control, so you can recreate the service around the restored volume.

Common errors.

  • no such volume on import: create the target volume first.
  • The restored count is older than expected: you exported before the last visits. Export again from the live volume.

Production note. Take exports while the service is stopped, or use the application's own backup method for a live database. An export of a volume in the middle of a write can capture a partly written file.

Production data rules

Hour 4 · 52–60 · 8 min

Put each piece of data in one of three classes, and follow the rule for its class:

ClassExamplesWhere it livesBackup
Disposablecaches, build output, temp fileswritable layer or --tmpfsnone
Service statecounters, queues, uploadsnamed volumeexport on a schedule, test restores
Configurationsettings, certificates you do not generateread-only bind mount, or a config file in the imageversion control

Check your volumes and find ones nobody uses:

podman volume ls
podman ps -a --filter volume=appdata

Expected: the second command lists containers that mount appdata. A volume with no containers is a candidate for cleanup, but check its owner first.

Clean up the restore volume from this hour:

podman volume rm appdata-restore

Expected: volume rm succeeds because no container uses appdata-restore.

podman volume prune removes every volume that no container uses, including volumes that hold data you meant to keep. podman system prune --volumes does the same across the system. Run the list first, and never run either on a host with unreviewed volumes.

Common errors.

  • volume rm refuses: a container still uses it. Find it with the --filter volume=NAME command above.
  • A volume you need disappeared after prune: restore from your last export. This is why exports come first.

Production note. Record the owner and the backup schedule of every volume. A volume without an owner is a volume nobody restores.

Cheat sheet

Hour 4 ·
TaskCommand
Create a named volumepodman volume create NAME
Mount a named volumepodman run -v NAME:/path IMAGE
Mount a volume, long formpodman run --mount type=volume,source=NAME,target=/path IMAGE
Read-only bind mountpodman run -v "$PWD/dir:/path:ro" IMAGE
SELinux private labelpodman run -v "$PWD/dir:/path:Z" IMAGE
Inspect a volumepodman volume inspect NAME
List volumespodman volume ls
Export a volumepodman volume export NAME --output FILE.tar
Import into a volumepodman volume import TARGET FILE.tar
Show the rootless ID mappodman unshare cat /proc/self/uid_map
Change owner for a container userpodman unshare chown -R 10001:10001 DIR
Map your user to a chosen IDpodman run --userns=keep-id:uid=10001,gid=10001 IMAGE
Remove an unused volumepodman volume rm NAME

Capstone lab: data that survives a rebuild

Hour 4 ·

Goal. Prove that the visit count survives removing the container, a backup and a restore into a fresh volume.

cd podman-app
podman volume create capstone-data
podman run -d --name cap1 -p 127.0.0.1:8011:8000 -v capstone-data:/data localhost/podman-app:dev
curl -s http://127.0.0.1:8011/
curl -s http://127.0.0.1:8011/
podman rm -f cap1
podman volume export capstone-data --output capstone.tar
podman volume create capstone-copy
podman volume import capstone-copy capstone.tar
podman run -d --name cap2 -p 127.0.0.1:8012:8000 -v capstone-copy:/data localhost/podman-app:dev
curl -s http://127.0.0.1:8012/
podman rm -f cap2

Expected. The last curl prints visits: 3, which continues from the two earlier visits, because the restored copy holds the same counter.

Verify. podman volume ls lists both capstone-data and capstone-copy. Neither is used after the containers are removed.

Clean up. podman volume rm capstone-data capstone-copy and rm capstone.tar.

Final quiz

Hour 4

Sources and version notes