Skip to main content
In this guide, you’ll learn how to configure a container to start automatically as a rootless systemd service while attaching persistent storage for serving static content from your local machine.
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:
  1. Reset the container-tools module:
  2. Install the updated container-tools stream (version 3.0 includes Podman 2.0):
Once the installation completes, clear your screen and proceed with the following steps.

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:
  1. Create the systemd user configuration directory and a directory for your container’s persistent storage:
  2. Verify that the directories have been created by listing your home directory:
  3. Confirm the systemd configuration directory exists:
  4. Populate the storage directory with initial content. For example, create a file named kodekloud.html:
You can later use the 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 local container_storage directory to /var/www/html inside the container with SELinux context adjustment.
  • The image is pulled from registry.access.redhat.com/rhsc1/httpd-24-rhel.
After the command executes, you should see an output similar to:
  1. Confirm the container is running:
  2. 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:
  1. Change directory to your user-level systemd configuration directory:
  2. 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.
    • ExecStop and ExecStopPost: 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:
  1. Stop and remove the container:
  2. Allow the user to run services even when not logged in (enable user linger):
  3. 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:
  4. Reboot your system to verify that the container starts automatically. Once rebooted, test the web server again:
  5. Confirm that the container is running under systemd management:

Conclusion

This guide demonstrated how to configure a container for automatic startup as a rootless systemd service with attached persistent storage. With these steps, you can streamline container management and serve static content efficiently on your system. For further details on container management, refer to the Podman documentation.

Watch Video

Practice Lab