第 12 章 Porting containers to systemd using Podman
Podman (Pod Manager) is a simple daemonless tool fully featured container engine. Podman provides a Docker-CLI comparable command line that makes the transition from other container engines easier and enables the management of pods, containers, and images.
Originally, Podman was not designed to provide an entire Linux system or manage services, such as start-up order, dependency checking, and failed service recovery. systemd was responsible for a complete system initialization. Due to Red Hat integrating containers with systemd, you can manage OCI and Docker-formatted containers built by Podman in the same way as other services and features are managed in a Linux system. You can use the systemd initialization service to work with pods and containers.
With systemd unit files, you can:
-
Set up a container or pod to start as a
systemdservice. - Define the order in which the containerized service runs and check for dependencies (for example making sure another service is running, a file is available or a resource is mounted).
-
Control the state of the
systemdsystem by using thesystemctlcommand.
You can generate portable descriptions of containers and pods by using systemd unit files.
12.1. Auto-generating a systemd unit file using Quadlets 复制链接链接已复制到粘贴板!
With Quadlet, you describe how to run a container in a format that is very similar to regular systemd unit files. The container descriptions focus on the relevant container details and hide technical details of running containers under systemd. Create the <CTRNAME>.container unit file in one of the following directories:
-
For root users:
/usr/share/containers/systemd/or/etc/containers/systemd/ -
For rootless users:
$HOME/.config/containers/systemd/,$XDG_CONFIG_HOME/containers/systemd/,/etc/containers/systemd/users/$(UID), or/etc/containers/systemd/users/
Quadlet is available beginning with Podman v4.6.
Prerequisites
-
The
container-toolsmeta-package is installed.
Procedure
Create the
mysleep.containerunit file:$ cat $HOME/.config/containers/systemd/mysleep.container [Unit] Description=The sleep container After=local-fs.target [Container] Image=registry.access.redhat.com/ubi10-minimal:latest Exec=sleep 1000 [Install] # Start by default on boot WantedBy=multi-user.target default.targetIn the
[Container]section you must specify:-
Image- container mage you want to tun Exec- the command you want to run inside the containerThis enables you to use all other fields specified in a
systemdunit file.
-
Create the
mysleep.servicebased on themysleep.containerfile:$ systemctl --user daemon-reloadOptional: Check the status of the
mysleep.service:$ systemctl --user status mysleep.service ○ mysleep.service - The sleep container Loaded: loaded (/home/username/.config/containers/systemd/mysleep.container; generated) Active: inactive (dead)Start the
mysleep.service:$ systemctl --user start mysleep.service
Verification
Check the status of the
mysleep.service:$ systemctl --user status mysleep.service ● mysleep.service - The sleep container Loaded: loaded (/home/username/.config/containers/systemd/mysleep.container; generated) Active: active (running) since Thu 2023-02-09 18:07:23 EST; 2s ago Main PID: 265651 (conmon) Tasks: 3 (limit: 76815) Memory: 1.6M CPU: 94ms CGroup: ...List all containers:
$ podman ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 421c8293fc1b registry.access.redhat.com/ubi10-minimal:latest sleep 1000 30 seconds ago Up 10 seconds ago systemd-mysleepNote that the name of the created container consists of the following elements:
-
a
systemd-prefix a name of the
systemdunit, that issystemd-mysleepThis naming helps to distinguish common containers from containers running in
systemdunits. It also helps to determine which unit a container runs in. If you want to change the name of the container, use theContainerNamefield in the[Container]section.For more information, see the
podman-systemd.unit(5)man page on your system.
-
a