25.3. Networking and Persistent Data
A running container has two relationships with the world outside itself: the network path clients use to reach it, and the files it must treat as state rather than content. Both are configured at launch time with flags, and both outlive any individual container when configured deliberately. This section covers the three mechanisms - published ports, container networks, and mounts - and the one design rule that matters most: the image is disposable, the data is not.
25.3.1. Published Ports, Container Networks, and Name Resolution
Publishing a port forwards a host address to a container port; it is how a
rootless container becomes reachable from other hosts without any
privilege. The mapping -p 192.168.245.10:8080:8080 in Section 25.2.3
pinned the host address deliberately, and pasta - the user-space forwarder
rootless Podman uses - simply listens there as john. Rootful publishing
instead creates real firewall rules; the difference becomes a diagnostic
topic in Section 25.8.2.
Containers that only need to talk to each other should not publish at all. A named container network gives its members a private bridge with address assignment and, crucially, name resolution between members. The subcommands:
| Subcommand | Effect |
|---|---|
podman network create NAME |
create a bridge network with an address pool and DNS |
podman network ls |
list networks |
podman network inspect NAME |
show the network's subnet, gateway, and DNS state |
podman network rm NAME |
remove an unused network |
Create the network, then attach one long-running container to it and a second container that looks the first up by name. On ctr1, as john:
$ podman network create handbook-net
handbook-net
The command prints the network's name. It also selects a subnet from netavark's default pool - the addresses in the next steps come from that pool. Start a target container on the network. On ctr1, as john:
$ podman run -d --name netapp --network handbook-net docker.io/library/alpine:3.20 sleep 300
cd821553032bf1740097cc6c0f84553c65643805c86de7d1653807acecdff6d7
The command prints the container ID and produces no other output. Now resolve the target by name from a second container on the same network. On ctr1, as john:
$ podman run --rm --network handbook-net docker.io/library/alpine:3.20 sh -c 'getent hosts netapp; ping -c1 -W2 netapp'
10.89.0.2 netapp.dns.podman netapp.dns.podman netapp
PING netapp (10.89.0.2): 56 data bytes
64 bytes from 10.89.0.2: seq=0 ttl=42 time=0.114 ms
--- netapp ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.114/0.114/0.114 ms
getent hosts netapp answered from aardvark, the DNS service netavark
runs alongside each network: the short name resolves, and the fully
qualified netapp.dns.podman form identifies the answer's origin. The
ping then confirms actual connectivity, not just a DNS entry. Two
containers that must cooperate should always be given a shared network and
names, never a pair of published ports on the host. Cleaning up is
symmetric. On ctr1, as john:
$ podman rm -f netapp
time="2026-11-23T13:09:51Z" level=warning msg="StopSignal SIGTERM failed to stop container netapp in 10 seconds, resorting to SIGKILL"
netapp
The busybox sleep process did not react to the default stop signal within
ten seconds, so the runtime escalated to SIGKILL; the warning is the
runtime reporting its own escalation, and the name printed afterwards is the
completed removal. On ctr1, as john:
$ podman network rm handbook-net
handbook-net
25.3.2. Bind Mounts, Named Volumes, and Ownership Mapping
A bind mount exposes an existing host directory inside the container; a
named volume is storage the runtime creates and manages for you. Bind
mounts fit data an administrator or another service already owns;
volumes fit state that belongs to the container but must outlive it. The
mount specification after -v takes modifiers:
| Modifier | Effect |
|---|---|
ro |
read-only inside the container |
Z |
relabel the source for exclusive container use (a SELinux operation; accepted and ignored on Debian's AppArmor hosts, kept here so the same command works unchanged on SELinux systems) |
rw |
read-write (the default) |
The application image from Section 25.2.4 serves whatever /srv holds,
which makes it a good subject for a read-only bind mount. Create a page in
the workspace and serve it. On ctr1, as john:
$ mkdir -p ~/debian-handbook/containers/site
$ printf 'Bind-mounted site page\n' > ~/debian-handbook/containers/site/index.html
Both commands write to the filesystem and produce no terminal output; the
redirection in the second creates site/index.html. Run a second instance
of the service against that directory. On ctr1, as john:
$ podman run -d --name bind-web -p 192.168.245.10:8083:8080 -v /home/john/debian-handbook/containers/site:/srv:ro,Z localhost/handbook-web:1.0
7aef0d354ed07875a856ad55d11d52fb6871ef9555ab9793cca0d7d90da3f057
The entrypoint found no APP_MESSAGE, so it left the mounted page alone;
ro guarantees that even a compromised container could not rewrite it.
On ctr1, as john:
$ curl -sS http://192.168.245.10:8083/
Bind-mounted site page
The container served the host directory's content, unchanged by the image. Remove the demo container. On ctr1, as john:
$ podman rm -f bind-web
time="2026-11-23T13:10:11Z" level=warning msg="StopSignal SIGTERM failed to stop container bind-web in 10 seconds, resorting to SIGKILL"
bind-web
The same ten-second escalation as in Section 25.3.1, naming this
container; the name line afterwards is the completed removal. Named
volumes need no preparation. Create one -
podman volume create appdata, which prints the volume's name - then
write to it from a throwaway container, and read the file back from a
second container. On ctr1, as john:
$ podman run --rm -v appdata:/data:Z docker.io/library/alpine:3.20 sh -c 'echo first-run > /data/state.txt'
The --rm container exits immediately after the write; the command itself
prints nothing. Read the file back. On ctr1, as john:
$ podman run --rm -v appdata:/data:Z docker.io/library/alpine:3.20 cat /data/state.txt
first-run
The data survived the container that wrote it, which is the entire point: replacing a container is routine, and volumes decouple that routine from the data. Ownership is the rootless subtlety. Under the default user namespace, the container's root is john on the host, so files a container writes land in john's ownership. On ctr1, as john:
$ mkdir -p ~/debian-handbook/containers/bind-data
$ podman run --rm -v /home/john/debian-handbook/containers/bind-data:/data:Z docker.io/library/alpine:3.20 sh -c 'echo mapped > /data/mapped.txt'
The write completes without output. Check what the host sees. On ctr1, as john:
$ stat -c '%u:%g %n' /home/john/debian-handbook/containers/bind-data/mapped.txt
1001:1001 /home/john/debian-handbook/containers/bind-data/mapped.txt
The file is owned by john's UID and GID - the container's "root" wrote it
as 1001, exactly as the UID map in Section 25.1.1 promised. That default is
usually what you want: the user can read, back up, and clean up the data
with ordinary tools. When a container must run as a specific UID and
write to a host directory, --userns=keep-id maps the invoking ID into the
container unchanged instead. On ctr1, as john:
$ podman run --rm --userns=keep-id -v /home/john/debian-handbook/containers/bind-data:/data:Z docker.io/library/alpine:3.20 sh -c 'id -u; echo keepid > /data/keepid.txt'
1001
The command prints the container-side UID - 1001, john's own - and writes a second file; the write itself is silent. On ctr1, as john:
$ stat -c '%u:%g %n' /home/john/debian-handbook/containers/bind-data/keepid.txt
1001:1001 /home/john/debian-handbook/containers/bind-data/keepid.txt
Both approaches produce host-john ownership here; they differ when the
container's process needs to believe it is UID 1001 rather than root -
for software that refuses to run as root, keep-id is the tool. The
permission problems that arise when these mappings are overlooked are the
first troubleshooting topic of Section 25.8.1.
25.3.3. Secrets, Configuration Injection, and Read-Only Filesystems
Environment variables are the simplest way to configure a container and
the worst way to hand it a credential: -e values appear in podman
inspect, in process listings inside the container, and in any tool that
captures the environment. Podman's secret store keeps credentials out of
both the image and the command line. The subcommands:
| Subcommand | Effect |
|---|---|
podman secret create NAME - |
read the secret value from standard input |
podman secret ls |
list stored secrets |
podman secret rm NAME |
delete a secret |
Create a secret the way the application will eventually use it. On ctr1, as john:
$ printf 'secret-value\n' | podman secret create db_password -
190afdcfdb063a6e03bdd4c9e
The pipeline sends the value on standard input; the command prints the new secret's ID, which the store reports when listed. On ctr1, as john:
$ podman secret ls
ID NAME DRIVER CREATED UPDATED
190afdcfdb063a6e03bdd4c9e db_password file 2 seconds ago 2 seconds ago
The listing shows the name and the file driver - the value lives under
john's container storage, not in any image. Mount it into a container and
read it from the path the runtime chose. On ctr1, as john:
$ podman run -d --name secret-test --secret db_password -e APP_MESSAGE='Secret mount test' localhost/handbook-web:1.0
1d87f401a9db2a50559c9a3a9d09dba199ffc96029130a080deb373b8c39367d
The run prints the container ID. On ctr1, as john:
$ podman exec secret-test cat /run/secrets/db_password
secret-value
--secret db_password mounted the secret at /run/secrets/db_password, a
tmpfs path readable only by the container process; the value never appeared
in the run command, the image, or the environment. Remove the demo
container; the secret stays in the store. On ctr1, as john:
$ podman rm -f secret-test
time="2026-11-23T13:10:29Z" level=warning msg="StopSignal SIGTERM failed to stop container secret-test in 10 seconds, resorting to SIGKILL"
secret-test
Configuration that is not secret - a config file, a page - has two good
channels: a bind mount for data an administrator edits
(Section 25.3.2), or baked image content when the file changes only with
the software. The final containment tool is --read-only, which makes the
container's whole root filesystem read-only so an intruder or a bug cannot
modify the image in place. Writable paths must then be supplied
explicitly. On ctr1, as john:
$ podman run -d --name readonly-test --read-only --tmpfs /tmp --tmpfs /srv -e APP_MESSAGE='Read-only root' -p 192.168.245.10:8084:8080 localhost/handbook-web:1.0
1ca884d09de81df868e8676d93dab013afa4e95204914f2c916c162529947d58
The run prints the container ID. Ask the container to write to a path its root filesystem owns. On ctr1, as john:
$ podman exec readonly-test touch /etc/forbidden
touch: /etc/forbidden: Read-only file system
The kernel refused the write. --tmpfs /srv kept the service itself
working - its entrypoint writes the served page there - while the image's
/etc stayed immutable. Remove the demo container with
podman rm -f readonly-test when you are done; as with the earlier demo
containers, the runtime escalates to SIGKILL to stop this one and says
so on standard error. Combined with --cap-drop=ALL and a non-root
USER, a read-only root filesystem is the containment baseline for any
container whose image you trust more than its input.
25.3.4. Backing Up Data Independently of Container Images
An image is a build product. Rebuilding it from its Containerfile is cheap; regenerating a volume of customer data is not. Keeping the two separate is the design rule behind every example in this chapter: images come from a registry (Section 25.6) or a rebuild, while data comes from a backup that was taken without reference to any container.
Figure 25.3. What a running container is made of, and which copy of each piece matters during recovery. The image (left) is rebuilt or re-pulled; runtime mounts (center) carry the state; the independent copies (right) are what a restore actually uses.
Concretely, a volume backup does not need the volume's container running,
or any Podman API at all: a one-shot container with the volume and a
backup directory mounted can archive the data with plain tar. Create
the destination directory in the workspace. On ctr1, as john:
$ mkdir -p ~/debian-handbook/containers/backups
The command prints nothing. Archive the demo volume from Section 25.3.2. On ctr1, as john:
$ podman run --rm -v appdata:/data:ro -v /home/john/debian-handbook/containers/backups:/backup:Z docker.io/library/alpine:3.20 tar -czf /backup/appdata.tar.gz -C /data .
The archive lands in backups/appdata.tar.gz; the container itself
produces no output. To prove the archive is a real backup, destroy the
original: run podman volume rm appdata followed by
podman volume create appdata - the volume is now new and empty.
Extract the archive into it. On ctr1, as john:
$ podman run --rm -v appdata:/data:Z -v /home/john/debian-handbook/containers/backups:/backup:ro,Z docker.io/library/alpine:3.20 tar -xzf /backup/appdata.tar.gz -C /data
The extraction is silent. Read the data back. On ctr1, as john:
$ podman run --rm -v appdata:/data:Z docker.io/library/alpine:3.20 cat /data/state.txt
first-run
The value written before the volume was destroyed is back - the backup,
not the volume, carried it. The same pattern (mount volume read-only,
mount a destination directory, run tar) backs up any named volume. Two
variations matter in practice:
- Back up bind-mounted directories directly with the file-level tools of Chapter 15; they are ordinary host paths.
- A running database is not a consistent backup source. Quiesce it first - stop its container or use the application's own dump tool - then archive the volume, as discussed for service data in Chapter 15 and applied to the registry itself in Section 25.6.6.
Secrets deserve one more note: the secret store is runtime state, but
the secret's source - the password manager, the deployment file, the
operator's record - is what a backup should protect. Recreating a secret is
a one-line podman secret create once the value is available; a backup
that cannot supply the value restores nothing. The practical lab in
Section 25.9 assembles all of these pieces - image from the registry by
digest, volume from its archive, secret from its source, unit file from
the workspace - into a full recovery.