Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ecnepsnai
Created February 26, 2022 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecnepsnai/887b80b94131060184ce4ea17ded1288 to your computer and use it in GitHub Desktop.
Save ecnepsnai/887b80b94131060184ce4ea17ded1288 to your computer and use it in GitHub Desktop.
Better Podman SystemD Unit

This is a systemd unit file for running a podman container as a systemd service.

For rootless containers, you can use systemd user units (More info)

Broken down, it does the following:

  1. ExecStartPre pulls the image. If you use the :latest tag, then every time you start this service it'll pull the latest version
  2. ExecStart runs the container. It's important to use a container name, since it's referenced elsewhere. Don't detact the container (I.E. don't include -d)
  3. ExecStopPost after the container is stopped, it'll remove the container (if it is still present), and remove the image. Removing the image is useful if you're using the :latest image tag.
[Unit]
Description=My Podman Container
[Service]
Type=simple
ExecStartPre=/usr/bin/podman pull image:tag
ExecStart=/usr/bin/podman run --rm --name my_container image:tag
ExecStop=/usr/bin/podman stop my_container
ExecStopPost=/usr/bin/podman rm --ignore my_container
ExecStopPost=/usr/bin/podman rmi --ignore image:tag
Restart=on-failure
[Install]
WantedBy=default.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment