Podman & Systemd
Systemd can be used to start and stop containers at boot and shutdown, respectively. This creates a scenario where no intervention is needed on a system reboot.
Before proceeding, be sure to enable linger for whichever user will be running the containers.
- Enable
lingerfor the user that will run the container.
loginctl enable-linger $USER
- Create
~/.config/systemd/userto keep the user systemd unit files.
mkdir -p ~/.config/systemd/user
Creating systemd unit files for podman containers
-
Run the container with all the mounts, ports, and env variables needed.
-
Generate systemd unit file using
podman generatecommand.
podman generate systemd --new --files --name $container_name
-
Stop and remove the container.
-
Move the service unit file to
~/.config/systemd/user.
mv -Z container-$container_name.service ~/.config/systemd/user/
- Reload the systemd daemon.
systemctl --user reload-daemon
- Enable and start the container using
systemctl.
systemctl --user enable --now container-$container_name.service
Creating systemd unit files for podman pods
Creating a systemd unit file for a pod is the same as with a container. The only difference is that a systemd unit file will be created for each container that is a part of the pod.
-
Run the pod and containers with all the mounts, ports, and env variables needed.
-
Generate service file using
podman generatecommand.
podman generate systemd --new --files --name $pod_name
-
Stop and remove the pod and containers.
-
Move all the unit files to
~/.config/systemd/user/
mv -Z pod-$pod_name.service container-* ~/.config/
- Reload the systemd daemon.
systemctl --user reload-daemon
- Enable and start the pod using
systemctl. This will start all dependent containers as well.
systemctl --user enable --now pod-$pod_name.service