DockerOperator: on_kill method does not respect auto_remove='force', leaving orphaned containers
#56149 opened on Sep 26, 2025
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
-
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, ) -
Run the DAG.
-
Wait for the "long_running_container" task to enter the running state.
-
In the Airflow UI, click on the running task and select "Clear", then check the box "Mark as Failed".
-
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
- I agree to follow this project's Code of Conduct