Creating systemd tasks for applications and podman rootless containers
Here I describe a procees of creating a simple service startup job at linux.
Sample systemd file
Here below is shown a sample service file for systemctl
[Unit]
Description=Description for application
After=network.target
[Service]
User=<user>
Group=<group>
WorkingDirectory=<path where running file is located>
Environment=<path with environment variable PATH "PATH=/location">
EnvironmentFile=<path to environment file>
ExecStart=<a command with necessary parameters to start application>
[Install]
WantedBy=multi-user.target
Running and activating tasks
A service file should be located under /etc/systemd/system
folder. A service file can be named anyhow, but extension of the file should be ended with .service
. For instance: /etc/systemd/system/testapp.service
.
When file is created reload daemon:
sudo systemctl daemon-reload
And start application:
sudo systemctl start testapp.service
If application has to be started automatically, do not do not forget to enable it:
sudo systemctl enable testapp.service
Running rootless containers
In order to automate starting and stopping a rootless container on podman I followed these steps:
- Generating systemd unit file using and update it:
[admin@podman ~]$ podman generate systemd --new --files --name registry
/home/admin/container-registry.service
After update file looks like as following:
\# container-registry2.service
\# autogenerated by Podman 4.0.2
\# Mon Jun 6 19:38:56 CEST 2022
[Unit]
Description=Podman container-registry2.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=%t/containers
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStartPre=/bin/rm -f %t/%n.ctr-id
ExecStart=/usr/bin/podman start registry2
ExecStop=/usr/bin/podman stop registry2
Type=notify
NotifyAccess=all
[Install]
WantedBy=default.target
- Installation generated unit file
For rootless container I need to store the unit file in ~/.config/systemd/user/
. For the root container copy the file to /etc/systemd/system/
.
cp container-registry.service ~/.config/systemd/user/registry.service
Then, for running the user services placed in $HOME/.config/systemd/user, enable service using --user
parameter:
[admin@podman ~]$ systemctl --user enable registry.service
Activate controlling the systemd login for admin:
[admin@podman ~]$ loginctl enable-linger admin
That’s it.
Reference
- podman generate systemd
- other resources