cancel
Showing results for 
Search instead for 
Did you mean: 
AlessandraS
Flight Engineer
Flight Engineer
  • 86 Views

DO188 - Chapter 4 - 4.3 Build Images with Advanced Containerfile Instructions - ARG/ENV instructions

Hi, 
I tried to build and run this containerfile to test ARG and ENV instructions.

AlessandraS_1-1758812749286.png

I built the image providing the --build-arg flag for VERSION and BIN_DIR and then ran the container (see image below).

I expected to find the Environment variable of the running container valued this way:

VERSION = 1.16.8 and BIN_DIR = /usr/local/bin/

while, as you can see below VERSION = 5 and BIN_DIR=/www (but these are the values specified in the --build-arg flag).   What's wrong in my tests?

Thanks

AlessandraS_0-1758812706355.png

3 Replies
ARoumiantsev
Flight Engineer
Flight Engineer
  • 63 Views

Hi @AlessandraS 

Try build with only one of your --build-arg and you will see a difference.  In your case, when you define both --build-args, ENV variables will get thier values from ARG values ( --build-arg ), but not default in ENV instruction.

Good luck 

Chetan_Tiwary_
Community Manager
Community Manager
  • 53 Views

@AlessandraS When you use --build-arg during the build, it actually overrides any default values you set in your ENV ...${VAR:-default} expression. That s exactly how Docker’s build arguments are supposed to work, so this behavior is expected. The default only kicks in when no --build-arg is given or when the ARG is undefined.

@Travis do you want to weigh in here ?

 

https://docs.docker.com/build/building/variables/ 

0 Kudos
Travis
Moderator
Moderator
  • 37 Views

@AlessandraS -

I would say @Chetan_Tiwary_ did an excellent job explaining. Essentially what you are doing with this line 

ENV VERSION=${VERSION:-1.16.8} \
    BIN_DIR=${BIN_DIR:-/usr/local/bin/}

is setting environment variables. In this instance, you have defaults set of 1.16.8 and /usr/local/bin. However, as part of the build process, you specified build arguments which would override the default values on the environment line and that is exactly what you saw.

Another interesting thing you could do, in addition to what @ARoumiantsev suggested (changing build arguments and only providing one value or providing no values) is to use a "-e" in the podman run command to provide runtime environment variables into the container. Use something that is neither in your Containerfile nor the podman build with arguments and see how that changes things. 

The real thing here that might not be clear ...

ARG - provides a user the ability to use build-time arguments as variables for a container allowing custom containers to be built with a single Containerfile just by changing variables.

ENV - provides a user the ability to provide runtime arguments as environment variables customizing the running container but not the container image.

Using both together allows a Container image to be customized at build time, at run time, or a combination of the two so you have the best of both worlds.

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
0 Kudos
Join the discussion
You must log in to join this conversation.