简体   繁体   English

在 docker-compose 而不是 dockerfile 中附加 python 路径环境变量

[英]Append python path environment variable in docker-compose instead of dockerfile

I use the AWS Lambda docker image to develop and do some test on my local host or during CI/CD.我使用 AWS Lambda docker 映像在本地主机上或 CI/CD 期间进行开发和测试。 On my Docker file I added ENV PYTHONPATH "${PYTHONPATH}:/var/task" to bind /var/task where my python libraries are installed.在我的 Docker 文件中,我添加了ENV PYTHONPATH "${PYTHONPATH}:/var/task"以绑定/var/task我的 python 库的安装位置。

I would to do the same but without add ENV PYTHONPATH "${PYTHONPATH}:/var/task" in my Dockerfile.我也会这样做,但不在我的 Dockerfile 中添加ENV PYTHONPATH "${PYTHONPATH}:/var/task"

I tried to add this line in my docker-compose but my python path wasn't updated.我试图在我的 docker-compose 中添加这一行,但我的 python 路径没有更新。

    environment:
      - PYTHONPATH="${PYTHONPATH}:/var/task"

What did I do wrong?我做错了什么?

Solution 1 - You can do with .env file解决方案 1 - 您可以使用.env 文件

Example:例子:

  $ cat .env
    TAG=v1.5
  $ cat docker-compose.yml
    version: '3'
    services:
      web:
        image: "webapp:${TAG}"

Note 1: Starting with +v1.28, .env file is placed at the base of the project directory.注 1:从 +v1.28 开始,.env 文件放在项目目录的基础上。

Note 2: Ideally .env file will be in the same dir where docker-compose at.注意 2:理想情况下.env文件将位于 docker docker-compose所在的同一目录中。 But you can customize as per you need.但是您可以根据需要进行自定义。

Testing before creating a Image - You can verify this with the config command ,在创建映像之前进行测试 - 您可以使用config 命令验证这一点,

$ docker-compose config $ docker-compose 配置

version: '3'
services:
  web:
    image: "webapp:v1.5"

Solution 2 - ARG directive in your Dockerfile解决方案 2 - Dockerfile 中的ARG 指令

Follow https://stackoverflow.com/a/34600106/3098330关注https://stackoverflow.com/a/34600106/3098330

If you use ${VARIABLE} syntax, Compose will do variable substitution.如果您使用${VARIABLE}语法,Compose 将执行变量替换。

To prevent Compose interpolation, you can use the $$ to escape $为防止 Compose 插值,可以使用$$转义$

    environment:
      - PYTHONPATH="$${PYTHONPATH}:/var/task"

see: https://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution见: https ://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM