Storage: volumes and bind mounts
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
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.
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
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 usewhen 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
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:
:Zrelabels the folder for this one container. Use it for a folder that only one container needs.:zrelabels 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.
: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 systemon write: the mount has:ro. That is correct for config and code, and wrong for data.Permission deniedon an SELinux host: add:Zto a dedicated folder. Checkgetenforceand the labels withls -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
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.
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.
newuidmaperrors: theuidmappackage is missing, or your user has no/etc/subuidentry. Hour 7 covers this.- Ownership still wrong after
chown: the folder was written by a different run. Check withls -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
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.
Common errors.
no such volumeon 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
Put each piece of data in one of three classes, and follow the rule for its class:
| Class | Examples | Where it lives | Backup |
|---|---|---|---|
| Disposable | caches, build output, temp files | writable layer or --tmpfs | none |
| Service state | counters, queues, uploads | named volume | export on a schedule, test restores |
| Configuration | settings, certificates you do not generate | read-only bind mount, or a config file in the image | version 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 rmrefuses: a container still uses it. Find it with the--filter volume=NAMEcommand 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
| Task | Command |
|---|---|
| Create a named volume | podman volume create NAME |
| Mount a named volume | podman run -v NAME:/path IMAGE |
| Mount a volume, long form | podman run --mount type=volume,source=NAME,target=/path IMAGE |
| Read-only bind mount | podman run -v "$PWD/dir:/path:ro" IMAGE |
| SELinux private label | podman run -v "$PWD/dir:/path:Z" IMAGE |
| Inspect a volume | podman volume inspect NAME |
| List volumes | podman volume ls |
| Export a volume | podman volume export NAME --output FILE.tar |
| Import into a volume | podman volume import TARGET FILE.tar |
| Show the rootless ID map | podman unshare cat /proc/self/uid_map |
| Change owner for a container user | podman unshare chown -R 10001:10001 DIR |
| Map your user to a chosen ID | podman run --userns=keep-id:uid=10001,gid=10001 IMAGE |
| Remove an unused volume | podman volume rm NAME |
Capstone lab: data that survives a rebuild
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.