Creating Podman container systemd unit files for rootless containers

By default Docker or Podman doesn’t have any systemd unit to start the container during a reboot. Let’s see how to create a systemd unit file for a rootless container and manage how to start, stop, restart and check the status of the container.

For reference, container name and application user used in this guide.

  • Container name – web_service_container
  • Container hosted app user – web_app_user

Before staring with config, lets first check the pid of running rootless containers, the pid can be used to kill the container when we need to test the system unit files.

$ su - web_app_user
$ podman top web_service_container hpid pid user args

Create the required directories under application users home directory to generate the systemd unit files.

$ mkdir -p .config/systemd/user/
$ cd .config/systemd/user/

Generate the systemd unit files.

$ podman generate systemd --name web_service_container ~/.config/systemd/user/container-web_service_container.service

If any changes required edit the generated systemd unit file and make the changes.

$ vim ~/.config/systemd/user/container-web_service_container.service

Reload the daemon

$ systemctl --user daemon-reload

Now the rootless containers are ready to managed using the systemd unit files.

$ systemctl --user status container-web_service_container.service
$ systemctl --user restart container-web_service_container.service
$ systemctl --user enable container-web_service_container.service

Enable/disable user lingering for one or more users, This allows users who are not logged in to run long-running services

$ sudo loginctl enable-linger web_app_user

Add this to .bashrc file under web_app_user user home directory

echo "export XDG_RUNTIME_DIR=/run/user/$(id -u)" >> ~/.bashrc

Now use the pid to kill the container or take a reboot to check whether the containers are started persistently during the reboots.

# reboot
$ podman ps

To check the container status run below commands.

$ cd /home/web_app_user/.config/systemd/user
$ systemctl --user status container-web_service_container.service

That’s it, we have created a systemd unit file for rootless container and start to manage the containers.

Exit mobile version