簡體   English   中英

將環境變量從主機傳遞到 Airflow 中的 DockerOperator

[英]Pass environment variable from host to DockerOperator in Airflow

我目前有一個 Docker 容器和一個 python 映像,我在其中運行 cronjobs。 我使用 docker 組合文件來運行它,我將令牌從主機(我的 Macbook)作為環境變量傳遞給容器。

version: '3.8'
services:
  backend:
    container_name: py-cont
    build: .
    environment:
      GOOGLE_ADS_TOKEN: ${GOOGLE_ADS_TOKEN}

我想將 cronjobs 遷移到 AirFlow (使用此處的 docker-compose 文件運行)我想使用 DockerOperator,但我不知道如何從主機傳遞環境變量,以實現我的 ZBAEDB53E8045AE7AE993 所做的。

這是我的 DAG,它在運行時在 AirFlow 日志中引發 KeyError,試圖獲取不存在的 env var(該 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 的新手,可能我誤解了這種情況下的主機是什么,它是 docker-compose 中定義的眾多容器之一,而不是我的 macbook? 這會令人困惑,因為 DockerOperator 的 volumes 參數將我的本地(Macbook)文件路徑安裝到容器中沒有問題。

謝謝。

第一步是檢查主機中是否存在環境變量:

echo $GOOGLE_ADS_TOKEN

第二步是將環境變量添加到所有調度程序和所有工作容器中,為此您需要更新 docker-compose 文件

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"'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM