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.0.tar.gz (3.7 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.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for cstg_airflow_container_utils-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e444240ce14a26945a32e9591baef87bbef22fc4701fd7cd692e1a5904eeb679
MD5 4a5593521f401ea124aa134678dfa38f
BLAKE2b-256 3a03e74d48306f18f02b032cdb09830ce89961ff59762c2e40c6afd0b4b9acf8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cstg_airflow_container_utils-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f55e7cd58fe968a9b3392882dce2295705299b490395665bf649e6ce73499d2
MD5 5f1738e59bc6241b1389603d018e8fc4
BLAKE2b-256 a3f172753063fceac41d535503e0b8aaa1eda320a8c6a9e04f042c9302d32375

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.1

2 files

This release

0.2.0 This release

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