Hi,
I tried to build and run this containerfile to test ARG and ENV instructions.
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
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
@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 ?
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.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.