Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active November 9, 2024 19:02
Show Gist options
  • Save plembo/9dda7e1fe28ac4f7ef4a1a5ef43ee069 to your computer and use it in GitHub Desktop.
Save plembo/9dda7e1fe28ac4f7ef4a1a5ef43ee069 to your computer and use it in GitHub Desktop.
Automatically start pod on reboot

Auto start podman containers on reboot as systemd services

NOTE: This was written while I was experimenting with using podman in lieu of docker, something I'm now looking at again. Not all docker images work with podman, so be prepared to struggle with the system and to engage in many fruitless Internet searches.

Intro

OK, so who knew podman containers won't automatically start on reboot? Yeah. Me neither.

Found out after a family member complained about getting a 500 error when trying to reach a PiGallery2 instance I had set up on the home server.

Fixing this is easy, as detailed in Pratham Patel's "How to Autostart Podman Containers?" over on Linux Handbook: A systemd service is created and enabled for each container.

Steps

What follows is my own summary of the steps Pratham sets out, modified for use in an environment where podman containers are owned and run by a nonprivileged user (in this case, "pods": see my One (non root) podman account to rule them all). Here, we'll use my "pigallery2" container as the target container.

  1. As root (or sudo root) enable lingering (sudo loginctl enable-linger).
  2. Log in as the container user (ssh pods@example, simply doing an "su - pods" or "sudo su - pods will not suffice).
  3. Shut down the container (podman stop pigallery2).
  4. Create a systemd service for the container (podman generate systemd --new --name "pigallery2" >pigallery2.service).
  5. Move the systemd file to the user's systemd service directory (~/.config/systemd/user/, if this doesn't exist create it).
  6. Reload systemd for the user (systemctl --user daemon-reload).
  7. Enable the service (systemctl --user enable pigallery2).
  8. Reboot.
  9. Log back in as the container user and verify the container is running (podman ps).
  10. Check service status (systemctl --user status pigallery2).

References

Prathan Patel. "How to Autostart Podman Containers?". Linux Handbook, 28 September 2022, https://linuxhandbook.com/autostart-podman-containers/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment