【问题标题】:How to pass variable from Makefile to Python script through docker-compose如何通过 docker-compose 将 Makefile 中的变量传递给 Python 脚本
【发布时间】:2021-02-02 16:48:28
【问题描述】:

我有一个运行 docker-compose 的 Makefile,它有一个执行 python 脚本的容器。我希望能够将命令行中的变量传递给 Makefile 并在 python 脚本 (testing.py) 中打印它。

我的目录如下:

main_folder:
  -docker-compose.yaml
  -Makefile
  -testing.py

我尝试了以下配置。 Makefile 是:

.PHONY: run run-prod stop stop-prod rm

run:
    WORKING_DAG=$(working_dag) docker-compose -f docker-compose.yml up -d --remove-orphans --build --force-recreate

docker-compose 是:

version: "3.7"
services:
  prepare_files:
    image: apache/airflow:1.10.14
    environment:
      WORKING_DAG: ${working_dag}
      PYTHONUNBUFFERED: 1
    entrypoint: /bin/bash
    command: -c "python3 testing.py $$WORKING_DAG"

而文件 testing.py 是:

import sys

print(sys.argv[0], flush=True)

当我在命令行中运行时:

 make working_dag=testing run

它不会失败,但它也不会打印任何东西。我怎么能做到?谢谢

【问题讨论】:

    标签: python docker variables makefile docker-compose


    【解决方案1】:

    我相信变量WORKING_DAG 已通过命令行正确分配,Makefile 将其正确传递给docker-compose。我通过运行容器不被破坏来验证它,然后登录到容器后,我检查了WORKING_DAG的值:

    为了在docker执行完成后不销毁容器,我修改了docker-compose.yml,如下:

    version: "3.7"
    services:
      prepare_files:
        image: apache/airflow:1.10.14
        environment:
          WORKING_DAG: ${working_dag}
          PYTHONUNBUFFERED: 1
        entrypoint: /bin/bash
        command: -c "python3 testing.py $$WORKING_DAG"
        command: -c "tail -f /dev/null"
    

    airflow@d8dcb07c926a:/opt/airflow$ echo $WORKING_DAG
    testing
    

    使用docker-compose部署时docker不显示Python的std.out的问题已经在Github评论here,但仍未解决。在使用docker-compose 时使其工作,只有当我们将文件传输/挂载到容器中,或者我们使用Dockerfile 时才有可能。

    当使用Dockerfile时,只需要运行相应的脚本如下,

    CMD ["python", "-u", "testing.py", "$WORKING_DAG"]
    

    要将脚本挂载到容器中,请查看@DazWilkin的答案here

    【讨论】:

      【解决方案2】:

      您需要将testing.py 挂载到容器中(使用volumes)。下面使用你当前的工作目录(${PWD}),并将testing.py挂载在容器的根目录中:

      version: "3.7"
      services:
        prepare_files:
          image: apache/airflow:1.10.14
          volumes:
            - ${PWD}/testing.py:/testing.py
          environment:
            PYTHONUNBUFFERED: 1
          entrypoint: /bin/bash
          command: -c "python3 /testing.py ${WORKING_DAG}"
      

      注意 无需在服务定义中包含 WORKING_DAG,因为它已通过 Makefile 暴露给 Docker Compose 环境。像你一样设置它,用""(空字符串)覆盖它,因为${working_dag}是你的原始环境变量,但是你在你的Makefile中将它重新映射到WORKING_DAGrun步骤。

      import sys
      
      print(sys.argv[0:], flush=True)
      

      然后:

      make --always-make working_dag=Freddie run
      WORKING_DAG=Freddie docker-compose --file=./docker-compose.yaml up
      Recreating 66014039_prepare_files_1 ... done
      Attaching to 66014039_prepare_files_1
      prepare_files_1  | ['/testing.py', 'Freddie']
      66014039_prepare_files_1 exited with code 0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-29
        • 2021-11-27
        • 1970-01-01
        • 2019-07-16
        • 2020-11-07
        • 2021-04-09
        • 2018-08-23
        • 1970-01-01
        相关资源
        最近更新 更多