apache/airflow

DockerOperator: on_kill method does not respect auto_remove='force', leaving orphaned containers

Open

#56149 opened on Sep 26, 2025

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Python (44,809 stars) (16,781 forks)batch import
area:providersgood first issuekind:bugprovider:docker

Description

Apache Airflow version

3.1.0

If "Other Airflow 2 version" selected, which one?

No response

What happened?

When a DockerOperator task with the auto_remove='force' parameter is killed via the Airflow UI (e.g., by selecting "Mark as Failed"), the on_kill method is triggered.

Expected behavior: The Docker container should be stopped and removed, as specified by auto_remove='force'.

Actual behavior: The on_kill method correctly calls self.cli.stop(), which stops the container (it exits with code 137). However, the container is not removed. It remains in a stopped state (Exited (137) ...).

This leaves an orphaned container on the Docker host. If a subsequent DAG run attempts to create a container with the same name, it fails with a 409 Conflict error.

What you think should happen instead?

No response

How to reproduce

  1. Create the following DAG:

    import pendulum
    from airflow.models.dag import DAG
    from airflow.providers.docker.operators.docker import DockerOperator
    
    with DAG(
        dag_id='docker_on_kill_bug_repro',
        start_date=pendulum.datetime(2025, 9, 1, tz="UTC"),
        schedule=None
    ) as dag:
        run_container = DockerOperator(
            task_id='long_running_container',
            image='alpine:latest',
            command=["sleep", "300"],
            container_name='test-on-kill-container',
            docker_url="unix://var/run/docker.sock",
            auto_remove='force',  # This parameter is not respected on_kill
            tty=True,
        )
    
  2. Run the DAG.

  3. Wait for the "long_running_container" task to enter the running state.

  4. In the Airflow UI, click on the running task and select "Clear", then check the box "Mark as Failed".

  5. On the Docker host, run "docker ps -a".

Observation: You will see the container "test-on-kill-container" with the status "Exited (137) ...". It has not been removed.

Operating System

Debian GNU/Linux 12 (bookworm)

Versions of Apache Airflow Providers

apache-airflow-providers-docker==4.4.2

Deployment

Docker-Compose

Deployment details

No response

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Contributor guide