我有一个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 .



上面的错误表明$ MyVersion在运行RUN dotnet的行上未通过/设置正确publish MyApp.csproj / p:Version = $ MyVersion -c Release -o / app

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

请指教,

最佳答案

尝试在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"]



您可以检查Understand how ARG and FROM interact

关于linux - 生成镜像时将生成参数传递给dockerfile时出错- '$MyVersion'不是有效的版本字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53162636/

10-10 16:30