Podman Keep Container Running: A Comprehensive Guide

Estimated reading time: 5 minutes

Last updated on November 8th, 2024 at 05:43 pm

Understanding the Podman and Containers

What is Podman?

Podman is an open-source, daemon-less, and Linux native container management engine to develop, manage, and run containers and pods like Docker and Kubernetes. Podman does not require a centralized daemon to manage containers, which makes it lightweight and flexible.

Podman offers identical CLI commands to Docker, but there are fundamental differences between Podman and Docker.

If you want to learn more about choosing the right technology for your next project:

Podman vs Docker: Choose the Right One

Importance of Podman Keep Container Running

Keeping your Podman container running is critical for the application to ensure the availability and reliability of the services. Additionally, keeping the container running helps troubleshoot the misbehaving application containers.

Ensure the application runs inside the container and stays active to prevent potential service downtime and uninterrupted user experience.

Podman Cheat Sheet

FREE Podman Cheat Sheet (Everything You Need, In One Place)

This is the last Podman Cheat Sheet you’ll ever need. Why?
Because it’s not just a list of commands—it’s a shortcut to make your work easier, faster, and more effective.

Stop wasting time digging through documentation. With this cheat sheet, you’ll get exactly what you need, right when you need it.

Methods to Podman Keep Container Running

1. Run Container in Detached Mode

Running the container in detached mode keeps it running in the background as opposed to the normal way which stops the container once you close the terminal. Background or detached mode you can close your terminal without worrying about stopping the container.

Let’s run the container in detached mode with -d flag:

Bash
podman run -d --name podman_detached alpine sleep 1000

Once you run this command it will print the container ID

Podman Keep Container Running - Detached
Podman Keep Container Running – Detached

Podman Attach to Running Container Bash: 3 Simple Methods

2. Use Long-Running Commands

The container runs as long as the main process inside is running. To keep the Podman container running, you can use the long-running commands that don’t terminate quickly.

Use tail -f /dev/null as the main process

Bash
podman run -d --name podman_dev_null alpine tail -f /dev/null

Use the sleep infinity command:

Bash
podman run -d --name podman_sleep alpine sleep infinity

Use the Infinite Loop:

Bash
podman run -d --name podman_loop alpine sh -c "while true; do sleep 1; done"

Use the cat command:

Bash
podman run -d --name podman_cat alpine cat

You can use any of the above command that runs as the main process and let Podman keep container running.

3. Implement Restart Policies

Unexpected failure and crash happened, but how to keep the container running during those events? Restart policies ensure the containers restart automatically in certain conditions without manual effort.

Let’s run the container with --restart=always to use the policies:

Bash
podman run -d --name podman_restart --restart=always alpine sleep 1000

Now this container will automatically restart if it stops.

4. Use Systemd for Container Management

We learned about the restart policies where the container starts automatically if an application crashes, but what about the system-level failure? How can we ensure the application container keeps running after a system reboot?

You can use systemd with the Podman to manage the container lifecycle as system services:

Bash
podman generate systemd --new --name podman_systemd > /etc/systemd/system/podman_systemd.service

# Enable Service
systemctl enable podman_systemd.service

# Start Service
systemctl start podman_systemd.service

This will create the systemd service file that enables it at boot time and manages it.

5. Use Pods for the Persistent Service

Podman allows you to run multiple containers in a single unit called pods that share the same network and storage namespace.

Let’s create our first Podman pod:

Bash
podman pod create --name podman_pod

Once the pod is created you can run the first container inside it:

Bash
podman run -d --pod podman_pod alpine sleep 1000

You can see the output by running podman pod ps

Podman Keep Container Running - Pods
Podman Cheat Sheet

FREE Podman Cheat Sheet (Everything You Need, In One Place)

This is the last Podman Cheat Sheet you’ll ever need. Why?
Because it’s not just a list of commands—it’s a shortcut to make your work easier, faster, and more effective.

Stop wasting time digging through documentation. With this cheat sheet, you’ll get exactly what you need, right when you need it.

Best Practices to Podman Keep Container Running

Monitor Container Health

Monitor the health of the containers using built-in commands such as podman ps and podman inspect. For more comprehensive monitoring opt-in for the tools like Prometheus, Grafana, and Netdata.

Handle Graceful Failure

Graceful failure is better than force stop, where we use the podman kill rather than podman stops. Graceful allows the process to run inside the container to prevent data loss and unexpected behavior.

Implement the strategies to handle the failure with restart policies and log monitoring for detecting errors with Elastic Search

Automated Restarts

You can use the cron jobs for the automatic restart if necessary:

Bash
*/10 * * * * podman start podman_container

This cron job checks every 10 minutes to ensure the container is running.

Troubleshooting Common Issues with the Podman Container

Identify the Reasons for Container Stops

Identifying the reason ensures the necessary fix to prevent issues in the future.

Let’s identify the reason by checking the container logs:

Bash
podman logs podman_container

Investigate and look for the error just before the container stops or anything that is out of order.

Fix Common Configuration Problems

The most common problem with “Podman Keep Container Running” is resource constraint. Ensure there are enough resources available to keep the container running.

Bash
podman run -d --memory=1024m --cpus=1 alpine

Summary

  • Use detached mode and long-running commands to keep Podman containers running.
  • Implement restart policies and use systemd for lifecycle management.
  • Utilize Podman pods for managing multiple containers.
  • Monitor and troubleshoot to ensure uptime and reliability.

Additional Reading

Podman vs Docker: Choose the Right One

Podman Attach to Running Container Bash: 3 Simple Methods

How to Use Docker Compose with Podman

Podman Cheat Sheet

FREE Podman Cheat Sheet (Everything You Need, In One Place)

This is the last Podman Cheat Sheet you’ll ever need. Why?
Because it’s not just a list of commands—it’s a shortcut to make your work easier, faster, and more effective.

Stop wasting time digging through documentation. With this cheat sheet, you’ll get exactly what you need, right when you need it.

Kashyap Merai

Kashyap Merai

Kashyap Merai, a Certified Solution Architect and Public Cloud Specialist with over 7 years in IT. He helped startups in Real Estate, Media Streaming, and On-Demand industries launch successful public cloud projects.

Passionate about Space, Science, and Computers, He also mentors aspiring cloud engineers, shaping the industry's future.

Connect with him on LinkedIn to stay updated on cloud innovations.