问题描述
我正在尝试使用单个 Dockerfile
、多个 docker-compose
文件和多个 envoronment_variables
文件为多个环境创建配置.我需要它为每个环境使用具有 python 依赖项的不同文件.
I am trying to create configuration for several environments with a single Dockerfile
, several docker-compose
files and several envoronment_variables
files. I need it to use a different file with python dependencies for each environment.
比方说,我们在两个环境中创建了一个 web
服务:development
和 production
.为此,我创建了以下文件结构:
Let's say, we create a web
service in two environments: development
and production
. To achieve this, I create the following file structure:
docker-compose-dev.yml
docker-compose-prod.yml
envs/
dev.env
prod.env
web/
Dockerfile
requirements_dev.txt
requirements_prod.txt
目标是在容器的build
过程中实例化Dockerfile
中requirements_*.txt
文件的正确名称.根据文档,我尝试了这种幼稚的方法,但似乎不起作用:
The objective is to instantiate the proper name of a requirements_*.txt
file in the Dockerfile
during the container's build
process. According to the documentation, I tried this naïve approach, which doesn't seem to work:
- 将名称定义为环境变量:
- envs/dev.env:
REQUIREMENTS=requirements_dev.txt
- envs/prod.env:
REQUIREMENTS=requirements_prod.txt
- envs/dev.env:
在 Dockerfile 中使用这个环境变量:
Use this environment variable in the Dockerfile:
FROM python:3.5
ENV PYTHONUNBUFFERED 1
ENV APP_ROOT /usr/src/app
...
COPY $REQUIREMENTS $APP_ROOT/
RUN pip install -r $APP_ROOT/$REQUIREMENTS
在docker-compose
配置中导入对应的定义:
Import the corresponding definition in a docker-compose
configuration:
version: '2'
services:
web:
build: ./web
env_file:
- envs/dev.env # in docker-compose-dev.yml
- envs/prod.env # in docker-compose-prod.yml
...
当我运行 docker-compose up --build docker-compose-dev.yml
时,$REQUIREMENTS
变量未在构建过程中定义.
When I run docker-compose up --build docker-compose-dev.yml
, the $REQUIREMENTS
variable is not defined at the build process.
在 docker-compose
文档中 有一个注释:
注意:如果您的服务指定了构建选项,则在构建过程中环境文件中定义的变量将不会自动可见.使用 build 的 args 子选项来定义构建时环境变量.
但是要使用Dockerfile
中的ARG
选项,比如ARG REQUIREMENTS
,需要一个对应的args
docker-compose
中的选项,像这样:
But to use the ARG
option in the Dockerfile
, like ARG REQUIREMENTS
, one needs a corresponding args
option in docker-compose
, like this:
services:
web:
build:
context: ./web
args:
- REQUIREMENTS
尽管如此,使用此配置我的测试也失败了,因为根据 文档,Dockerfile中REQUIREMENTS
变量的值取自
Nevertheless, with this configuration my tests also failed, because, according to the documentation, the value of the REQUIREMENTS
variable in Dockerfile is taken from
Compose 运行的环境
因此,它从我的 shell 中获取值,而不是从 envs/dev.env
中获取值.
Therefore, it takes value from my shell, but not from envs/dev.env
.
所以我的问题是:
是否可以将环境变量从docker-compose
中的env_file
传递到Dockerfile
构建时间没有在我的 shell 中本地设置变量?
推荐答案
您的 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/stackoverflow-41747843
这篇关于在构建阶段将环境变量从 docker-compose 传递到容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!