Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error passing build arguments to dockerfile when building an image - '$MyVersion' is not a valid version string

I have a asp.net .net core 2.1 project and we're using docker container hosted in Linux. This has been working well. Now, we have a requirement to set the assembly version of the project when building the docker image using docker file.

So, supposedly I can pass it as build-arg when building the docker image with dockerfile. But, I can't get it working.

ARG MyVersion="0.0.1.0"

FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1709 AS base
WORKDIR /app
EXPOSE 8081 

FROM microsoft/dotnet:2.1-sdk-nanoserver-1709 AS build
WORKDIR /src
COPY source/MyApp/MyApp.csproj source/MyApp/  
RUN dotnet restore source/MyApp/MyApp.csproj  
COPY . .
WORKDIR /src/source/MyApp
RUN dotnet build MyApp.csproj /p:Version=$MyVersion -c Release -o /app

FROM build AS publish
RUN dotnet publish MyApp.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyApp.dll"]

and build it with the command

docker build --build-arg MyVersion=1.22.1.0 -t MyGoodApp -f MyApp.Dockerfile .

In linux,I get this message

The "GetAssemblyVersion" task was not given a value for the required parameter "NuGetVersion"

In windows

C:\Program Files\dotnet\sdk\2.1.402\NuGet.targets(114,5): error : '$MyVersion' is not a valid version string.

The error above indicates $MyVersion is not getting passed/set correct on the line RUN dotnet publish MyApp.csproj /p:Version=$MyVersion -c Release -o /app

But, I can't see what is wrong.

Please advise,

like image 570
Felix Cen Avatar asked Oct 25 '25 02:10

Felix Cen


2 Answers

Finally I found the issues,

For windows based docker file, we have to reference the variable with %MyVar%,

RUN dotnet build MyApp.csproj /p:Version=%MyVersion% -c Release -o /app

and regarding to ARG, they must be placed right below the FROM statement.

FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1709 AS base ARG MyVersion="0.0.1.0"

Here is the working docker file

FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1709 AS base
ARG MyVersion="0.0.1.0"
WORKDIR /app
EXPOSE 8081 

FROM microsoft/dotnet:2.1-sdk-nanoserver-1709 AS build
ARG MyVersion
WORKDIR /src
COPY source/MyApp/MyApp.csproj source/MyApp/  
RUN dotnet restore source/MyApp/MyApp.csproj  
COPY . .
WORKDIR /src/source/MyApp
RUN dotnet build MyApp.csproj /p:Version=%MyVersion% -c Release -o /app

FROM build AS publish
RUN dotnet publish MyApp.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyApp.dll"]

For linux, it is the same, except that the ARG is reference using $MyVar

RUN dotnet build MyApp.csproj /p:Version=$MyVersion -c Release -o /app

like image 109
Felix Cen Avatar answered Oct 26 '25 16:10

Felix Cen


Try to add ARG MyVersion before RUN dotnet build MyApp.csproj /p:Version=$MyVersion -c Release -o /app.

Here is a complete dockerfile

    ARG MyVersion="0.0.1.0"

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 49880
EXPOSE 44381

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["DockerVersion/DockerVersion.csproj", "DockerVersion/"]
RUN dotnet restore "DockerVersion/DockerVersion.csproj"
COPY . .
WORKDIR "/src/DockerVersion"
RUN dotnet build "DockerVersion.csproj" -c Release -o /app

FROM build AS publish
ARG MyVersion
RUN echo $MyVersion
RUN dotnet publish "DockerVersion.csproj" /p:Version=$MyVersion -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DockerVersion.dll"]

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:

You could check Understand how ARG and FROM interact.

like image 28
Edward Avatar answered Oct 26 '25 18:10

Edward