25.9. Practical Lab: Rebuilding an Isolated Workload
This lab assembles the chapter into one recovery story. The service from Section 25.4 is running under a Quadlet user unit, its image is published in the private registry from Section 25.6, and the registry's data and credentials are backed up per Section 25.6.6. The exercise writes real state into the service, then destroys everything replaceable on the host - the local images and the data volume - and rebuilds the workload from the registry by digest, restoring the data from its independent backup. The client watches from outside the whole time.
25.9.1. Creating an Isolated Guest and a Containerized Service
The starting position is the one the chapter built: ctr1 runs the
rootless handbook-web service (Section 25.4.1) with the secret, volume,
and published port wired through its Quadlet unit; the rootful registry
(Section 25.6.2) holds handbook/web tagged 1.0, whose manifest digest
Section 25.6.4 recorded as
sha256:4c946d6657714df40b58a082ce6a8cdfdda826030a377e191a908c8e05c1ea5e;
and ctrclient can reach both services over the lab network. Verify the
service is live, then write state through it. On ctr1, as john:
$ podman exec handbook-web sh -c 'printf "lab state\n" > /data/state.txt; cat /data/state.txt'
lab state
The service wrote its own volume, handbook-data, mounted at /data
by the unit - one command wrote and read back through the running
container. The state now exists in exactly one place: the volume. The
lab's premise is that this is one place too few.
25.9.2. Publishing an Image to a Private Registry and Deploying by Digest
The image is already published - that was Section 25.6.4's work - so this step's publishing task is to use it as the recovery source. First, take the independent copy of the state. On ctr1, as john:
$ podman run --rm -v handbook-data:/data:ro -v /home/john/debian-handbook/containers/backups:/backup:Z docker.io/library/alpine:3.20 tar -czf /backup/handbook-data.tar.gz -C /data .
The one-shot container read the volume and wrote the archive into the
workspace; the command itself prints nothing, and
backups/handbook-data.tar.gz now holds the state outside the volume.
With the copy secured, stop the workload. On ctr1, as john:
$ systemctl --user stop handbook-web.service
$ podman ps --format '{{.Names}}'
The stop is silent, and the listing is empty: the service's container was its only rootless container. Now simulate the loss - remove every local copy of the application's image and the data volume. On ctr1, as john:
$ podman rmi localhost/handbook-web:1.0 localhost/handbook-web:2.0 registry.ctr1.lab:5000/handbook/web:1.0
Untagged: localhost/handbook-web:1.0
Untagged: registry.ctr1.lab:5000/handbook/web:1.0
The 2.0 tag no longer exists - the image prune in Section 25.4.4
removed it - so podman rmi reports an error for it on standard error
and returns a nonzero status; the two 1.0 references are untagged as
shown. The image data itself stays in the store for now, because the
stopped service container still references it; that is fine - the point
is that no name points at it locally, which is exactly the situation
after a host has lost its images. Remove the volume. On ctr1, as john:
$ podman volume rm handbook-data
handbook-data
Everything replaceable is gone or nameless. The recovery source is the registry, and the pin is the digest. Edit the unit to deploy by digest. On ctr1, as john:
$ sed -i "s|handbook/web:1.0|handbook/web@sha256:4c946d6657714df40b58a082ce6a8cdfdda826030a377e191a908c8e05c1ea5e|" /home/john/.config/containers/systemd/handbook-web.container
The edit is silent. Recreate the volume the unit expects with
podman volume create handbook-data, which prints its name, then reload
and start the service. On ctr1, as john:
$ systemctl --user daemon-reload
$ systemctl --user start handbook-web.service
The reload and the start print nothing: the generator picks up the edited unit at reload, and the start pulls the digest-pinned image from the registry - there is no local copy to use - and runs it. Verify what came up. On ctr1, as john:
$ systemctl --user is-active handbook-web.service
active
$ podman inspect --format '{{.ImageName}} {{.State.Health.Status}}' handbook-web
registry.ctr1.lab:5000/handbook/web@sha256:4c946d6657714df40b58a082ce6a8cdfdda826030a377e191a908c8e05c1ea5e healthy
The replacement workload is running from the exact manifest the lab
recorded - ImageName shows the digest, not a tag - and it is healthy.
The rebuild used nothing from the old host state except the unit file,
the secret, and the registry.
25.9.3. Preserving Configuration, Secrets, Registry Images, and Application Data
Take stock of what the recovery so far depended on, because that inventory is the backup policy:
| Artifact | Where it lives | How it was preserved |
|---|---|---|
| Application image | registry handbook/web, digest-pinned |
pushed (Section 25.6.4); registry data in the host backup (Section 25.6.6) |
| Service definition | ~/.config/containers/systemd/handbook-web.container |
a workspace file; version it with the site's configuration management (Chapter 27) |
| Build recipe | ~/debian-handbook/containers/webapp/ |
plain files in the workspace |
| Application data | handbook-data volume |
backups/handbook-data.tar.gz (this lab) |
| Secret | Podman's secret store | recreatable from its source with podman secret create (Section 25.4.1) |
| Registry trust and credentials | CA certificate, htpasswd file |
inside backups/registry-full.tar.gz (Section 25.6.6), protected like the live material |
Two of these rows carry the lab's warnings from earlier sections. The secret row: the rootless secret store is runtime state; what a backup must protect is the source of the secret, or a dedicated secret manager, not the store. The image row: the registry itself has a backup (Section 25.6.6 exercised losing its data and restoring it), so the application's image does not need a second archive - the registry's own backup covers it, and the digest proves any restored copy is the same bytes. Move both archives off the host on the same schedule as the rest of the site's backups (Chapter 15); archives that live only on the host they protect are copies, not backups.
25.9.4. Replacing the Workload and Restoring Client-Visible Service
The rebuilt container is running, but its volume is empty - the state must come back from the archive. On ctr1, as john:
$ podman run --rm -v handbook-data:/data:Z -v /home/john/debian-handbook/containers/backups:/backup:ro,Z docker.io/library/alpine:3.20 tar -xzf /backup/handbook-data.tar.gz -C /data
The extraction is silent. Read the state back through the running service. On ctr1, as john:
$ podman exec handbook-web cat /data/state.txt
lab state
The state written in Section 25.9.1 is visible again in a container that did not exist five minutes ago, running an image that had no local name three minutes ago. The client's view, from outside. On ctrclient, as john:
$ curl -sS http://192.168.245.10:8080/
Hello from Quadlet
The client never noticed the rebuild. Finally, prove the whole chain survives the ultimate restart: reboot the host. On ctr1, as john:
$ sudo systemctl reboot
After the host returns, check each layer. The lingering user service. On ctr1, as john:
$ systemctl --user is-active handbook-web.service
active
The rootful registry. On ctr1, as root:
# systemctl is-active registry.service
active
The client-visible service, and a fresh digest pull from the registry, on ctrclient as john:
$ curl -sS http://192.168.245.10:8080/
Hello from Quadlet
$ podman pull --tls-verify=true registry.ctr1.lab:5000/handbook/web@sha256:4c946d6657714df40b58a082ce6a8cdfdda826030a377e191a908c8e05c1ea5e
Trying to pull registry.ctr1.lab:5000/handbook/web@sha256:4c946d6657714df40b58a082ce6a8cdfdda826030a377e191a908c8e05c1ea5e...
Getting image source signatures
Copying blob sha256:e8fd4134be5fd28ad73e449af0eadab39eb0196c86c70c688198a54c0625ff41
Copying blob sha256:c265cb3fa7c80ac67446fc0db739a202eace2b21df78d52382314a7500106fe5
Copying blob sha256:de2651157514a5b93fe82c52ae7d5d46c4af31bf8a567bd08c58c05ccac84684
Copying blob sha256:fc54165448d97c3cabb093f937a40d5ae0ee388acc90ee463f471de4e307ce37
Copying blob sha256:d5b3ea0df3ea07741683cd79c30264bbd3c899d4d269117fcfd8076731744ce5
Copying config sha256:fdd4551414df382f9fb0ae62ed7fd354ac626dc04d92b17a651e69c5872b8523
Writing manifest to image destination
fdd4551414df382f9fb0ae62ed7fd354ac626dc04d92b17a651e69c5872b8523
Boot brought back the user service (linger plus the generator-applied
[Install]), the rootful registry, the published port, and the
registry's stored images - no manual step anywhere in the chain. That is
the end state this chapter has been building toward: a workload whose
image comes from a registry by digest, whose state lives outside it, whose
lifecycle belongs to systemd, and whose loss is an inconvenience rather
than a disaster.
Removing the practice material is bounded. On ctr1, as john:
systemctl --user stop handbook-web.service and
systemctl --user stop podman.socket, then
rm ~/.config/containers/systemd/handbook-web.container,
podman rm -f handbook-web, podman secret rm db_password, and
podman volume rm handbook-data. On ctr1, as root:
systemctl stop registry.service,
rm /etc/containers/systemd/registry.container,
rm -rf /srv/registry /etc/containers/certs.d/registry.ctr1.lab:5000,
and systemctl daemon-reload; for the machine of Section 25.7,
machinectl stop handbookctr followed by
rm -rf /var/lib/machines/handbookctr /srv/machine-data /etc/systemd/nspawn/handbookctr.nspawn /usr/local/sbin/handbookctr-network /etc/systemd/system/systemd-nspawn@handbookctr.service.d.
The Compose project is already down. On both hosts, podman image prune
--all --force empties the stores, and on ctrclient,
rm -rf ~/.config/containers/certs.d/registry.ctr1.lab:5000 removes the
lab trust. The workspace archives under
~/debian-handbook/containers/backups are ordinary files; keep or delete
them with the workspace itself.