Dockerfile:在单行中设置多个环境变量 [英] Dockerfile: Setting multiple environment variables in single line
问题描述
我的印象是可以将环境变量设置在一行中,以尽量减少中间图像.
I was under the impression that environmental variables could be set on a single line as follows so as to minimize intermediary images.
FROM alpine:3.6
ENV RUBY_MAJOR 2.4
RUBY_VERSION 2.4.1
RUBY_DOWNLOAD_SHA256 4fc8a9992de3e90191de369270ea4b6c1b171b7941743614cc50822ddc1fe654
RUBYGEMS_VERSION 2.6.12
BUNDLER_VERSION 1.15.3
但是,基于此代码段运行容器并调用 # set |grep RU
我看到变量没有单独分配,而是组合成一个字符串.
However, running a container based off of this snippet and calling # set |grep RU
I see that the variables are not being assigned separately, but are combined into a single string.
RUBY_MAJOR='2.4 RUBY_VERSION 2.4.1 RUBY_DOWNLOAD_SHA256 4fc8a9992de3e90191de369270ea4b6c1b171b7941743614cc50822ddc1fe654 RUBYGEMS_VERSION 2.6.12 BUNDLER_VERSION 1.15.3'
但是,如果我如下明确设置每个变量,我会得到预期的输出,并且在调用变量时没有错误.
However, if I explicitly set each variable as below, I get the expected output and there are no errors when calling the variables.
ENV RUBY_MAJOR 2.4
ENV RUBY_VERSION 2.4.1
ENV RUBY_DOWNLOAD_SHA256 4fc8a9992de3e90191de369270ea4b6c1b171b7941743614cc50822ddc1fe654
ENV RUBYGEMS_VERSION 2.6.12
ENV BUNDLER_VERSION 1.15.3
问题:是否可以将环境变量的设置组合在一行中?如果是这样,我会怎么做?这是一个好习惯吗?
Question: Is it is possible to combine the setting of environment variables on a single line? If so, how would I do it? And is it a good practice?
推荐答案
指定环境有两种格式.如果你需要单个变量,那么你下面的格式
There are two formats for specifying environments. If you need single variable then you below format
ENV X Y
这会将 X 分配为 Y
ENV X Y Z
这会将 X 分配为 Y Z
如果您需要分配多个环境变量,则使用其他格式
If you need to assign multiple environment variables then you use the other format
ENV X=Y Z=A
这会将 X 指定为 Y
并将 Z 指定为 A
.所以你的 Dockerfile
应该是
This will assign X as Y
and Z as A
. So your Dockerfile
should be
FROM alpine:3.6
ENV RUBY_MAJOR=2.4
RUBY_VERSION=2.4.1
RUBY_DOWNLOAD_SHA256=4fc8a9992de3e90191de369270ea4b6c1b171b7941743614cc50822ddc1fe654
RUBYGEMS_VERSION=2.6.12
BUNDLER_VERSION=1.15.3
RUN env
这篇关于Dockerfile:在单行中设置多个环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!