Jon Abraham
Jon Abraham

Reputation: 965

How to read a value in dockerfile during run-time

I am having a use-case in which i am storing two arguments in openshift config map as below -

DT_URL="https://www.something.com"
DT_TOKEN="1234ABCD"

Now i want these values to be fetched during run-time using jenkins. In my dockerfile i have arguments as below-

ARG URL=${DT_URL}
ARG TOKEN=${DT_TOKEN}

Unfortunately i am not getting any values fetched. I would appreciate any help.

Upvotes: 1

Views: 57

Answers (1)

Neo Anderson
Neo Anderson

Reputation: 6372

ARG is used to define variables that live only during the image build-time.
ENV can be used to define environment variables that live during image build-time and container run-time.

The problem here is that you can only pass ARGs, when you build the image, using the --build-arg option.

However, nothing is stopping you to assign the value of an ARG to an ENV, inside the Dockerfile.
Here is a example:

Create a Dockerfile

FROM alpine
ARG MY_ARG
ENV MY_ENV=$MY_ARG
RUN echo "the env is:" $MY_ENV
RUN echo "the arg is:" $MY_ARG

Build the image:

docker build -t arg-env-example --build-arg MY_ARG=some_value .

Both the ARG and the ENV were assigned some_value during build time:

...
Step 4/5 : RUN echo "the env is:" $MY_ENV
 ---> Running in 9a383134c927
the env is: some_value
Removing intermediate container 9a383134c927
 ---> 8a2d83609296
Step 5/5 : RUN echo "the arg is:" $MY_ARG
 ---> Running in b881d9f4208e
the arg is: some_value
...

Run the container and check both the ARG and the ENV:

docker run -it arg-env-example sh

/ # echo $MY_ENV
some_value   << this is the value that was passed at build-time to MY_ARG
/ # echo $MY_ARG
      << nothing here :)
/ #

Upvotes: 1

Related Questions