简体   繁体   English

如何使用气流的 DockerOperator 将参数传递给 docker 容器

[英]How to pass an argument to a docker container using airflow's DockerOperator

I have a docker image that runs a python script.我有一个运行 python 脚本的 docker 映像。

The Dockerfile looks like: Dockerfile 看起来像:

FROM python:3.6
ADD . /


RUN pip install -r requirements.txt
ENTRYPOINT ["python3.6", "./main.py"]

As I understand it, using ENTRYPOINT instead of CMD to run the python script allows parameters to be passed to main.py that can be parsed with argparse within main.py据我了解,使用 ENTRYPOINT 而不是 CMD 来运行 python 脚本允许将参数传递给 main.py 可以在 main.py 中使用 argparse 解析

I have succesfully passed in arguments and retrieved and used them in main.py when running the docker container (named param-test) with the following command:在使用以下命令运行 docker 容器(名为 param-test)时,我已成功通过 arguments 并在 main.py 中检索并使用它们:

docker run -it param-test "cats"

I now wish to do the equivalent of the above 'docker run' command via Apache Airflow.我现在希望通过 Apache Airflow 执行与上述“docker run”命令等效的操作。

I have the following code that creates a DockerOperator within airflow succesfully but i am unclear how to pass the argument in this code configuration like i do in the above docker run command:我有以下代码在 airflow 中成功创建 DockerOperator,但我不清楚如何在此代码配置中传递参数,就像我在上面的 docker 运行命令中所做的那样:

DockerOperator(dag=dag,
            task_id='my_task_name',
            image='my_docker_container_ecr_url'
)

How do i pass the param "cats" to the DockerOperator in an equivalent way to running:我如何以与运行等效的方式将参数“cats”传递给 DockerOperator:

docker run -it param-test "cats"

so that my python script cat use the cats param?以便我的 python 脚本猫使用猫参数?

EDIT: python code in main.py that uses the cats param passed in from docker:编辑: main.py 中的 python 代码使用从 docker 传入的 cat 参数:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('city')
args = parser.parse_args()
print(args.cat_var)

Below should work下面应该工作

DockerOperator(dag=dag,
            task_id='my_task_name',
            image='my_docker_container_ecr_url',
            command='cats'
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Airflow 2.0 中的 DockerOperator 从 GitLab 容器注册表中提取私有 docker 映像? - How to pull private docker image from GitLab container registry using DockerOperator in Airflow 2.0? 如何使用 airflow DockerOperator 提取我自己的私有存储库的 docker 映像? - How to pull my own private repository's docker image using airflow DockerOperator? 在 Airflow 2.0 中使用 DockerOperator 从 GitLab 容器注册表中拉取私有 docker 图像时出错 - Error when pulling private docker image from GitLab container registry using DockerOperator in Airflow 2.0 Airflow DockerOperator 如何在 Docker 映像创建错误时发送通知 - How Airflow DockerOperator send notification when Docker image creation error 如何将来自 AWS ECR 的私有镜像与 Airflow 的 DockerOperator 一起使用? - How to use a private image from AWS ECR with Airflow's DockerOperator? Airflow Docker-in-Docker 设置的 DockerOperator 解决方法 - Airflow DockerOperator workaround for Docker-in-Docker Setup Apache Airflow DockerOperator 未找到 docker 模块 - Apache Airflow DockerOperator not finding docker module 使用 Airflow DockerOperator 时出现无效参数错误 - Invalid arguments Error when using Airflow DockerOperator 如何从 Apache Airflow 使用 DockerOperator - How to use the DockerOperator from Apache Airflow Airflow 在 Docker 中运行其他容器的作业 - Airflow Running Other Container's Job in Docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM