How to Start Podman Containers on Boot

Start Podman containers on boot

Podman has gained popularity as a container management tool that offers a daemon-less architecture, which is a significant departure from traditional Docker setups. This feature enhances security by allowing unprivileged users to run containers without root access. However, this also means that Podman containers do not automatically restart after a system reboot, unlike Docker containers which are managed by a daemon that starts on boot.

The key to enabling Podman containers to start on boot lies in the integration with systemd, the init system used by many Linux distributions. Systemd is responsible for managing system services and can be leveraged to ensure that Podman containers start automatically upon system startup.

In this guide, we will walk through the steps to configure Podman containers to start on boot using systemd on Ubuntu.

How to Start Podman Containers on Boot

Step 1: Preparing Podman Container

Step 2: Generate a Systemd Unit File

Step 3: Placing the Service File

Step 4: Enable and Start the Service

Step 5: Verify the Setup

Step 6: Testing the Boot Configuration

Conclusion

How to Start Podman Containers on Boot

Podman has emerged as a popular container management tool, offering a daemon-less alternative to Docker. One of the common requirements when working with containers is ensuring they start automatically upon system boot. This is crucial for maintaining the availability of services and applications without manual intervention.

Prerequisite: Install Podman on Linux

For most modern Linux distributions, Podman is available in the official repositories as Ubuntu 24.04:

sudo apt update # For Debian/Ubuntu-based systems
sudo apt install podman

Verifying the Installation:

Once installed, users can check it by running:

podman version

Step 1: Preparing Podman Container

Before diving into the systemd configuration, ensure that your Podman container is set up correctly and can run without issues.

If you’re new to Podman, you must familiarize yourself with the basic commands to pull images, create containers, and manage them as below.

Pull the Image

    Before creating a container, you need to pull the desired image. Let’s pull the Ubuntu image as below:

    podman pull ubuntu:latest

    Create the Container

      Now, utilize the podman create command to create the new container. This command creates a container but doesn’t start it:

      podman create --name my_ubuntu_container ubuntu:latest

      Here, –name my_ubuntu_container: It assigns a name to the container, and ubuntu:latest: It specifies the image to use.

      Start the Container

        For starting the container on Ubuntu, utilize the podman start command as below:

        podman start my_ubuntu_container

        Users can also check the services via the ls command:

        ls *.service

        Step 2: Generate a Systemd Unit File

        Systemd is the init system for modern Linux distributions, responsible for managing system services. Now, users can generate a systemd unit file through Podman’s “generate systemd” command.

        Let’s create a unit file that tells systemd how to manage your container service:

        podman generate systemd --new --name my_ubuntu_container

        Note: This command creates a service file named my_ubuntu_container.service in /etc/systemd/system or home directory. You can customize this file further based on your needs.

        Step 3: Placing the Service File

        Once the “.service” file is generated, move it to the appropriate directory for systemd to recognize it. For user services, this would typically be “~/.config/systemd/user”. For system-wide services, the directory would be “/etc/systemd/system”.

        sudo mv container-my_ubuntu_container.service /etc/systemd/system

        Step 4: Enable and Start the Service

        With the systemd unit file in place, users can now enable the service to start on boot and start the service immediately if desired. This command tells systemd to create the necessary symlinks to start your container service at boot time.

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

        This initiates the container based on the configurations set in the service file.

        Step 5: Verify the Setup

        After a system reboot, you can verify that your container started successfully by checking the status of the systemd service:

        sudo systemctl status container-my_ubuntu_container.service

        Or users can list the running containers with Podman. This command provides the current status of your container service, ensuring that it’s operational:

        podman ps

        Step 6: Testing the Boot Configuration

        To test if the container starts on boot, you can reboot your system and check the status again:

        reboot

        If everything is configured correctly, your container should be up and running after the system boots.

        Note: While systemd integration offers a robust solution for auto starting Podman containers, it’s essential to understand the implications of each step and tailor the process to your specific needs and system configurations.

        Conclusion

        Automating the startup of Podman containers using systemd ensures that your services are always available and reduces the need for manual management. By following these steps, you can configure your Podman containers to start on boot, ensuring that your services remain available after a system restart. This setup is particularly useful for production environments where uptime and reliability are critical.