Apache Airflow Docker Provider

4.5.4 · active · verified Thu Apr 09

The Apache Airflow Docker Provider package enables Airflow to interact with Docker, allowing users to execute tasks within Docker containers. This provider is actively maintained and frequently updated, with the current version being 4.5.4. All core functionalities are encapsulated within the `airflow.providers.docker` Python package.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Airflow DAG using the `DockerOperator` to execute a simple Python command within a Docker container. It uses environment variables for Docker host URL and explicitly handles container removal and temporary directory mounting, common considerations for Docker environments.

import os
from datetime import datetime

from airflow.models.dag import DAG
from airflow.providers.docker.operators.docker import DockerOperator

with DAG(
    dag_id='docker_operator_quickstart',
    start_date=datetime(2023, 1, 1),
    schedule_interval=None,
    catchup=False,
    tags=['docker', 'example'],
) as dag:
    run_docker_task = DockerOperator(
        task_id='run_hello_world_in_docker',
        image='python:3.10-slim-buster',
        command='python -c "print(\'Hello from Docker container in Airflow!\')"',
        auto_remove='force', # Automatically remove the container on exit
        mount_tmp_dir=False, # Set to True or False depending on local/remote Docker setup
        docker_url=os.environ.get('DOCKER_HOST', 'unix://var/run/docker.sock'), # Use DOCKER_HOST env var or default
    )

view raw JSON →