066 Configure a Container to Start Automatically as a Systemd Service

Tech Tutorial: Manage Containers with systemd on Red Hat Enterprise Linux #

Introduction #

In the world of Red Hat Certified System Administration (RHCSA), understanding how to manage containers effectively is crucial. One essential skill is configuring a container to start automatically using systemd, the default system and service manager in Red Hat Enterprise Linux (RHEL). This tutorial will guide you through the process of setting up a container to run as a systemd service, ensuring it starts automatically upon system boot.

Prerequisites #

Before we begin, ensure you have the following setup:

  • A RHEL 8 or later environment.
  • Podman installed (Podman is the default container engine in RHEL 8 and above).
  • Sudo or root access to the system.
  • Basic familiarity with container concepts and the command line.

Step-by-Step Guide #

Step 1: Install Podman #

First, ensure that Podman is installed on your system. Podman is a daemonless container engine that is developed by RedHat itself.

sudo dnf install -y podman

Step 2: Create a Sample Container #

For demonstration purposes, we’ll use a simple Docker container. Here, we’ll pull an image from the Docker registry and create a container named mywebserver using the nginx image.

sudo podman pull nginx
sudo podman run -d --name mywebserver nginx

Step 3: Create a systemd Service File #

To configure the container to start automatically, we need to create a systemd service file. This file will tell systemd how to handle the container at system boot.

  1. Stop the running container (if it’s running):

    sudo podman stop mywebserver
    
  2. Generate a systemd unit file for the container:

    Podman can generate systemd unit files with its built-in generate systemd command. This makes it very simple to integrate containers into the system boot process.

    sudo podman generate systemd --name mywebserver -f
    

    This command will create a .service file in the current directory. For example, container-mywebserver.service.

  3. Move the systemd file to the systemd directory:

    sudo mv container-mywebserver.service /etc/systemd/system/
    
  4. Reload systemd to recognize the new service:

    sudo systemctl daemon-reload
    

Step 4: Enable and Start the Service #

Now, enable the service to start at boot and then start the service immediately:

sudo systemctl enable container-mywebserver.service
sudo systemctl start container-mywebserver.service

Step 5: Verify the Container is Running #

Check the status of the systemd service:

sudo systemctl status container-mywebserver.service

You should see that the service is active. Additionally, you can check that the container itself is running:

sudo podman ps

Detailed Code Examples #

Here is a more detailed example of what the container-mywebserver.service file might contain, generated by Podman:

# /etc/systemd/system/container-mywebserver.service
[Unit]
Description=Podman container-mywebserver.service
Wants=syslog.service

[Service]
Restart=always
ExecStart=/usr/bin/podman start -a mywebserver
ExecStop=/usr/bin/podman stop -t 10 mywebserver
KillMode=none
Type=forking

[Install]
WantedBy=multi-user.target

This configuration ensures that the container starts with the system under normal multi-user conditions and gracefully stops upon system shutdown.

Conclusion #

By integrating Podman containers with systemd, you gain robust control over container management, aligning container operations with standard system administration practices in RHEL. This setup not only ensures that your containers start automatically with the system but also benefits from systemd’s logging and restart capabilities, enhancing overall service reliability.

Congratulations! You now know how to configure a container to start automatically as a systemd service in a Red Hat environment, a crucial skill for any RHCSA certified professional.