airflowairflow-2.x

Airflow DockerOperator Dynamic docker_url passing


Airflow Docker operator Docker url dynamic config.

we want to pass the run time conf to docker_url.

docker operator code

end = DockerOperator(
task_id='docker_command_sleep',
container_name="{{dag_run.conf.get('ip', '') if dag_run}}",
image='nginx',
api_version='auto',
auto_remove=True,
docker_url="{{dag_run.conf.get('ip', '') if dag_run}}"

run_config

{"ip":"tcp://1.1.1.1:2375"}

error we are getting docker.errors.DockerException: Invalid bind address format: port is required:

{"ip":"tcp://1.1.1.1:2375"}

Solution

  • The docker_url parameter is not a templated field for the DockerOperator. Only templated fields can accept a Jinja expression (you can learn more here).

    However, you can subclass the DockerOperator, add docker_url as a templated field, and then use the subclassed version of the operator in your DAG(s).

    from airflow.providers.docker.operators.docker import DockerOperator
    
    
    class MyDockerOperator(DockerOperator):
        template_fields = (*DockerOperator.template_fields, "docker_url")