Content:
It can be desirable to run containerised applications automatically when the system boots.
Unlike Docker, Podman does not have a built-in mechanism to do this. This is due to Podman’s daemon-less architecture.
This article provides a couple of solutions to ensure your containers auto-start on system boot.
Creating Systemd Service Files
The preferred solution is to use a systemd service file, but this requires the OS to be running the systemd init system. This will cover most Linux distributions, but if you’re unsure, it’s easy to check.
Simply run
systemctl
and take note of the result. If you’re presented with a list of command parameters, your system is using systemd. If an error is returned, skip over this section to the alternative method.
The first step is to create the required systemd service files, which will be used to start the required container(s). To make this process easier, a utility called podman-generate
. Podman generate produces structured data based on the contents of a pod or container.
To create a systemd service file for a container/pod, use the following command:
podman generate systemd --files --name container_name
Replace container_name
with the name of the pod or container to create a service file for. The resulting .service
file will be created in the folder where the command is executed. Note that if you specified a pod, a pod service file will be created, along with service files for all containers within the pod.
Taking a look inside a .service
file, you’ll find something similar to the entry below.
# container-mycontainer.service
# autogenerated by Podman 4.4.1
# Fri Feb 23 18:07:50 GMT 2024
[Unit]
Description=Podman container-mycontainer.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/run/user/1000/containers
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStart=/usr/bin/podman start mycontainer
ExecStop=/usr/bin/podman stop \
-t 10 mycontainer
ExecStopPost=/usr/bin/podman stop \
-t 10 mycontainer
PIDFile=/run/user/1000/containers/overlay-containers/8d36d2a167119c2ef3cd79e8bdfb22daf2dc13fa03487ab5f7190d459ca1cb9d/userdata/conmon.pid
Type=forking
[Install]
WantedBy=default.target
This might look complicated, but the important entry here is the line beginning with ExecStart
. This defines the command to execute when the service starts. In this case, the service file calls podman start
followed by the name of the container to start.
Move the .service
file(s) to the ~/.config/systemd/user
directory, so that systemd is able to find the file.
Once a service file has been moved, it can then be started. Ensure the container is currently stopped, then run
systemctl --user start container-mycontainer
Replace container-mycontainer
with the name of the service file. The .service
suffix can be included, but is not required.
If this worked correctly, the service can be enabled. Enabling the service will ensure it is automatically executed on boot.
systemctl --user enable container-mycontainer
If you’re using a pod, make sure to execute this for both the pod service, and the services for all included containers.
Reboot the system, and your services should start automatically.
Using a systemd service file has the benefit of logging container activity in the system journal, making debugging easier should issues arise.
Cron Job
If you’re unable to use systemd service files, creating a cron job is a simple alternative method. In fact, this method is much easier to set up than a systemd service file.
To create a cron job, you’ll need to open the cron file. To do this, run
crontab -e
This will automatically open the cron file, which is likely to be empty at this point.
Create a new line in the file, similar to the one below.
@reboot /usr/bin/podman start mycontainer
Replace mycontainer
with the name of the container to start. Save and exit the cron file.
That’s all there is to it. Try rebooting the system, and check if the container started correctly.
What’s even better if that with a single entry, it’s possible to start an entire pod. Simply enter a pod name instead of a container name, and the whole pod will be started.
Conclusion
This article has shown two methods to start podman containers on boot. While the systemd service option is preferred, creating a cron job is a simple alternative if you are unable to use systemd services. Either option will ensure a system reboot will not stop your container from running.