Skip to main content

get_container_operator

A factory function for Apache Airflow that abstracts containerized task execution. It dynamically returns either a DockerOperator (for local development) or a KubernetesPodOperator (for production/cluster environments) based on the active environment variable.

Description

When developing Airflow DAGs locally, it is often easier and faster to run tasks in local Docker containers rather than provisioning a full Kubernetes cluster. This function bridges the gap between local development and production deployments by providing a unified interface.

It reads the AIRFLOW_ENV environment variable to determine the execution context and provisions the appropriate operator with sensible defaults.

Parameters

  • task_id (str): The unique identifier for the Airflow task.
  • image (str): The Docker image to execute (e.g., my-repo/my-image:latest).
  • environment (dict): A dictionary of environment variables to inject into the container at runtime.
  • command (str): The command to execute inside the container.
    • Note: For Kubernetes deployments, this string is safely parsed into a list using shlex.split() to comply with the KubernetesPodOperator requirements.
  • **kwargs: Additional keyword arguments to pass down to the underlying DockerOperator or KubernetesPodOperator.

Returns

  • BaseOperator: Returns either a DockerOperator or a KubernetesPodOperator instance configured for the respective environment.

Environment Variables

Variable Default Description
AIRFLOW_ENV local Controls the execution mode. If set to dev, the function returns a DockerOperator. For any other value (including local or prod), it defaults to returning a KubernetesPodOperator.

Operator Configurations

Development Mode (AIRFLOW_ENV=dev)

Returns a DockerOperator configured to run on the local Docker daemon:

  • Connects to: unix://var/run/docker.sock
  • Network: airflow-docker_default
  • Cleans up the container automatically after success (auto_remove="success").

Production Mode (Default/Fallback)

Returns a KubernetesPodOperator configured for in-cluster execution:

  • Namespace: airflow
  • Service Account: airflow-worker
  • Image Pull Secrets: cstg-pullsecret
  • Automatically deletes the pod after execution (is_delete_operator_pod=True).
  • Fetches logs automatically (get_logs=True).

Usage Example

from datetime import datetime
from airflow import DAG
from cstg_airflow_container_utils.operators import get_container_operator

with DAG(
    dag_id="example_container_dag",
    start_date=datetime(2023, 1, 1),
    schedule_interval=None,
    catchup=False
) as dag:

    run_data_job = get_container_operator(
        task_id="run_data_processing",
        image="[my-registry.com/data-processor:v1.2](https://my-registry.com/data-processor:v1.2)",
        environment={"LOG_LEVEL": "INFO", "API_KEY": "secret"},
        command="python main.py --process-all",
        # kwargs passed to the underlying operator:
        retries=2 
    )

Download files

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

Source Distribution

cstg_airflow_container_utils-0.2.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

cstg_airflow_container_utils-0.2.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file cstg_airflow_container_utils-0.2.1.tar.gz.

File metadata

File hashes

Hashes for cstg_airflow_container_utils-0.2.1.tar.gz
Algorithm Hash digest
SHA256 79934ff1c7e6f8f279dfaab467742b33dbe54668bb5e35cead03f3d2edd49665
MD5 9f82f1d9e849712645c1b7128fba7514
BLAKE2b-256 9804ffdaf891bf0b21623eeb35e8798b82ec9dae9221abc694ffc56a079c8fe3

See more details on using hashes here.

File details

Details for the file cstg_airflow_container_utils-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cstg_airflow_container_utils-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0f1eec0d8f568a07fc706ba4d4b08c4965aa1bca3454abe9f050800d53af5855
MD5 6dd2979d9df85c9a7ae152a5e062b6b6
BLAKE2b-256 709a766bfd9993c4a4c9c16d6c1fac2b464df57688a34e2d605de7a65bd46f9b

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page