我现在有一个带有python映像的Docker容器,我在这里运行cron作业。我使用一个坞编写文件来运行它,在这里,我将令牌从主机(我的Macbook)作为环境变量传递给容器。
version: '3.8'
services:
backend:
container_name: py-cont
build: .
environment:
GOOGLE_ADS_TOKEN: ${GOOGLE_ADS_TOKEN}我想将cron作业迁移到AirFlow (使用这里中的停靠-组合文件运行),在这里我想使用DockerOperator,但是我不知道如何从主机传递环境变量,以实现我的对接者-组合所做的事情。
这是我的DAG,它在运行时将KeyError抛到AirFlow日志中,试图获取不存在的env ( var来源于主机,通过回显确认):
DockerOperator(
dag=dag,
task_id='refresh_tickers',
image='mypythonimage',
api_version='auto',
auto_remove=True,
environment={
'GOOGLE_ADS_TOKEN': os.environ['GOOGLE_ADS_TOKEN']
},
command='echo $GOOGLE_ADS_TOKEN',
docker_url='tcp://docker-proxy:2375',
network_mode='bridge',
)我刚开始接触AirFlow,我可能误解了这个例子中的主机是什么,它是在拼图中定义的许多容器中的一个,而不是我的macbook吗?这可能会让人感到困惑,因为DockerOperator的卷参数将我的本地(Macbook)文件路径安装到容器中,没有任何问题。
谢谢。
发布于 2022-08-08 19:45:07
第一步是检查主机中是否存在env变量:
echo $GOOGLE_ADS_TOKEN第二步是将env变量添加到所有调度程序和所有员工容器中,为此您需要更新docker-组合文件。
version: '3'
x-airflow-common:
&airflow-common
# In order to add custom dependencies or upgrade provider packages you can use your extended image.
# Comment the image line, place your Dockerfile in the directory where you placed the docker-compose.yaml
# and uncomment the "build" line below, Then run `docker-compose build` to build the images.
image: ${AIRFLOW_IMAGE_NAME:-apache/airflow:2.3.3}
# build: .
environment:
&airflow-common-env
AIRFLOW__CORE__EXECUTOR: CeleryExecutor
...
_PIP_ADDITIONAL_REQUIREMENTS: ${_PIP_ADDITIONAL_REQUIREMENTS:-}
GOOGLE_ADS_TOKEN: ${GOOGLE_ADS_TOKEN}最后一步是检查工作容器中是否存在env变量:
docker-compose exec airflow-worker bash -c 'echo "$GOOGLE_ADS_TOKEN"'https://stackoverflow.com/questions/73280926
复制相似问题