Ensure that your system’s container-tools module stream is updated to use at least Podman version 2.0. RHEL 8’s default module stream includes Podman version 1.6, which is insufficient for this demonstration.
Step 1. Update the container-tools Module Stream
Before you begin, change the container-tools module stream. Follow these steps:-
Reset the container-tools module:
-
Install the updated container-tools stream (version 3.0 includes Podman 2.0):
Step 2. Prepare Directories for systemd Service and Persistent Storage
Configure your environment to run the container as a non-root user. First, create the necessary directories in your home directory:-
Create the systemd user configuration directory and a directory for your container’s persistent storage:
-
Verify that the directories have been created by listing your home directory:
-
Confirm the systemd configuration directory exists:
-
Populate the storage directory with initial content. For example, create a file named
kodekloud.html:
cat command to verify the file’s content.
Step 3. Create and Test the Container
Next, create a container that will run a web server by pulling a web server image from Red Hat’s registry. Run the container in detached mode with the following command:Command Breakdown
-d: Runs the container in detached mode.--name container_service: Assigns a custom name to the container.-p 1025:8080: Maps container port 8080 to host port 1025. Non-root users must use ports above 1024.-v ~/container_storage:/var/www/html:Z: Mounts your localcontainer_storagedirectory to/var/www/htmlinside the container with SELinux context adjustment.- The image is pulled from
registry.access.redhat.com/rhsc1/httpd-24-rhel.
-
Confirm the container is running:
-
Test the web server using curl:
Step 4. Generate a Systemd Unit File for the Container
Now that your container is running correctly, generate a systemd unit file for it. Follow these steps:-
Change directory to your user-level systemd configuration directory:
-
Generate the systemd unit file using Podman:
This command creates a unit file named similarly to
container-container_service.service. You can inspect the generated file using:The file includes details such as:ExecStart: Command to launch the container with proper options.ExecStopandExecStopPost: Instructions for a clean shutdown and removal of the container.
Step 5. Enable and Manage the Systemd Service
Before enabling the systemd service to manage the container, stop and remove the currently running container:-
Stop and remove the container:
-
Allow the user to run services even when not logged in (enable user linger):
-
Reload the systemd user daemon and enable the service to start immediately and on boot:
You should see output indicating the creation of symbolic links, such as:
-
Reboot your system to verify that the container starts automatically. Once rebooted, test the web server again:
-
Confirm that the container is running under systemd management: