Reputation: 2328
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
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 aFROM
is outside of a build stage, so it can’t be used in any instruction after aFROM
. To use the default value of anARG
declared before the firstFROM
use anARG
instruction without a value inside of a build stage.
Upvotes: 6