title: Airflow Docker Operator remove container or start container in attached mode
body: <p>I have Airflow running in a container.</p>
  <p>I use the Docker Operator to create a number of container that run a script.</p>
  <p>the name for the container is dynamically created with some parameters which make it meaningful to me if I list the containers to see via terminal what's going on.</p>
  <p>For instance the container name is called my_script_daily_A</p>
  <p>if the script runs successfully the container is removed automatically.</p>
  <p>I have retries in case the task fails.
  There is a problem in case the script fails and airflow retries the run:</p>
  <p>In case of script failure the container would still exists in exited status, it is not removed. This makes its name already assigned and therefore when Airflow runs the task again it throws an error &quot;a container with this name already exists&quot;.</p>
  <p>To solve this I created a script that runs on the host every minute:</p>
  <pre><code>for i in $(docker ps -aq --filter status=exited); do
  echo 'Removing '; docker rm $i done
  </code></pre>
  <p>Im not very happy of this, I would rather trigger this script only if there is a failure, even better if I could detect whether the failure was due to the name conflict.</p>
  <p>I know you can set up actions on call back with &quot;on_failure_callback&quot; attribute.</p>
  <p>But running my script to remove the exited containers would only work if I was running Airflow directly on the host, not on the container.</p>
  <p>Any suggestions on what to do here?</p>