Podman: Advanced Concepts

Pods in Podman

Understanding Pods

  • What is a Pod? A way to run multiple containers together with shared resources and networking.
  • Podman Pods vs. Kubernetes Pods – Podman pods mimic Kubernetes behavior but don’t require a full cluster.
  • Use Cases – Useful for running multi-container applications (e.g., a web server and database).

Creating and Managing Pods

  • Create a Pod:
    podman pod create --name mypod -p 8080:80
  • Add a container to the Pod:
    podman run -dt --pod mypod nginx
  • View Pod details:
    podman pod ps
    podman pod inspect mypod
  • Stop and remove a Pod:
    podman pod stop mypod
    podman pod rm mypod

Managing Container Networks

Container Networking Basics

  • Network Modes:
    • Bridge (default): Containers get unique IPs and can communicate.
    • Host: Uses the host’s network, offering better performance but less isolation.
    • None: No networking.

Creating Custom Networks

  • List existing networks:
    podman network ls
  • Create a custom network:
    podman network create mynetwork
  • Run a container in a custom network:
    podman run -dt --network mynetwork nginx
  • Inspect network details:
    podman network inspect mynetwork

Connecting Containers

  • Expose a container on a specific port:
    podman run -dt -p 8080:80 nginx
  • Connect two containers to the same network:
    podman network connect mynetwork container1
    podman network connect mynetwork container2

Volumes and Persistent Storage

Understanding Storage in Podman

  • Bind Mounts vs. Volumes:
    • Bind Mounts: Directly link to host paths.
    • Volumes: Managed by Podman, offering better portability.
  • When to Use Persistent Storage? When data needs to outlive the container.

Managing Volumes

  • Create a volume:
    podman volume create myvolume
  • Mount a volume in a container:
    podman run -dt -v myvolume:/data nginx
  • List and inspect volumes:
    podman volume ls
    podman volume inspect myvolume
  • Remove a volume:
    podman volume rm myvolume

Using Bind Mounts

  • Mount a host directory in a container:
    podman run -dt -v /host/data:/container/data nginx

Container Health Checks and Management

Understanding Health Checks

  • Why? Ensures containers stay functional and responsive.
  • How? Define a health check command that regularly verifies container status.

Implementing Health Checks

  • Run a container with a health check:
    podman run -d --name mywebserver \
      --health-cmd "curl -f http://localhost || exit 1" \
      --health-interval 30s nginx
  • Check container health status:
    podman inspect --format='{{.State.Healthcheck}}' mywebserver

Managing Container Resources

  • Limit CPU and memory usage:
    podman run -dt --memory=512m --cpus=1 nginx
  • Monitor container performance:
    podman stats

Hands-on Exercises

1. Create and Manage a Pod with Multiple Containers

podman pod create --name myapp -p 8080:80
podman run -dt --pod myapp nginx
podman run -dt --pod myapp redis
podman pod ps

2. Set Up a Custom Network and Connect Containers

podman network create mynetwork
podman run -dt --network mynetwork --name webserver nginx
podman run -dt --network mynetwork --name database redis
podman network inspect mynetwork

3. Implement Health Checks for a Web Server

podman run -d --name healthcheck-nginx \
  --health-cmd "curl -f http://localhost || exit 1" \
  --health-interval 30s \
  nginx
podman inspect --format='{{.State.Healthcheck}}' healthcheck-nginx

Summary & Next Steps

Key Takeaways

  • Pods allow multiple containers to share resources and networking.
  • Custom networks improve container communication and security.
  • Persistent storage ensures data survives beyond container lifetimes.
  • Health checks keep applications running smoothly.

Next Module: Building and Managing Images

Up next: We’ll cover building container images, managing registries, and securing your images.