cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 1,193 Views

Enabling containers at boot

When you use Podman directly from the command line, it operates only as long as you are actively using the terminal session. Podman-managed containers will cease to run once you close the session. This behavior isn't suitable for your requirement of having a Podman-based service remain functional even after system restarts. This is where integrating Podman with systemd comes into play.

Refer this doc here  :https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/building_runnin... 

Chetan_Tiwary__0-1700576686440.png

For a privileged user - It is easy to play with systemd service , let us see the same for a basic user ( because we want this user account to start a service that persists over logouts/ reboot ) :

1. You need to have that basic user present in the system , if not create the user with useradd command.

2. Login with the user and run the command : podman version ( just to test podman command)

3. Now, we have to inform systemd to keep the managed container running and  automatically start when the system boots up / even after the user who started it logs out. 

For this, we use loginctl command ( This we have to do it as root user ) ( https://www.freedesktop.org/software/systemd/man/latest/loginctl.html )

#loginctl show-user <username> | grep ^Linger   

If Linger=no then enable the lingering using :

#loginctl enable-linger <username>

4. Now as that user : create / run the container from the image ( as you generally do with persistent volume )

a. podman pull registry.redhat.io/xxx

b. podman images ls

c. podman run -d --name=<name> -p 8080:80 -v /pathinhostvm:/pathincontainer:Z <image>

d. podman ps

5. Now the persistent configuration with systemd will be done ( for the non privileged user that we created in step 1):

We will use podman  to do our work : https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/building_runnin... 

as that user :

a. #mkdir -p ~/.config/systemd/user  ( You could also use the /etc/systemd/system directory, but only a privileged user can copy the systemd service file to this directory, hence we are not using that dir)

b. #podman generate systemd  --new --name <containername>  --files

refer : podman generate systemd --help for all parameters available or https://docs.podman.io/en/latest/markdown/podman-generate-systemd.1.html 

Chetan_Tiwary__1-1700581366384.png

Chetan_Tiwary__2-1700581407986.png

The generated systemd service unit file will contain instructions for how to start, stop, and manage the  container.

The Restart=on-failure line sets the restart policy and instructs systemd to restart when the service cannot be started or stopped cleanly, or when the process exits non-zero.
The ExecStart line describes how we start the container.
The ExecStop line describes how we stop and remove the container.

Chetan_Tiwary__3-1700582130710.png

c. #cp -Z <containername>.service ~/.config.systemd/user/     ( copying a unit file to $HOME/.config/systemd/user and enabling it marks the unit file to be automatically started on user login)

d. You can check the generated systemd unit file using #cat ~/.config.systemd/user/container-name.service 

e. #systemctl --user daemon-reload  (Reload the user systemd service to use the new service unit)

f. #systemctl --user enable --now <containername>.service

g. #systemctl --user enable <containername>.service

h. #systemctl --user status  <containername>.service

That is it !!

When you do not need this service : just disable it and stop it like any service units. 

Refer Ch14s05 in RH134v9 course for practise.

 

1 Reply
Wasim_Raja
Moderator
Moderator
  • 1,111 Views

Excellent and very handy info, thank you for sharing @Chetan_Tiwary_ 

Join the discussion
You must log in to join this conversation.