25.4. Operating Containerized Services

A detached podman run starts a service that nothing will restart after a reboot, a crash, or an image update. The missing piece is systemd: a unit that owns the container's lifecycle, a restart policy, health monitoring, and a boot-time dependency chain. Podman's Quadlet generator provides the integration this chapter uses: instead of writing a service file that shells out to podman run, you write a declarative .container unit and let the generator produce the service on every daemon-reload.

25.4.1. Systemd Integration with Quadlet

A .container file is a systemd unit file with one extra section. Quadlet - run by systemd's generator machinery - reads it and writes a matching .service unit whose start, stop, and restart behavior all follow systemd's rules. The search paths mirror systemd's own:

Location Scope
/etc/containers/systemd/*.container rootful units, started by the system manager
~/.config/containers/systemd/*.container rootless units, started by the user's manager

The example unit for the web application references the image in the private registry that Section 25.6 publishes; the practice lab deploys the registry first, and if your registry is not up yet, use the locally built tag localhost/handbook-web:1.0 in the Image= line until it is - the rest of the unit is identical. Two prerequisites from earlier sections must exist under the account that will run the service: the secret, recreated with its production value, and the volume. On ctr1, as john, remove the demo secret with podman secret rm db_password, create the real one with printf 'S3cret-Data\n' | podman secret create db_password - (which prints the new secret's ID), and create the volume with podman volume create handbook-data, which prints its name.

The manually launched container from Section 25.2.3 still owns the handbook-web name, so remove it with podman rm -f handbook-web and let the service own that name from now on.

Now create ~/.config/containers/systemd/handbook-web.container with:

[Unit]
Description=Handbook web application

[Container]
Image=registry.ctr1.lab:5000/handbook/web:1.0
ContainerName=handbook-web
PublishPort=192.168.245.10:8080:8080
Environment="APP_MESSAGE=Hello from Quadlet"
Secret=db_password,type=mount,target=db_password
Volume=handbook-data:/data:Z
HealthCmd=wget -q -O /dev/null http://127.0.0.1:8080/
HealthInterval=5s
HealthRetries=3
HealthStartPeriod=5s
PodmanArgs=--memory=128m

[Service]
Restart=on-failure

[Install]
WantedBy=default.target

The [Container] section is the declarative podman run:

Directive Equivalent run option
Image= the image reference (pulled automatically if absent)
ContainerName= --name
PublishPort= -p, with the same host:container syntax
Environment= -e, one quoted assignment per line
Secret= --secret, with type=mount and a target path
Volume= -v, for named volumes and bind mounts alike
HealthCmd= and companions the health probe, replacing an image HEALTHCHECK
PodmanArgs= passthrough for any option Quadlet has no directive for

[Service] and [Install] are ordinary systemd sections, so the restart policy and boot dependency are stated the same way as for any service in Chapter 8. Reload the user manager and confirm the generator produced the unit. On ctr1, as john:

$ systemctl --user daemon-reload
$ systemctl --user list-unit-files | grep handbook-web
handbook-web.service                    generated -

daemon-reload produces no output; the listing shows the unit in the generated state - it exists under the generator's runtime directory, not as a file in ~/.config/systemd/user. This is also why systemctl --user enable handbook-web.service is the wrong tool here: systemd refuses to enable a generated unit, because enablement is recorded by the generator itself from the [Install] section of the .container file. Start the service - systemctl start prints nothing on success - and verify both its systemd state and the container it manages. On ctr1, as john:

$ systemctl --user start handbook-web.service
$ systemctl --user is-active handbook-web.service
active
$ podman inspect --format '{{.State.Health.Status}} {{.HostConfig.Memory}}' handbook-web
healthy 134217728

One start produced a container in healthy state with a cgroup memory limit of 134217728 bytes - the --memory=128m from PodmanArgs, applied through the cgroup integration described in Chapter 9. The service answers on the published address, from the host and from the independent client. On ctr1, as john:

$ curl -sS http://192.168.245.10:8080/
Hello from Quadlet

On ctrclient, as john:

$ curl -sS http://192.168.245.10:8080/
Hello from Quadlet

The response differs from Section 25.2.3 because the unit's Environment= replaced the manual -e. The secret and volume are in place too - podman exec handbook-web cat /run/secrets/db_password prints the secret's value on demand - and from here on, "restart the service" means a systemctl command, never podman rm plus podman run.

25.4.2. User Services, Lingering, and Boot-Time Startup

A user manager - the per-account systemd instance that answers systemctl --user - normally exists only while its owner has a login session. A container service must run without one, which is precisely what lingering changes: with linger enabled, the user manager starts at boot and outlives every login. Enable it for john. On ctr1, entered by john:

$ sudo loginctl enable-linger john

The command produces no output. Verify the state. On ctr1, as john:

$ loginctl show-user john -p Linger
Linger=yes

With linger on and the [Install] section applied by the generator (Section 25.4.1), the chain at boot is: system manager starts john's user manager, the user manager pulls in default.target, and the target wants handbook-web.service. The lab's final reboot in Section 25.9.4 exercises this chain end to end; the registry - a rootful Quadlet unit from Section 25.6.2 - starts through the equivalent system-level chain at the same time.

Working in a user manager has one everyday consequence: environment variables that a login session provides are not present when systemd itself starts the unit. The XDG_RUNTIME_DIR that locates the user's runtime paths is set by the session machinery and is available to the user manager's units; nothing in this chapter's units depends on session-only state beyond it, which is why they start cleanly at boot.

25.4.3. Health Checks, Logs, and Resource Limits

The unit in Section 25.4.1 defines its own health probe, overriding the image's HEALTHCHECK with a faster interval. The probe is a command the runtime executes inside the container on a schedule; its result is the single field to watch:

$ podman inspect --format '{{.State.Health.Status}} {{.HostConfig.Memory}}' handbook-web
healthy 134217728

Values move from starting through healthy, and land in unhealthy when HealthRetries consecutive probes fail. An unhealthy container is a signal, not an action: Quadlet does not remove it automatically, so pair the probe with monitoring (Chapter 28 covers the alerting side) and treat unhealthy as the trigger for the inspection steps in Section 25.8.

Resource limits belong in the same unit. PodmanArgs=--memory=128m caps the container's cgroup, and the cap in force is the number above: 134217728 bytes. CPU limits work the same way (--cpus, --cpu-shares); the cgroup mechanics are those of Chapter 9, applied per container. Limits turn a runaway container into a bounded event - Section 25.8.3 shows the kernel killing a container that exceeds exactly such a limit rather than letting it drag the host down.

Service logs live in the journal, one unit per service, exactly as for native services in Chapter 8. On ctr1, as john:

$ journalctl --user -u handbook-web.service -n 8 --no-pager
Nov 23 12:06:35 ctr1 systemd[670]: Starting handbook-web.service - Handbook web application...
Nov 23 12:06:36 ctr1 podman[5356]: 2026-11-23 12:06:36.012498969 +0000 UTC m=+0.026998439 image pull fdd4551414df382f9fb0ae62ed7fd354ac626dc04d92b17a651e69c5872b8523 registry.ctr1.lab:5000/handbook/web:1.0
Nov 23 12:06:36 ctr1 podman[5356]: 2026-11-23 12:06:36.1350001 +0000 UTC m=+0.149499580 container create de2ed165aca90c637a65fa18730a7356b45212c343aa69d92e191134dcdf6106 (image=registry.ctr1.lab:5000/handbook/web:1.0, name=handbook-web, org.opencontainers.image.title=handbook-web, org.opencontainers.image.version=1.0, io.buildah.version=1.39.3, org.opencontainers.image.description=Small HTTP service for the Debian handbook, PODMAN_SYSTEMD_UNIT=handbook-web.service)
Nov 23 12:06:36 ctr1 podman[5356]: 2026-11-23 12:06:36.639650443 +0000 UTC m=+0.654149952 container init de2ed165aca90c637a65fa18730a7356b45212c343aa69d92e191134dcdf6106 (image=registry.ctr1.lab:5000/handbook/web:1.0, name=handbook-web, org.opencontainers.image.title=handbook-web, org.opencontainers.image.version=1.0, io.buildah.version=1.39.3, org.opencontainers.image.description=Small HTTP service for the Debian handbook, PODMAN_SYSTEMD_UNIT=handbook-web.service)
Nov 23 12:06:36 ctr1 systemd[670]: Started handbook-web.service - Handbook web application.
Nov 23 12:06:36 ctr1 podman[5356]: 2026-11-23 12:06:36.654890291 +0000 UTC m=+0.669389877 container start de2ed165aca90c637a65fa18730a7356b45212c343aa69d92e191134dcdf6106 (image=registry.ctr1.lab:5000/handbook/web:1.0, name=handbook-web, org.opencontainers.image.title=handbook-web, org.opencontainers.image.version=1.0, io.buildah.version=1.39.3, org.opencontainers.image.description=Small HTTP service for the Debian handbook, PODMAN_SYSTEMD_UNIT=handbook-web.service)
Nov 23 12:06:36 ctr1 handbook-web[5356]: de2ed165aca90c637a65fa18730a7356b45212c343aa69d92e191134dcdf6106

Selected output; the unit's earlier stop event is omitted. The Podman events - image pull, container create, init, start - are logged under the unit's name, as is the container ID itself. The PODMAN_SYSTEMD_UNIT label in the create event is the generator's bookkeeping: it ties the container to the unit that owns it, so systemctl --user stop cleans up exactly the container the unit created. For the service's own application output, podman logs handbook-web remains the direct view - empty here, as noted in Section 25.2.3, because this image's server is silent unless it errors.

25.4.4. Updating Images, Rolling Back, and Pruning Unused Data

An image update through Quadlet is deliberately boring: build or pull the new image, change the unit's Image= line, reload, restart. Build version 2.0 from the same Containerfile - only the build argument changes. On ctr1, as john:

$ cd ~/debian-handbook/containers/webapp && podman build --format docker --build-arg IMAGE_VERSION=2.0 -t localhost/handbook-web:2.0 .
STEP 1/11: FROM docker.io/library/alpine:3.20
STEP 2/11: ARG IMAGE_VERSION=1.0
--> Using cache 46147036fd4cb2a372718688f978784ed6343c11f8ce08e615cc63ff119852d0
--> 46147036fd4c
STEP 3/11: LABEL org.opencontainers.image.title="handbook-web"       org.opencontainers.image.version="${IMAGE_VERSION}"       org.opencontainers.image.description="Small HTTP service for the Debian handbook"
--> Using cache 71afb4db3e00f47d13e94f8ccc46b81ffb8f5fd9b1030e8baad1ae20f613198a
--> 71afb4db3e00
STEP 4/11: RUN apk add --no-cache wget busybox-extras     && addgroup -g 10001 -S app     && adduser -u 10001 -S -G app -h /srv app
--> ed157e5a7881
STEP 5/11: COPY index.html /srv/index.html
--> 4125546f2a4e
STEP 6/11: COPY entrypoint.sh /usr/local/bin/entrypoint.sh
--> 1654181d7cda
STEP 7/11: RUN chmod 0755 /usr/local/bin/entrypoint.sh     && chown -R app:app /srv
--> 005b5f672f63
STEP 8/11: USER 10001:10001
--> 8123929fb03a
STEP 9/11: EXPOSE 8080
--> 351a31b2c3bb
STEP 10/11: HEALTHCHECK --interval=10s --timeout=3s --start-period=5s   CMD wget -q -O /dev/null http://127.0.0.1:8080/ || exit 1
--> ea62de4d282a
STEP 11/11: CMD ["/usr/local/bin/entrypoint.sh"]
COMMIT localhost/handbook-web:2.0
--> 43a2ebe759c4
Successfully tagged localhost/handbook-web:2.0
43a2ebe759c410c114b47168764a185bcf23090b7fae117fed8c43ad80f2abdf

Selected output; the alpine package fetch lines under step 4 are identical to the first build and omitted here. Steps 2 and 3 were reported as cache hits from the first build, while every layer from the RUN instruction onward was built fresh, so version 2.0 received its own package and copy layers. The committed image carries the overridden value - the next inspection reads the version label back as 2.0 - which is the check that matters: the build argument is in the image metadata, not only in the build log. The layer numbering carries the general lesson: instructions that rarely change belong early in a Containerfile, and the parts that change on every release belong last.

Publish version 2.0 to the registry (Section 25.6 must be complete for this step) and point the unit at it. On ctr1, as john:

$ podman tag localhost/handbook-web:2.0 registry.ctr1.lab:5000/handbook/web:2.0 && podman push --tls-verify=true registry.ctr1.lab:5000/handbook/web:2.0
Getting image source signatures
Copying blob sha256:62a40ba69ff46b59669f2825976c0bd25f74cc659e59ca96a5732ae40c59a11b
Copying blob sha256:08bc4e534116aa76b16015484b82eac51f9a593416feae9296c8a2d4bb7aa4a2
Copying blob sha256:edbc68c2678a49707547c8f16716592162f93ba132ef4eac43bdd224706e726e
Copying blob sha256:f248d18a764f73da995374346fd0c01437f5c73e3cc7e32bc0eb47f6637e2ba5
Copying blob sha256:08ed09cb6edb58c39172ab17ad2b11b1e873b41fb85f4a302536181263d9f738
Copying config sha256:43a2ebe759c410c114b47168764a185bcf23090b7fae117fed8c43ad80f2abdf
Writing manifest to image destination

The && chain is one logical operation - name the image for the registry, then upload it - with podman tag itself silent. Now edit the unit. On ctr1, as john:

$ sed -i 's|handbook/web:1.0|handbook/web:2.0|' /home/john/.config/containers/systemd/handbook-web.container

The in-place edit is silent; the unit's Image= line now names 2.0. Reload and restart, then confirm which version the running container carries. On ctr1, as john:

$ systemctl --user daemon-reload
$ systemctl --user restart handbook-web.service
$ podman image inspect --format '{{index .Labels "org.opencontainers.image.version"}}' handbook-web:2.0
2.0

The service restarted from the 2.0 reference; the label answers from the image metadata. Rollback is the same operation with the old value - no image archaeology, because version 1.0 is still in the registry. On ctr1, as john:

$ sed -i 's|handbook/web:2.0|handbook/web:1.0|' /home/john/.config/containers/systemd/handbook-web.container
$ systemctl --user daemon-reload && systemctl --user restart handbook-web.service

The reload and restart are silent individually. On ctr1, as john:

$ podman inspect --format '{{.ImageName}} {{.State.Health.Status}}' handbook-web
registry.ctr1.lab:5000/handbook/web:1.0 healthy

The service is back on 1.0 and healthy. Keeping the previous tag in the registry is what makes this a thirty-second rollback; deleting old tags too eagerly converts a routine revert into a rebuild.

Pruning reclaims what updates leave behind. Images accumulate fastest - every build and every pull leaves layers - so prune them first. On ctr1, as john:

$ podman image prune --all --force
bf8527eb54c3680e728d5b4b383a8ba730d72dae7236fbc8dff97ed6b224a731
46147036fd4cb2a372718688f978784ed6343c11f8ce08e615cc63ff119852d0
71afb4db3e00f47d13e94f8ccc46b81ffb8f5fd9b1030e8baad1ae20f613198a
b78e63619c90b09666f415a160b7a409dbe086c30cf28355aa81172d6b6be8ef
812fb7d7a2322841fc6f6d618b7b548f3e498006383cbfbcc352cf038ff0af3e
96e09b4e9d901622d244027ff410a468cd363ec1486f1e888ff1e6db01fc743d
d7b3e5c04dbd0f0711a3d49d3c75a908a010f6b9a6f6290199ffb7137aaceab3
6c72e4eef1b5043f50c7c803472882f3625940b5d1dfb3146dbca15f6711bfdd
978592d5d7f3198a3aa90b8900bdeff2d394840a9290251c9f2c393339da443b
8d85e87cb5125f270b479b3598ef7bf53958cf710462be158bc74daec65b5785
ed157e5a7881d1af425c9be3b0409c9eb005d83dbc956f01bf29c846ffb45411
4125546f2a4eed9353313380020a2f06edc1bd78bc1a404a1de4512632560ca4
1654181d7cdaa8c47a916fc79f080ffddf4c2f8f0cc548db716f403ec22c8ce7
005b5f672f63373dbc17e2b18a0e2b36751c99d19bbd1591cf8591598ffb9769
8123929fb03a8d4ff32ea5159bc692398ecaf6238bb3a3542853eb0f8d26f2e4
351a31b2c3bbf068839c62adf4315534ab219fb4b7bfd00cb59d940941f0f9e7
ea62de4d282a637f7d2e1d4155861c68b535814b5a94b8fab1e4123fbdf2bce2
43a2ebe759c410c114b47168764a185bcf23090b7fae117fed8c43ad80f2abdf

The command lists every removed layer ID - the alpine base, each intermediate build layer, and both versions of the application image. --all also removes images that carry a tag but no container, which is how the 2.0 image disappears from the local store, as the lab in Section 25.9 will notice. Images the running service uses are left alone, which the next command proves immediately. On ctr1, as john:

$ podman system prune --force
Total reclaimed space: 0B
$ podman volume prune --force
appdata

The system prune removed stopped containers and unused networks - nothing was left, hence 0B. The volume prune removed appdata, the demo volume from Section 25.3.2 that no container references - and it would equally remove a named volume you still wanted, because Podman's volume prune deletes every unreferenced volume, named or not. Check podman volume ls before running it, and keep volume data under a bind mount or a backup if it matters. The running service was untouched throughout:

$ curl -sS http://192.168.245.10:8080/
Hello from Quadlet