Dimt
Dimt

Reputation: 2328

Dockerfile ARG always null

I have spent quite a lot of time to get to the bottom of this. What I am trying to achieve is to pass a connection string to dockerfile as a build argument from docker-compose. The below is a very simple snippet which repoduces the problem. The output of Write-Host ${SOMEARG} is always null.

Having the below dockerfile:

FROM microsoft/iis
ARG SOMEARG=test

SHELL ["powershell", "-command"]

RUN Write-Host 'HERE:';`
    Write-Host ${SOMEARG}

I have output:

Step 1/4 : FROM microsoft/iis
 ---> 85fb57957cf1
Step 2/4 : ARG SOMEARG=test
 ---> Running in 84a7fc37994c
Removing intermediate container 84a7fc37994c
 ---> 2099e56f466f
Step 3/4 : SHELL ["powershell", "-command"]
 ---> Running in 61852677a26a
Removing intermediate container 61852677a26a
 ---> b934f778c13b
Step 4/4 : RUN Write-Host 'HERE:';    Write-Host ${SOMEARG}
 ---> Running in 3fde911ecdc1
HERE:

Removing intermediate container 3fde911ecdc1
 ---> 5331ce22c3ec
Successfully built 5331ce22c3ec

Upvotes: 5

Views: 4305

Answers (2)

Rikki
Rikki

Reputation: 3528

Using ARG value of ArgName with default SHELL set to PowerShell can be done by referring to $env:ArgName.

Just remember to understand how ARG and FROM interact:

An ARG declared before a FROM is outside of a build stage, so it can’t be used in any instruction after a FROM. To use the default value of an ARG declared before the first FROM use an ARG instruction without a value inside of a build stage.

Upvotes: 6

yamenk
yamenk

Reputation: 51778

Actually this is solved here.

Since you are using a windows container, build args should be referenced using %%since cmd is the default shell.

FROM microsoft/iis
ARG SOMEARG=test

SHELL ["powershell", "-command"]

RUN Write-Host 'HERE:';`
    Write-Host %SOMEARG%

Upvotes: 1

Related Questions