Managing container in Podman with systemd unit files

sonu kushwaha
4 min readFeb 12, 2022

--

NOTE:-if you don't know what systemd is ,then you must read this first

In this blog , i will be showing how to we can run/control podman container with the help of systemd unit file (ie service file) so let me explain what service we will get for the container, will have a service section which will have a key and value as “Restart=0n-failure” this wall be like watch dog as the term makes clear it will create new container if previous one fails. so here can be two scenarios.

  1. first scenario →running container’s unit file as root user.
  2. second scenario → running container’s unit file as general user.

now ,considering the first scenario what we will do is create a pod/container, then with the help of the running container we will generate a systemd unit file ie service unit file its termed/named as “sonu.service”, and will place the file in the “/etc/systemd/system/” directory.so lets follow up till this process

podman run -d — name sonu -v tj:/var/www/html -p 8080:80 nextcloud
podman ps
podman inspect sonu | grep -i pid
podman generate systemd sonu > /ect/systemd/system/sonu.service

After, we are done with creating service unit file and placing it on the right directory , we need to reload our daemon, so that systemd can keep the track of the new service file, that we have just added with the name called sonu.service

systemctl daemon-reload

cat /ect/systemd/system/sonu.service
systemctl daemon-reload

to make sure , our newly created service file is now in the record of systemd ,we will go with the following command

systemctl status sonu.service

systemctl status sonu.service

now we are ready to start our service, that will manage container,so now if we try killing pod , this service will restart the new one.

systemctl start sonu.service

systemctl start service

here, we need to keep in mind that our service is not enable so once the system is booted(can be warm or cold), the service need to manually started hence we need to enable the service

systemctl enable sonu.service

systemctl enable sonu

SECOND SCENERIO ( running container’s unit file as general user)

for , example i am logged in as general user ie sonu , hence what I need to do now is to place the systemd unit file inside the directory called /home/sonu/.config/systemd/user , make sure that you need to create the “systemd/” and “user/” directory on your own , inside the “.config” director of the general user directory.

then, another thing you need to do is like , add “— user” whenever you you use systemctl cmd for example (see below) after that rest of the procedure is almost same .

systemclt — user daemon-relaod

systemctl — user status sonu.service

systemctl — user start sonu.service

systemctl — user enable sonu.service

One more thing ,if the service is enabled in the second scenario, the service will start and stop when logged in and logged out respectively ,so if one’s need to start the service even after boot , the with out login , you need to run following command

sudo loginctl enable-linger sonu

that’s it . if you want to know more about systemd its in progress , will be posting its link here itself .

Resource:-here

--

--

sonu kushwaha
sonu kushwaha

Written by sonu kushwaha

open source enthusiast Docker, kubernetes

No responses yet