12.5. Generating a systemd unit file using Podman
Podman allows systemd to control and manage container processes. You can generate a systemd unit file for the existing containers and pods by using podman generate systemd command. It is recommended to use podman generate systemd because the generated units files change frequently (via updates to Podman) and the podman generate systemd ensures that you get the latest version of unit files.
Starting with Podman v4.6, you can use the Quadlets that describe how to run a container in a format similar to regular systemd unit files and hides the complexity of running containers under systemd.
Prerequisites
-
The
container-toolsmeta-package is installed.
Procedure
Create a container (for example
myubi):$ podman create --name myubi registry.access.redhat.com/ubi10:latest sleep infinity 0280afe98bb75a5c5e713b28de4b7c5cb49f156f1cce4a208f13fee2f75cb453Use the container name or ID to generate the
systemdunit file and direct it into the~/.config/systemd/user/container-myubi.servicefile:$ podman generate systemd --name myubi > ~/.config/systemd/user/container-myubi.service
Verification
Display the content of generated
systemdunit file:$ cat ~/.config/systemd/user/container-myubi.service # container-myubi.service # autogenerated by Podman 3.3.1 # Wed Sep 8 20:34:46 CEST 2021 [Unit] Description=Podman container-myubi.service Documentation=man:podman-generate-systemd(1) Wants=network-online.target After=network-online.target RequiresMountsFor=/run/user/1000/containers [Service] Environment=PODMAN_SYSTEMD_UNIT=%n Restart=on-failure TimeoutStopSec=70 ExecStart=/usr/bin/podman start myubi ExecStop=/usr/bin/podman stop -t 10 myubi ExecStopPost=/usr/bin/podman stop -t 10 myubi PIDFile=/run/user/1000/containers/overlay-containers/9683103f58a32192c84801f0be93446cb33c1ee7d9cdda225b78049d7c5deea4/userdata/conmon.pid Type=forking [Install] WantedBy=multi-user.target default.target-
The
Restart=on-failureline sets the restart policy and instructssystemdto restart when the service cannot be started or stopped cleanly, or when the process exits non-zero. -
The
ExecStartline describes how we start the container. The
ExecStopline describes how we stop and remove the container.For more information, see
podman-generate-systemd(1)man page on your system.
-
The