Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 

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?

like image 250
BrianWilson Avatar asked Aug 06 '17 06:08

BrianWilson


People also ask

Can Dockerfile have environment variables?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How do I pass an environment variable in Dockerfile?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

What is the difference between ARG and env in Dockerfile?

ENV is for future running containers. ARG for building your Docker image. ¶ ENV is mainly meant to provide default values for your future environment variables.

Does docker build .env files?

If you are using docker-compose (which now comes bundled with Docker), . env is the default filename for the file that contains variables that are made available to the parser for the docker-compose. yml file ONLY, and not to the build process or container environment variables.


1 Answers

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

ENV 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 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 
like image 78
Tarun Lalwani Avatar answered Sep 19 '22 23:09

Tarun Lalwani