Mike Christensen
Mike Christensen

Reputation: 91608

Using a dockerfile argument in a RUN statement

I have the following Dockerfile, which works:

FROM someimage
ARG transform

COPY Web*.config /inetpub/wwwroot/
RUN powershell /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/web.debug.config"

However, I'd like to make the web.debug.config file an argument passed in when I build. So I've changed the last line to:

RUN powershell /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/${transform}"

When I do this, the ${transform} argument does not get interpolated and gets converted to an empty string. I've confirmed that the transform argument is getting passed in correctly, since I can do:

COPY ${transform} /inetpub/wwwroot/

...and the file gets copied. Is there some other way of interpolating arguments in a string using the RUN command?

I'm using Docker 18.03.1-ce on Windows 10.

Upvotes: 7

Views: 3576

Answers (3)

mahes wari
mahes wari

Reputation: 91

Try this dockerfile

FROM someimage
ENV trans transform

COPY ${trans}/inetpub/wwwroot/
RUN powershell /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/${trans}"

Run the command

docker build --build-arg transform="web.debug.config" -t sample:latest .

Upvotes: 0

Mike Christensen
Mike Christensen

Reputation: 91608

Well, I found a solution that works but it's definitely not ideal. It appears variables simply aren't expanded in RUN statements (at least on Windows, haven't tried Linux). However, the COPY statement will expand them. So, I can copy my file to a temp file with a hard coded name, and then use that:

COPY ./ /inetpub/wwwroot/
COPY ${transform} /inetpub/wwwroot/Web.Current.config
RUN powershell -executionpolicy bypass /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/Web.Current.config"

In this particular situation, it works for me.

Update: Found another method that works

This is perhaps a better method using environment variables:

FROM someimage
ARG transform
ENV TransformFile ${transform}

COPY ./ /inetpub/wwwroot/
RUN powershell -executionpolicy bypass /Build/Transform.ps1 -xml "/inetpub/wwwroot/web.config" -xdt "/inetpub/wwwroot/$ENV:TransformFile"

In this case, Docker will evaluate the parameter transform and set it to the environment variable TransformFile. When I use it in the PowerShell script, it's no longer Docker that's evaluating the argument, but Powershell itself. Thus, the Powershell syntax for interpolating an environment variable must be used.

Upvotes: 4

VonC
VonC

Reputation: 1324258

The shell form of Dockerfile RUN should allow for arg expansion.

Check if the quotes are not in the way with

RUN powershell ... -xdt "/inetpub/wwwroot/"${transform}

Note: you can see in the Microsoft documentation powershell commands using $var instead of ${var}

Upvotes: 0

Related Questions