How to read environment variable during build time when using baseimage-docker from phusion

49 Views Asked by At

I build my docker image using based on phusion/baseimage

The command I run to build my image is:

docker build --build-arg ENV_FILE=.env -t demo:latest .

my .env file looks like this:

VAR1=foo
VAR2=bar

and my dockerfile is like this

FROM phusion/baseimage:jammy-1.0.1

ARG VAR1

# I would like to print VAR1
RUN echo "my var1 equals to : $VAR1"

However I can't get VAR1 printed, it just shows an empty value

....
 ---> Running in 8f78bb5d59cc
my var1 equals to :

Any idea how I can read it when building the image?

1

There are 1 best solutions below

0
Hans Kilian On

You can do

(source .env; docker build --build-arg VAR1=$VAR1 -t demo:latest .)

You have to specify each variable though. I don't think there's a way to pass them all in as build arguments. .env files are meant for use at run-time.