Podman Python Client

5.8.0 · active · verified Wed Apr 15

Podman-py provides Python bindings for the Podman RESTful API, enabling programmatic interaction with Podman containers, images, networks, and pods. As of version 5.8.0, it offers a comprehensive client for managing Podman resources. The project is actively maintained with frequent minor releases, typically on a monthly or bi-monthly cadence, introducing new features and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `PodmanClient` to connect to a running Podman daemon. By default, it tries to auto-detect the socket. It then prints system information and lists any running containers. This example is designed to work with both rootful and rootless Podman installations, assuming the default socket paths.

import podman
import os

# By default, PodmanClient attempts to connect to the Podman socket based on environment variables
# (e.g., PODMAN_HOST) or standard locations (e.g., /run/user/$UID/podman/podman.sock).
# You can also explicitly specify a base_url, e.g., for a remote Podman service or specific socket.

# Example using default connection (rootless or rootful Podman socket):
try:
    client = podman.PodmanClient()
    info = client.system.info()
    print(f"Connected to Podman. Version: {info['Version']}")
    
    # Example: List containers
    print("\nRunning containers:")
    for container in client.containers.list():
        print(f"  - {container.name} ({container.id[:12]})")

except Exception as e:
    print(f"Error connecting to Podman or executing command: {e}")
    print("Ensure Podman is running and accessible (e.g., 'systemctl --user start podman.socket' for rootless).")

# Example with explicit base_url (uncomment to test specific connections)
# rootless_socket = os.environ.get('XDG_RUNTIME_DIR', '/run/user/1000') + '/podman/podman.sock'
# try:
#     client_socket = podman.PodmanClient(base_url=f'unix://{rootless_socket}')
#     info_socket = client_socket.system.info()
#     print(f"\nConnected via explicit socket. Version: {info_socket['Version']}")
# except Exception as e:
#     print(f"Error connecting via explicit socket: {e}")

view raw JSON →