agapanthusblue
agapanthusblue

Reputation: 477

How can I print the value of a variable, either env or arg, at "docker build" time?

Title says it all. The ARG instruction prints the variable and it's default value, not it's current value. I'd like to know it's current value.

How do I find that?

I mean, I know what was passed in, but that's apparently not what's being used so I'm confused.

Upvotes: 6

Views: 20482

Answers (2)

Hans Kilian
Hans Kilian

Reputation: 25479

Maybe I'm missing something, but can't you just echo it?

If you take this Dockerfile

FROM ubuntu
ARG test
RUN echo $test

and build it with

docker build --build-arg test=hello . --progress=plain

it prints hello.

Upvotes: 10

ErikMD
ErikMD

Reputation: 14743

The answer by @HansKilian is probably the way to go if you are able to change the Dockerfile and check the value of the build-arg on build time.

Otherwise, here is another way to know after the image build time what were the values of the build args: the only requirement is that after the ARG directive you are interested in, there is at least one RUN directive (but not necessarily using the build-arg $variable). − If it's not the case anyway, this ARG directive wouldn't be useful at all!

Proof-of-concept

Consider the following Dockerfile:

FROM ubuntu
ARG test1
RUN echo any shell command
ARG test2
RUN echo another shell command
CMD ["/bin/bash"]

You can build this example image like this:

$ docker build --build-arg test1="hi" --build-arg test2="hello world" -t image-name .

then run:

$ docker history image-name --format {{.CreatedBy}} --no-trunc
/bin/sh -c #(nop)  CMD ["/bin/bash"]
|2 test1=hi test2=hello world /bin/sh -c echo another shell command
/bin/sh -c #(nop)  ARG test2
|1 test1=hi /bin/sh -c echo any shell command
/bin/sh -c #(nop)  ARG test1
[…]

to get from the history of the image (displayed in reverse chronological order), the concrete values of the build args (cf. |2 test1=hi test2=hello world …). These build-args values are specifically exposed as environment variables at build time, hence this notation with = signs here, and the |2 prefix denotes the number of variables assignments that have to be extracted from the displayed command line.

Upvotes: 2

Related Questions