Prefect Docker

0.7.1 · active · verified Sun Apr 12

The `prefect-docker` library provides Prefect integrations for orchestrating flow runs within Docker containers. It includes infrastructure blocks for defining Docker execution environments and workers for managing Docker-based work pools. This library is specifically designed for Prefect 2.x and later. The current version is 0.7.1, with releases typically aligning with new features or compatibility updates for the core Prefect library.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Prefect flow and configure it to run within a Docker container using the `DockerContainer` infrastructure block. It includes a runnable example that executes the flow locally using the specified Docker image. For full deployment, the infrastructure block would typically be saved and referenced in a Prefect deployment.

from prefect import flow
from prefect_docker.infrastructure import DockerContainer

@flow(log_prints=True)
def hello_docker_flow(name: str = "World"):
    """A simple flow that prints a greeting."""
    print(f"Hello, {name} from a Docker container!")

if __name__ == "__main__":
    # Define a DockerContainer infrastructure block.
    # This block specifies how your flow runs will be executed in Docker.
    docker_infra = DockerContainer(
        image="python:3.10-slim-buster", # Use a small, readily available image
        # You can also specify other Docker options like network, volumes, env, etc.
    )

    # For a quick local test without full Prefect deployment,
    # you can directly apply the infrastructure to the flow.
    # Ensure your Docker daemon is running.
    print("Running flow locally using DockerContainer infrastructure...")
    hello_docker_flow.with_options(infrastructure=docker_infra)("TestUser")
    print("Flow run completed.")

    # For full Prefect deployment, you would typically save the block:
    # import asyncio
    # asyncio.run(docker_infra.save("my-docker-infra-block", overwrite=True))
    # Then deploy via CLI: `prefect deploy --name "my-flow" --infra-block docker-container/my-docker-infra-block`

view raw JSON →