我有一个asp.net .net core 2.1项目,我们使用Linux托管的docker容器。这一直很好。现在,我们需要在使用docker文件构建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 .
在Linux中,我收到此消息
“ GetAssemblyVersion”任务未获得所需值 参数“ NuGetVersion”
在Windows中
C:\ Program Files \ dotnet \ sdk \ 2.1.402 \ NuGet.targets(114,5): 错误:“ $ MyVersion”不是有效的版本字符串。
上面的错误表明$ MyVersion在运行RUN dotnet的行上未通过/设置正确,发布MyApp.csproj / p:Version = $ MyVersion -c发行-o / app
但是,我看不出有什么问题。
请告知,
答案 0 :(得分:1)
尝试在ARG MyVersion
之前添加RUN dotnet build MyApp.csproj /p:Version=$MyVersion -c Release -o /app
。
这是一个完整的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"]
在FROM之前声明的ARG在构建阶段之外,因此,FROM之后的任何指令都不能使用它。要使用在第一个FROM之前声明的ARG的默认值,请使用ARG指令,该指令在构建阶段内部不带值:
答案 1 :(得分:0)
最后我找到了问题,
对于基于Windows的docker文件,我们必须使用%MyVar%引用变量,
运行dotnet build MyApp.csproj / p:Version =%MyVersion%-c发行-o / app
关于ARG,它们必须放在FROM语句的正下方。
FROM microsoft / dotnet:2.1-aspnetcore-runtime-nanoserver-1709 AS基础 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,它是相同的,除了使用$ MyVar引用ARG
运行dotnet build MyApp.csproj / p:Version = $ MyVersion -c版本-o / app