且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在构建阶段将环境变量从 docker-compose 传递到容器

更新时间:2022-05-31 10:12:12

您的 docker-compose.yml 应如下所示:

Your docker-compose.yml should look like this:

version: '2'
services:
  web:
    build:
      context: ./web
      args:
        REQUIREMENTS: "requirements_dev.txt"

你的 Dockerfile 应该像这样使用 ARG 定义构建参数:

Your Dockerfile should define the build-argument using ARG like this:

FROM python:3.5
ENV PYTHONUNBUFFERED 1
ENV APP_ROOT /usr/src/app
ARG REQUIREMENTS
...
COPY $REQUIREMENTS $APP_ROOT/
RUN pip install -r $APP_ROOT/$REQUIREMENTS

我对此进行了验证并在 Github 创建了一个缩小的功能演示:

I validated this and created a minified functional demo at Github:

https://github.com/jannikweichert/***-41747843