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
)
Metadata
Release files for cstg-airflow-container-utils 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cstg_airflow_container_utils-0.2.1.tar.gz | 3.9 kB | Without provenance | Details for cstg_airflow_container_utils-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cstg_airflow_container_utils-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for cstg_airflow_container_utils-0.2.1-py3-none-any.whl |
Total release size: 9.1 kB
Release files / cstg_airflow_container_utils-0.2.1.tar.gz
| Download URL | cstg_airflow_container_utils-0.2.1.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
79934ff1c7e6f8f279dfaab467742b33dbe54668bb5e35cead03f3d2edd49665
|
|
BLAKE2b-256 checksum How to use checksums |
9804ffdaf891bf0b21623eeb35e8798b82ec9dae9221abc694ffc56a079c8fe3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.16
|
Release files / cstg_airflow_container_utils-0.2.1-py3-none-any.whl
| Download URL | cstg_airflow_container_utils-0.2.1-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0f1eec0d8f568a07fc706ba4d4b08c4965aa1bca3454abe9f050800d53af5855
|
|
BLAKE2b-256 checksum How to use checksums |
709a766bfd9993c4a4c9c16d6c1fac2b464df57688a34e2d605de7a65bd46f9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.16
|
Security for cstg-airflow-container-utils 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.