Podman to Systemd using Quadlets

· Vodka Rat - 伏特加鼠

A quick guide to going from Dockerfile to Systemd service

Not Docker? #

Docker requires a background centralized Docker-daemon to be up and running for containers to run. Podman doesn't require this using a "fork/exec" model running containers individually. This also inadvertently reduces memory overhead, making it more suitable for thin clients or other ligtweight servers.

I will be covering the advised Quadlets method and the much easier but deprecated "systemd generate" method.

Quadlets method #

This is the advised way to go about this.

Build the image, and run to create the container.

1podman build -t this-app .
2
3podman run --name this-container this-app
4# Use whatever flags are necessary
5
6podman ps -a
7#CONTAINER ID  IMAGE                                                             COMMAND          CREATED         STATUS                       PORTS       NAMES
8#8c9e4286f18f  localhost/this-app:latest                                         python3 main.py  24 seconds ago  Exited (130) 20 seconds ago              this-container

Make a new containers systemd directory.

1sudo mkdir -p /etc/containers/systemd

Alternatively

1sudo mkdir -p /usr/share/containers/systemd

Next create a .container file in your new directory.

[Unit]
Description=Describe the container here
Wants=network-online.target
After=network-online.target

[Container]
Image=this-app

# Use the name of the container you made
ContainerName=this-container

[Install]
WantedBy=multi-user.target default.target

For example, my-file.container can be used for the container named this-container if ContainerName is defined in the file, otherwise it will attempt to resolve a container named my-file.

Next, the service should be generated when systemctl is reloaded. Reload systemctl then start your new service.

1sudo systemctl daemon-reload
2sudo systemctl enable this-container.service
3sudo systemctl start this-container.service
4sudo systemctl status this-container.service

The easier, deprecated method #

This method is for cheats and it is not guaranteed that you will always be able to do it this way.

Build the image, and run to create the container.

1podman build -t this-app .
2
3podman run --name this-container this-app
4# Use whatever flags are necessary
5
6podman ps -a
7#CONTAINER ID  IMAGE                                                             COMMAND          CREATED         STATUS                       PORTS       NAMES
8#8c9e4286f18f  localhost/this-app:latest                                         python3 main.py  24 seconds ago  Exited (130) 20 seconds ago              this-container

Follow this with Podman's deprecated "generate systemd" command.

1podman generate systemd --name this-container --files --new
2# --new creates a new container, allowing you to make changes to the original without screwing with the daemon

The flag --files generates a .service file. Move this to the systemd directory (which should be /etc/systemd/system/), reload systemd and enable your new service.

1sudo systemctl daemon-reload
2sudo systemctl enable this-container.service
3sudo systemctl start this-container.service
4sudo systemctl status this-container.service
last updated:

拜拜