Podman Essential

Podman is an open-source container engine that runs and manages Open Container Initiative (OCI) containers and pods. Unlike Docker, which relies on a central daemon process, Podman runs containers directly as child processes of the Podman command. This daemonless architecture enhances security by eliminating a potential attack surface and improves flexibility by allowing containers to run rootless (as non-root users).

Podman is fully compatible with Docker CLI commands, making migration straightforward for existing Docker users. It also supports Kubernetes YAML files, enabling a smooth transition to container orchestration platforms.

Key Podman Snippets and Commands

1. Create and Run a Container

Run an Nginx container in detached mode with resource limits and restart policy:

podman run -dt \
  --name my-nginx \
  --memory=512m \
  --cpus=1.0 \
  --restart=unless-stopped \
  -p 8080:80 \
  nginx:alpine
  • -d runs the container in the background
  • -t allocates a pseudo-TTY
  • --memory=512m limits memory usage to 512MB
  • --cpus=1.0 limits CPU usage to 1 core
  • --restart=unless-stopped automatically restarts the container unless explicitly stopped
  • -p 8080:80 maps host port 8080 to container port 80
  • Access Nginx homepage via http://localhost:8080

2. Create a Pod with Shared Network and Add Containers

Pods group containers sharing resources like network namespaces, making them ideal for multi-container applications.

Create a pod with shared network:

podman pod create --name webapp-pod -p 8080:8080

Add an application container to the pod:

podman run -dt --pod webapp-pod \
  --name webapp-container \
  -e DATABASE_URL=postgresql://dbuser:dbpass@localhost:5432/mydb \
  my-webapp:latest

Add a supporting service container to the same pod:

podman run -dt --pod webapp-pod \
  --name redis-container \
  redis:alpine

List containers in the pod:

podman ps --pod

3. Start, Stop, and Remove Containers with Status Checking

Start a container only if it’s not already running:

if [ "$(podman inspect -f '{{.State.Running}}' my-nginx 2>/dev/null)" != "true" ]; then
  podman start my-nginx
fi

Gracefully stop a container with timeout:

podman stop --time 30 my-nginx

Remove a container (must be stopped first):

podman rm my-nginx

Force remove a running container:

podman rm -f my-nginx

4. Comprehensive Pod Management

Start all containers in a pod:

podman pod start webapp-pod

Stop all containers in a pod gracefully:

podman pod stop --time 30 webapp-pod

Pause and unpause a pod (useful for debugging):

podman pod pause webapp-pod
podman pod unpause webapp-pod

Remove a pod (containers must be stopped):

podman pod rm webapp-pod

Force remove a pod and all its containers:

podman pod rm -f webapp-pod

5. Advanced Inspection and Troubleshooting

View detailed container information in JSON format:

podman inspect my-nginx | jq '.[0].NetworkSettings.Ports'

Monitor container resource usage:

podman stats my-nginx

View pod details including container relationships:

podman pod inspect webapp-pod | jq '.[0].Containers[].Id'

Check container logs with timestamps:

podman logs --timestamps my-nginx

Follow container logs in real-time:

podman logs -f my-nginx

6. Batch Operations and Automation Scripts

List all containers, including stopped ones:

podman ps -a

Batch stop all running containers:

podman stop $(podman ps -q)

Batch remove all stopped containers:

podman rm $(podman ps -aq --filter status=exited)

Clean up unused images:

podman image prune -f

Automated backup script for all containers:

#!/bin/bash
for container in $(podman ps -q); do
  podman commit $container backup-$container-$(date +%Y%m%d)
done

7. Rootless Container Management

Run containers as non-root user (requires user namespace setup):

podman run -dt --name rootless-nginx -p 8080:80 nginx

Check user namespace mappings:

podman unshare cat /proc/self/uid_map

8. Working with Registries and Images

Login to a private registry:

podman login registry.example.com

Build an image from Dockerfile with custom tag:

podman build -t my-app:v1.0 .

Push image to registry:

podman push my-app:v1.0 registry.example.com/my-app:v1.0

Why Use Pods in Podman?

Pods allow multiple containers to share the same network namespace and IPC, making it easier to manage multi-container applications locally without complex orchestration tools. For example, you can run a web app container and a database container inside the same pod, allowing them to communicate over localhost.

This architecture closely mirrors Kubernetes pods, making local development and testing more consistent with production environments. It also simplifies networking configurations and enables better resource sharing between related containers.

Advanced Use Cases

1. Systemd Integration for Persistent Pods

Generate systemd service files for automatic pod startup:

podman generate systemd --new --files --name webapp-pod

This creates systemd unit files that can be installed to automatically start pods on boot.

2. Health Checks and Monitoring

Define health checks in container run commands:

podman run -dt \
  --name health-nginx \
  --health-cmd="curl -f http://localhost || exit 1" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  nginx

3. Volume Management for Data Persistence

Create named volumes for data persistence:

podman volume create my-data-volume

Mount volumes to containers:

podman run -dt \
  --name nginx-with-data \
  -v my-data-volume:/usr/share/nginx/html \
  nginx

Migration Tips from Docker

  1. Command Compatibility: Most Docker commands work identically in Podman (dockerpodman)
  2. Daemon Management: No need to manage Docker daemon - Podman runs containers directly
  3. Rootless Operation: Podman supports running containers without root privileges by default
  4. Kubernetes YAML Support: Podman can directly run Kubernetes pod manifests
  5. API Compatibility: Podman provides a Docker-compatible REST API through the podman system service command

Conclusion

Podman offers a powerful, Docker-compatible experience with added security and flexibility through its daemonless architecture. Mastering these essential snippets and advanced techniques will help you confidently manage containers and pods in your development or production environments.

The pod-centric architecture of Podman not only simplifies local development workflows but also bridges the gap between container development and Kubernetes orchestration. Its rootless capabilities and systemd integration make it particularly suitable for enterprise environments with strict security requirements.

Start experimenting with Podman today and leverage its advanced features to simplify container orchestration while improving security posture in your infrastructure!

Further Resources

Podman Podman snippets container management Podman pods Podman commands rootless containers container orchestration Podman tutorial Docker alternative container lifecycle