构建图像时将构建参数传递给 dockerfile 时出错 - '$MyVersion' 不是有效的版本字符串

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

我有一个 asp.net .net core 2.1 项目,我们正在使用 docker 托管在 Linux 中的容器。这一直运作良好。现在,我们需要在使用 docker 文件构建 docker 图像时设置项目的汇编版本。

因此,假设我可以在使用 docker 文件构建 docker 图像时将其作为 build-arg 传递。但是,我无法让它工作。

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"]

并使用命令构建它

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.1.402\NuGet.targets(114,5): error : '$MyVersion' is not a valid version string.

上面的错误表明 $MyVersion 在行 运行 dotnet publish MyApp.csproj /p:Version=$MyVersion -c Release -o /app[ 上没有得到 passed/set 正确=13=]

但是,我看不出有什么问题。

请指教,

尝试在 RUN dotnet build MyApp.csproj /p:Version=$MyVersion -c Release -o /app 之前添加 ARG MyVersion

这是一个完整的 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:

您可以查看 Understand how ARG and FROM interact

终于找到问题了,

对于基于 windows 的 docker 文件,我们必须使用 %MyVar%,

来引用变量

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

关于 ARG,它们必须放在 FROM 语句的正下方。

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

这是工作 docker 文件

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"]

对于linux,也是一样的,只是ARG是使用$MyVar

引用的

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