问题描述
我的印象是,可以如下将环境变量设置在一行上,以最大程度地减少中间图像。
从阿尔卑斯山出发:3.6
ENV RUBY_MAJOR 2.4 \
RUBY_VERSION 2.4.1 \
RUBY_DOWNLOAD_SHA256 4fc8a9992de3e90191de369270ea4b6c1b171b7941743614cc50822ddc1fe654 \
RUSION_MS VER2.6
但是,根据此代码段运行容器并调用#set | grep RU
我看到这些变量没有单独分配,而是组合为一个字符串。
RUBY_MAJOR ='2.4 RUBY_VERSION 2.4.1 RUBY_DOWNLOAD_SHA256 4fc8a9992de3e90191de369270ea4b6c1b171b7941743614cc50822ddc1fe654 RUBYGEMS_VERSION 2.6.12 BUNDLER_VERSION 1.15.3'
但是,如果我按如下所示明确设置每个变量,则可以得到预期的输出,并且在调用变量。
$ c $ b>ENV RUBY_MAJOR 2.4
ENV RUBY_VERSION 2.4.1
ENV RUBY_DOWNLOAD_SHA256 4fc8a9992de3e90191de369270ea4b6c1b171b7941743614CC50822dDC1FE .12
ENV BUNDLER_VERSION 1.15.3
问题:是否可以在一行上组合环境变量的设置?如果是这样,我该怎么办?
解决方案有两种用于指定环境的格式。如果需要单个变量,则可以使用以下格式
ENV XY
这会将X分配为
Y
ENX XYZ
这会将X分配为
YZ
如果需要分配多个环境变量,则可以使用其他格式
ENV X = YZ = A
这会将X指定为
Y
和Z为A
。因此,您的Dockerfile
应该是FROM高山:3.6
ENV RUBY_MAJOR = 2.4 \
RUBY_VERSION = 2.4.1 \
RUBY_DOWNLOAD_SHA256 = 4fc8a9992de3e90191de369270ea4b6c1b171b7941743614cc50822ddc1fe654 \
RUBYGEMS_VERSION = 2.612。
RUN env
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
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
This will assign X as
Y
ENX X Y Z
This will assign X as
Y Z
If you need to assign multiple environment variables then you use the other format
ENV X=Y Z=A
This will assign X as
Y
and Z asA
. So yourDockerfile
should beFROM 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:在一行中设置多个环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!