Chapter 22. Creating and restoring container checkpoints Checkpoint/Restore In Userspace (CRIU) is a software that enables you to set a checkpoint on a running container or an individual application and store its state to disk. You can use data saved to restore the container after a reboot at the same point in time it was checkpointed. Warning The kernel does not support pre-copy checkpointing on AArch64. 22.1. Creating and restoring a container checkpoint locally This example is based on a Python based web server which returns a single integer which is incremented after each request. Prerequisites The container-tools meta-package is installed. Procedure Create a Python based server: Create a container with the following definition: The container is based on the Universal Base Image (UBI 8) and uses a Python based server. Build the container: Files counter.py and Containerfile are the input for the container build process ( podman build ). The built image is stored locally and tagged with the tag counter . Start the container as root: To list all running containers, enter: Display IP address of the container: Send requests to the container: Create a checkpoint for the container: Reboot the system. Restore the container: Send requests to the container: The result now does not start at 0 again, but continues at the value. This way you can easily save the complete container state through a reboot. Additional resources Podman checkpoint 22.2. Reducing startup time using container restore You can use container migration to reduce startup time of containers which require a certain time to initialize. Using a checkpoint, you can restore the container multiple times on the same host or on different hosts. This example is based on the container from the Creating and restoring a container checkpoint locally . Prerequisites The container-tools meta-package is installed. Procedure Create a checkpoint of the container, and export the checkpoint image to a tar.gz file: Restore the container from the tar.gz file: The --name ( -n ) option specifies a new name for containers restored from the exported checkpoint. Display ID and name of each container: Display IP address of each container: Send requests to each container: Note, that the result is 4 in all cases, because you are working with different containers restored from the same checkpoint. Using this approach, you can quickly start up stateful replicas of the initially checkpointed container. Additional resources Container migration with Podman on RHEL 22.3. Migrating containers among systems You can migrate the running containers from one system to another, without losing the state of the applications running in the container. This example is based on the container from the Creating and restoring a container checkpoint locally section tagged with counter . Important Migrating containers among systems with the podman container checkpoint and podman container restore commands is supported only when the configurations of the systems match completely, as shown below: Podman version OCI runtime (runc/crun) Network stack (CNI/Netavark) Cgroups version kernel version CPU features You can migrate to a CPU with more features, but not to a CPU which does not have a certain feature that you are using. The low-level tool doing the checkpointing (CRIU) has the possibility to check for CPU feature compatibility: https://criu.org/Cpuinfo . Prerequisites The container-tools meta-package is installed. The following steps are not necessary if the container is pushed to a registry as Podman will automatically download the container from a registry if it is not available locally. This example does not use a registry, you have to export previously built and tagged container (see Creating and restoring a container checkpoint locally ). Export previously built container: Copy exported container image to the destination system ( other_host ): Import exported container on the destination system: Now the destination system of this container migration has the same container image stored in its local container storage. Procedure Start the container as root: Display IP address of the container: Send requests to the container: Create a checkpoint of the container, and export the checkpoint image to a tar.gz file: Copy the checkpoint archive to the destination host: Restore the checkpoint on the destination host ( other_host ): Send a request to the container on the destination host ( other_host ): As a result, the stateful container has been migrated from one system to another without losing its state. Additional resources Container migration with Podman on RHEL

Commands:
cat counter.py #!/usr/bin/python3 import http.server counter = 0 class handler(http.server.BaseHTTPRequestHandler): def do_GET(s): global counter s.send_response(200) s.send_header('Content-type', 'text/html') s.end_headers() s.wfile.write(b'%d\n' % counter) counter += 1 server = http.server.HTTPServer(('', 8088), handler) server.serve_forever()
cat Containerfile FROM registry.access.redhat.com/ubi9/ubi COPY counter.py /home/counter.py RUN useradd -ms /bin/bash counter RUN dnf -y install python3 && chmod 755 /home/counter.py USER counter ENTRYPOINT /home/counter.py
podman build . --tag counter
podman run --name criu-test --detach counter
podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e4f82fd84d48 localhost/counter:latest 5 seconds ago Up 4 seconds ago criu-test
podman inspect criu-test --format "{{.NetworkSettings.IPAddress}}" 10.88.0.247
curl 10.88.0.247:8088 0 curl 10.88.0.247:8088 1
podman container checkpoint criu-test
podman container restore --keep criu-test
curl 10.88.0.247:8080 2 curl 10.88.0.247:8080 3 curl 10.88.0.247:8080 4
podman container checkpoint criu-test --export /tmp/chkpt.tar.gz
podman container restore --import /tmp/chkpt.tar.gz --name counter1 podman container restore --import /tmp/chkpt.tar.gz --name counter2 podman container restore --import /tmp/chkpt.tar.gz --name counter3
podman ps -a --format "{{.ID}} {{.Names}}" a8b2e50d463c counter3 faabc5c27362 counter2 2ce648af11e5 counter1
#\ufe0f podman inspect counter1 --format "{{.NetworkSettings.IPAddress}}" 10.88.0.248 #\ufe0f podman inspect counter2 --format "{{.NetworkSettings.IPAddress}}" 10.88.0.249 #\ufe0f podman inspect counter3 --format "{{.NetworkSettings.IPAddress}}" 10.88.0.250
#\ufe0f curl 10.88.0.248:8080 4 #\ufe0f curl 10.88.0.249:8080 4 #\ufe0f curl 10.88.0.250:8080 4
podman save --output counter.tar counter
scp counter.tar other_host :
ssh other_host podman load --input counter.tar
podman run --name criu-test --detach counter
podman inspect criu-test --format "{{.NetworkSettings.IPAddress}}" 10.88.0.247
curl 10.88.0.247:8080 0 curl 10.88.0.247:8080 1
podman container checkpoint criu-test --export /tmp/chkpt.tar.gz
scp /tmp/chkpt.tar.gz other_host :/tmp/
podman container restore --import /tmp/chkpt.tar.gz
*curl 10.88.0.247:8080* 2