In this quick article we will be discussing how to start podman containers on boot. If you are used to Docker, it may come as a surprise that this is not so simple with podman. Don't fret, we will show you how to start your podman containers at boot with systemd units in just a few easy steps.

Create and Start Your Podman Container

The first step is to create and start your container. Creating containers is outside the scope of this tutorial, but if you are here chances are you have this step done already.

For this example I will be using the unifi controller that I have running in a podman container. This is a perfect use case since I need this controller running at all times.

$ podman ps
CONTAINER ID  IMAGE                                           COMMAND         CREATED         STATUS             PORTS                                                                                                 NAMES
c23d1c116c00             unifi           18 minutes ago  Up 18 minutes ago>8080/tcp,>8443/tcp,>3478/udp,>10001/udp  unifi

Now that I have my container configured and running, let's create a systemd unit file.

Create a Systemd Service Unit from the Podman Container

Podman makes it simple to create a systemd unit file for your containers with podman generate. Let's use it to generate the service unit file. Below is an example command, you will need to change unifi to the name of your container.

podman generate systemd --new --name unifi

Here is an example output from the above command:

Using podman generate to create a systemd service unit file that can be used to start podman containers on boot.

Now that we know how to generate the unit file, let's move it to the correct location and get systemd working with it.

To do this, I used simple redirection. You can use the --files option if you like.

podman generate systemd --new --name unifi > ~/.config/systemd/user/unifi.service

Here we redirected the generated file into ~/.config/systemd/user/unifi.service which is a location for user generate unit files. Now that we have the service unit file generated we can configure the service to start out podman container on boot.

Set SELinux Context and Permissions of New Unit File

Set the permissions of the new file. It does not (should not) need to be executable.

chmod 644 ~/.config/systemd/user/unifi.service

Now ensure SELinux has the correct context for the file. If you are not using SELinux you can skip this.

restorecon ~/.config/systemd/user/unifi.service

Enable Your New Systemd Service to Start Podman Container on Boot

First let's tell systemd that we created a new service unit.

systemctl --user daemon-reload

Now that systemd has read the new file, you can enable the service just like any other daemon/service.

systemctl --user enable unifi.service

Remember to use the name of the service you created, not unifi.

Once you have it enabled, you can use systemctl to check the status of the new service unit.

systemctl --user status unifi.service

Here is an example. Notice the third line where is says "enabled". This means the service is enabled to start at boot.

[savona@fenrir user]$ systemctl --user status unifi.service 
● unifi.service - Podman container-unifi.service
   Loaded: loaded (/home/savona/.config/systemd/user/unifi.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2023-03-11 13:25:15 EST; 35min ago
     Docs: man:podman-generate-systemd(1)
  Process: 3930 ExecStartPre=/bin/rm -f /run/user/1000/unifi.service.ctr-id (code=exited, status=0/SUCCESS)
 Main PID: 4126 (conmon)

That's it! You can now do a reboot and test your new service unit. In just three easy steps we are now able to start podman containers on boot.