Skip to main content

A Pythonic client for the Docker/Podman Engine API.

Project description

Bollard

A Pythonic, zero-dependency client for the Docker and Podman Engine APIs. Prioritizes descriptive naming, context managers, and cross-platform ease of use.

Installation

pip install bollard

Project Structure

For a detailed overview of the project's architecture, see ARCHITECTURE.md.

Key Features

  • Pythonic API: list_containers instead of ps; remove_image instead of rmi.
  • Zero Dependencies: Uses only the Python standard library (http.client, socket, json).
  • Smart Connection: Auto-detects Docker/Podman sockets (Unix, Windows Pipes, DOCKER_HOST).
  • Windows Friendly: Auto-starts the Podman machine on Windows if connection fails.
  • Resource Safety: Context managers for client connections and ephemeral containers.
  • Streaming Output: Real-time progress updates for long-running operations like pull and build.
  • .dockerignore Support: Respects .dockerignore files when building images.
  • Full Lifecycle: Manage Containers, Images, Networks, and Volumes.

Usage

Basic Connection

from bollard import DockerClient

with DockerClient() as client:
    for image in client.list_images():
        print(f"Image: {image.tags[0]}")

Managing Containers

with DockerClient() as client:
    # Run a container and get logs
    container = client.run_container("alpine:latest", command="echo 'Hello World'")
    print(container.logs())

    # Stop and remove
    container.stop()
    container.remove(force=True)

Ephemeral Containers (Auto-Cleanup)

Use ephemeral_container to automatically remove the container after the block exits, even if errors occur.

with DockerClient() as client:
    with client.ephemeral_container("alpine", command="sleep 60") as container:
        container.exec(["echo", "Running inside container"])
    # Container is automatically removed

Streaming Image Operations

Methods pull_image, build_image, and push_image, when called with progress=True, return a generator that yields progress updates.

with DockerClient() as client:
    # Pull an image with progress
    for progress in client.pull_image("alpine:latest", progress=True):
        if "status" in progress:
            print(f"{progress['status']} {progress.get('progress', '')}")

    # Build from directory
    for log in client.build_image(".", "my-app:latest", progress=True):
        if "stream" in log:
            print(log["stream"], end="")

Managing Networks & Volumes

Create and manage Docker networks and volumes using Resource objects.

with DockerClient() as client:
    # Networks
    net = client.create_network("my-net", driver="bridge")
    print(net.id)
    net.remove()

    # Volumes
    vol = client.create_volume("my-data")
    print(vol.name)
    vol.remove()

File Operations

Copy files and directories in and out of containers directly from the Container object.

with DockerClient() as client:
    with client.ephemeral_container("alpine:latest", command="sleep 60") as container:
        # Copy host -> container
        container.copy_to("local_data/", "/dest/path/")

        # Copy container -> host
        container.copy_from("/src/path/data.txt", "local_output/")

Real-world Example: Stirling-PDF Conversion

This example demonstrates running a Stirling-PDF container to convert a "Hello World" HTML file into a PDF and then retrieving it.

from bollard import DockerClient
from time import sleep

env = {"SECURITY_ENABLE_LOGIN": "false"}

with DockerClient().container("frooodle/s-pdf", environment=env) as container:
        # Wait for the container to be ready
        res = ""
        while "HTTP" not in res:
            res = container.exec("curl -s -I http://localhost:8080")
            sleep(1)

        # Convert the HTML to PDF
        container.exec("sh -c 'echo \"<h1>Test PDF</h1>\" > /test.html'")
        result = container.exec(
            'curl -s -w "%{http_code}" '
            "-F 'fileInput=@/test.html' "
            "http://localhost:8080/api/v1/convert/html/pdf "
            "-o /test.pdf"
        )

        # Copy the PDF to the host
        container.copy_from("/test.pdf", ".")

Kubernetes YAML Support

Execute Kubernetes YAML files directly using Podman's native play kube feature.

with DockerClient() as client:
    # Requires a valid Kubernetes YAML file (Pod, Deployment, etc.)
    result = client.play_kube("pod.yaml")

    # Returns the JSON response from Podman describing created resources
    print("Created Pods:", result.get("Pods"))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bollard-0.1.1.tar.gz (39.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bollard-0.1.1-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file bollard-0.1.1.tar.gz.

File metadata

  • Download URL: bollard-0.1.1.tar.gz
  • Upload date:
  • Size: 39.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for bollard-0.1.1.tar.gz
Algorithm Hash digest
SHA256 859fb1ff1f733ece7507c0844d782a8ad100514b1fcd2d7e57a51a30a45e68af
MD5 0a85285b395f0da2fad8694ca697adf1
BLAKE2b-256 5ca0a1ee72b66ae27bd286134080c56824317aaa16fb4c16fac0d75f7e0d9742

See more details on using hashes here.

File details

Details for the file bollard-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: bollard-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for bollard-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d749351fc26980dc65199dbe0dc8c14c2965ac05bcbf23ac48203fec1a7f2f75
MD5 1d8451d2c9f0e812e1a0418bff4277d4
BLAKE2b-256 4896528124fa7d12ad43e893e7d1f8fd6adeed22139cc615c1c5c3ecf926fb73

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page