Search code examples
docker.net-coredockerfile

ARG variable in Dockerfile not expanding


I'm very new to Docker (this is the first time I try it after reading a few tutorials) and I'm stuck on something that seems very simple.

I have this lines in my Dockerfile that are supposed to build a dotnet app, and as you can see I have an ARG where I define the build configuration and then I pass it to the compiler in the next line:

ARG BUILD_CONFIGURATION=Release
RUN dotnet build "MyProject/MyProject.csproj" -c ${BUILD_CONFIGURATION} -o /bin

However the problem is that, when building the Docker image, the ${BUILD_CONFIGURATION} arg does not get expanded, but is instead passed in literally so the build command that is actaully invoked is literally:

dotnet build "MyProject/MyProject.csproj" -c ${BUILD_CONFIGURATION} -o /bin

Obviously the compiler doesn't like that and gives me the error:

MSB3052: The parameter to the compiler is invalid, '/define:${BUILD_CONFIGURATION}' will be ignored.

Why is this not working? What am I missing?

NOTE: I've also tried $BUILD_CONFIGURATION without the curly braces, and also tried using ENV instead of ARG, but the result is the same.

EDIT: some further info if needed

  • I'm using Docker Desktop on Windows 11
  • I'm invoking the build process from the Windows Terminal with the command docker build .
  • This is the full dockerfile if it's any help:

Dockerfile

#Copy source code into container
WORKDIR /src
COPY . .    
#Build application
ARG BUILD_CONFIGURATION=Release
RUN dotnet build "MyProject/MyProject.csproj" -c ${BUILD_CONFIGURATION} -o /bin

Solution

  • I attempted your example using linux alpine base image, but for me in the console it prints the variable while running docker build .

    Here is Dockerfile:

    FROM alpine
    #Copy source code into container
    WORKDIR /src
    COPY . .    
    #Build application
    ARG BUILD_CONFIGURATION=Release
    RUN dotnet build "MyProject/MyProject.csproj" -c ${BUILD_CONFIGURATION} -o /bin
    

    Here is output:

    ....
     => [2/4] WORKDIR /src                                                                                     0.1s
     => [3/4] COPY . .                                                                                         0.3s
     => ERROR [4/4] RUN dotnet build "MyProject/MyProject.csproj" -c Release -o /bin                           0.3s
    ------                                                                                                          
     > [4/4] RUN dotnet build "MyProject/MyProject.csproj" -c Release -o /bin:
    0.323 /bin/sh: dotnet: not found
    ...
    

    Although it is outputting error but at least it is printing Release as part of log, which proves that it is working for linux base image...

    Edit

    Since you are using windows based base image, the environment variables are evaluated using %% rather than $ symbol. So you can modify your run command to make it working:

    dotnet build "MyProject/MyProject.csproj" -c %BUILD_CONFIGURATION% -o /bin