Monday, June 12, 2023

Autostart podman containers on host boot/reboot

Podman is an excellent alternative to docker, but it would not survive host reboot since it does not have any daemon running. Is all hope lost? Nope, podman has one trick up its sleeve that can save the day.

Introducing "podman generate" command. This command can generate yaml, json or systemd file for any containers. In this case, we are going to generate a systemd script for our container, to make sure it survives host reboot.

How to use this tool? First we need a running container.

Once we have a running container, simply run below command to create systemd service file for your container (in this example, mycontainer is the name of the container), and save it to a file called container-mycontainer.service.
$ podman generate systemd --new --name mycontainer -f

Then, move the conatainer-mycontainer.service file into /etc/systemd/system, for systemd to start recognizing your service (we changed the name to just mycontainer.service, to shorten the name, and make it easier to type). 
$ sudo mv container-mycontainer.service /etc/systemd/system/mycontainer.service

After that, we need to reload the systemd for the root user, to make systemd aware of the new service.
$ sudo systemctl daemon-reload

Now, start the new service, and enable it on every boot
$ sudo systemctl enable --now mycontainer.service

Check whether your new service is running
$ sudo systemctl status mycontainer.service


No comments:

Post a Comment