我有一个 asp.net .net core 2.1 项目,我们正在使用 Linux 中托管的 docker 容器。这一直运作良好。现在,我们有一个需求,就是在使用docker file构建docker镜像时,需要设置项目的汇编版本。
因此,假设我可以在使用 dockerfile 构建 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\2.1.402\NuGet.targets(114,5): error : '$MyVersion' is not a valid version string.
上面的错误表明 $MyVersion 没有在 RUN dotnet publish MyApp.csproj/p:Version=$MyVersion -c Release -o/app 行上正确传递/设置
但是,我看不出有什么问题。
请指教,
最佳答案
终于找到问题了
对于基于 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
关于linux - 构建图像时将构建参数传递给 dockerfile 时出错 - '$MyVersion' 不是有效的版本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53162636/