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 theKubernetesPodOperatorrequirements.
- Note: For Kubernetes deployments, this string is safely parsed into a list using
**kwargs: Additional keyword arguments to pass down to the underlyingDockerOperatororKubernetesPodOperator.
Returns
BaseOperator: Returns either aDockerOperatoror aKubernetesPodOperatorinstance 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cstg_airflow_container_utils-0.2.0.tar.gz.
File metadata
- Download URL: cstg_airflow_container_utils-0.2.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.8.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e444240ce14a26945a32e9591baef87bbef22fc4701fd7cd692e1a5904eeb679
|
|
| MD5 |
4a5593521f401ea124aa134678dfa38f
|
|
| BLAKE2b-256 |
3a03e74d48306f18f02b032cdb09830ce89961ff59762c2e40c6afd0b4b9acf8
|
File details
Details for the file cstg_airflow_container_utils-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cstg_airflow_container_utils-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.8.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f55e7cd58fe968a9b3392882dce2295705299b490395665bf649e6ce73499d2
|
|
| MD5 |
5f1738e59bc6241b1389603d018e8fc4
|
|
| BLAKE2b-256 |
a3f172753063fceac41d535503e0b8aaa1eda320a8c6a9e04f042c9302d32375
|