ARGs in dockerfile are great for passing in build time paramaters (before a container is built)

ENVs in dockefile are for passing in run time parameters (once a container is already running)

Your powershell in your Dockerfile may need custom arguments passed in at run time.  These are RUN TIME arguments but your Dockerfile is a BUILD TIME artifact. So, how do you turn build time parameters to runtime parameters?

Powershell – EXEC mode vs. SHELL MODE

Gotcha 1 – ARG by itself will not work for powershell

– Try just ARG by itself to pass in parameter – The first thing that took a while for me to realize is that all the CMDs, ENTRYPOINTS and RUN directives inside a dockerfile, are always executed in EXEC mode. In EXEC mode, you CANNOT do variable substitution like the one shown below.

ARG path=\”c:\MyPath\”

RUN echo $ENV: PATH C:\code\repros\win-arg-env > docker build –no-cache -t win-arg-env .


Gotcha 2 – Try Adding both ENV and ARG. Let the ARG value be the default ENV value and pass in the new ENV value at runtime. This SHOULD work, but doesn’t (Last step will explain why)

ARG path=\”c:\MyPath\”

ENV PATHENV = $ARG

RUN echo $ENV: PATHENV C:\code\repros\win-arg-env > docker build –no-cache -t win-arg-env .

All indications point to this working  – since all powershell needs in an environment variable passed in as $env:value

However, this DOES NOT WORK either. What gives?

The solution – Switch to SHELL mode

Using the line below , powershell now runs in SHELL mode. This PERMITS variable substitution.

SHELL [“powershell”, “-Command”]

SO, this code below works in dockerfile

ARG path=\”c:\MyPath\”

ENV PATHENV = $ARG

SHELL [“powershell”, “-Command”]

RUN echo $ENV: PATHENV C:\code\repros\win-arg-env > docker build –no-cache -t win-arg-env .

The Final Gotcha – Escape characters for powershell strings

Now that you are able to pass in runtime variables, you just have to escape quote values correctly (as shown at the top of this post).

Happy Coding with ARG and ENV, Happy Dockerfile  coding.

Another post on what Docker COmpose should look like –to work in conjunction with the ARG, ENV and Dockerfile powershell will follow.

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.