How to Run Containers as Systemd Service with Podman

As we know podman is an open-source daemon-less tool which provides environment to build, run and manage containers. Running containers as systemd service means that containers will automatically start when the system gets rebooted.

In this post, we will learn how to run containers as systemd service with podman on RHEL based distributions like RHEL 8, CentOS 8 and Rocky Linux 8.

Prerequisites :

  • Minimal RHEL based OS Installation.
  • Stable Internet Connection
  • Sudo User with root privileges

In this demonstration, I am using minimal RHEL 8.5, but these steps are also applicable for CentOS 8 and Rocky Linux 8.  Let’s jump into the steps,

Step 1) Install Podman

To install podman on RHEL 8 , run

$ sudo dnf install @container-tools -y

For CentOS 8 / Rocky Linux 8, run

$ sudo dnf install -y podman

Verify podman installation

To check whether podman is installed successfully or not, try to spin ‘hello-world’ container using beneath podman command.

$ podman -v
podman version 3.3.1
$
$ podman run 'hello-world'

Note: When we run podman first time then it prompt us to choose the registry where you want to download the container image.

Output of above command would like below:

podman-installation-verification-rhel8

Prefect above confirms that podman is installed successfully. Let’s move to the next step.

Step 2) Generate Systemd Service of a container

Let’s assume we want to generate systemd service for rsyslog container. First spin up rsyslog container using following podman commands,

$ podman run -d --name <Container-Name>  <Image-Name>

Note : If you wish to download rsyslog container image from a specific registry then use following syntax:

$ podman run -d --name container-name  <registry-URL>/<image-name>

In this demonstration, I have used Red hat registry. So first login to registry

$ podman login registry.access.redhat.com
Username: <Specify-User-Name>
Password: <Enter-Password>
Login Succeeded!
$

Now try to spin container using following podman command,

$ podman run -d --name rsyslog-server registry.access.redhat.com/rhel7/rsyslog
$ podman ps

podman-run-rsyslog-container-linux

Now create systemd service for rsyslog-server container, run the following commands

$ mkdir -p .config/systemd/user
$ cd .config/systemd/user/
$ podman generate systemd --name rsyslog-server --files --new
/home/sysops/.config/systemd/user/container-rsyslog-server.service
$

As we can above that systemd service is created.

For more details on ‘podman generate systemd‘ command, refer it’s help page

$ podman generate systemd --help

podman-generate-systemd-command

Step 3) Start and Enable Container Systemd Service

Run following systemctl command to start and enable systemd service for rsyslog-server container.

$ cd .config/systemd/user/
$ systemctl --user daemon-reload
$ systemctl --user enable container-rsyslog-server.service
$ systemctl --user restart container-rsyslog-server.service

Now verify the systemd service status and container status, run

$ systemctl --user status container-rsyslog-server.service
$ podman ps

Now, When the system is rebooted, container will automatically be started via its systemd service. So let’s reboot it once and verify the container service.

$ sudo reboot

Once the system is back online, then login to system and verify the container service

$ podman ps
$ cd .config/systemd/user/
$ systemctl --user status container-rsyslog-server.service

container-status-post-reboot

Great, this confirms that rsyslog-server container is started automatically post reboot via it’s systemd service.

That’s all from this post, I hope you have found it informative. Please do share your feedback and queries.

Also Read: How to Run Jenkins Container as Systemd Service with Docker

Leave a Reply

Your email address will not be published. Required fields are marked *