Starting Podman containers at boot with systemd
Because podman does not have a daemon (background service), there is nothing to start our containers when the system boots.
Luckily Podman has excellent integration with systemd which is known for managing background services on linux including starting them when the system starts.
Here are the steps we'll take to ensure our containers start with the system:
- We'll use
podman generate systemd
to generate systemd service files - Then we'll move those files into the appropriate systemd folder
- Finally enable and start the container as a service
NOTE: This command has been deprecated in favor of Quadlets, however the podman team won't remove the command, they will continue to fix bugs but not add any new features.
Generating systemd files with Podman
As mentioned earlier to generate the systemd files, we'll use podman generate systemd.
Let's see it in action:
# command synopsis
podman generate systemd <container name | id>
# example:
podman generate systemd my-pgsql > my-pgsql.service
In the example we have generated a systemd service file for our my-pgsql
example container and piped the output into a file my-pgsql.service
that we
can easily reference later on.
Here is an example using my website container:
Moving the generated file to systemd folder
Our next step after generating the container service files is to move it to your user's systemd folder, this will allow us to enable and start our container as a service (CaaS), lol if you get the reference.
The folder can be found on this path ~/.config/systemd/user
, this is for your user with rootless containers.
We can use the linux mv
command to move the file like this:
mv <file-name> ~/.config/systemd/user/
# from example earlier:
mv jaze-dev.service ~/.config/systemd/user/
Enabling the systemd service
Now that we've moved the service file into our user's systemd folder. All that is left to do is simply enable and start the service:
# enable the service
systemctl --user enable my-container.service
# start the service
systemctl --user start my-container.service
Once that is done, we can simply test by either rebooting with the linux reboot
command or restarting the service.
In summary
We've learned how to use podman generate systemd
to allow our containers to start at startup, although it is deprecated the
team is not going to remove the command and they will still fix bugs with it as well.
Here is a recap of the steps we took:
- Use
podman generate systemd <container>
to generate a service file - Move the file to
~/.config/systemd/user/
- Enable and start the service with
systemctl --user enable/start
- Test by rebooting or restarting the service
I'll soon be covering quadlets, which is an upgrade for containers that integrate with systemd.
Thanks for reading and I hope this helped you on your podman journey!!