Airflow Docker Operator returning sucess when python code inside fail

Viewed 503

I'm a pretty new dev in the docker world and at this point I really need help.

I have a python script inside a docker container. When python exits with sys.exit(-1) the docker container still exits with success and consequently mark success in airflow too, omitting real errors and trace-backs.

How can I exit from container with error ?

2 Answers

I know this question is old, but I just figured out the answer myself.

First, remove any old images that airflow might be using. List them with docker images, and then remove with docker rmi <image_id>

Now, in your code add a sys.exit(1) in a try/except for the main part of the code that is getting called by the DAG.

First, set remove=True to your docker container, so it's automatically removed when it's finished running. Second, use python's try:except: in both your python code and your DAG to catch the exceptions. And last - raise AirflowException in the DAG, so Airflow can detect the issue and mark the task as failed.

  1. In your DAG:

     from airflow.exceptions import AirflowException
    
     try:
         client = docker.from_env()
         response = client.containers.run(image, command, remove=True, ...)
         ...
     except Exception as e:
         raise AirflowException(e)
    
  2. In your python code:

     import logging
    
     try:
         ...
     except Exception as e:
         logging.exception(e)
    
Related