简体   繁体   中英

Docker - build .net core runnable project referencing .net framework 4.7.2 projects

I have a solution structure in which my runnable application is asp.core webapi (netcoreapp3.1) and it references projects that are written in .net framework 4.7.2. I want to dockerize this application and I'm pretty sure I'll have to write a custom Dockerfile downloading the sdk for 4.7.2 and then the same for the runtime but the question is - how would you approach this?

EDIT: I tried the following Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet-framework:4.7.2-sdk AS dotnet-framework-build
WORKDIR /src
COPY ["ClassLibrary1/ClassLibrary1.csproj", "ClassLibrary1/"]
COPY . .
RUN dotnet build "ClassLibrary1/ClassLibrary1.csproj" -c Release -o /app/build

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS dotnet-core-build
WORKDIR /src
COPY ["NetCoreAndFrameworkDocker/NetCoreAndFrameworkDocker.csproj", "NetCoreAndFrameworkDocker/"]
COPY --from=dotnet-framework-build /src .
RUN dotnet restore "NetCoreAndFrameworkDocker/NetCoreAndFrameworkDocker.csproj"
COPY . .
WORKDIR "/src/NetCoreAndFrameworkDocker"
RUN dotnet build "NetCoreAndFrameworkDocker.csproj" -c Release -o /app/build

FROM dotnet-core-build AS publish
RUN dotnet publish "NetCoreAndFrameworkDocker.csproj" -c Release -o /app/publish

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

but it still fails on dotnet build NetCoreAndFrameworkDocker.csproj ... line with the same error as previously.

C:\Program Files\dotnet\sdk\3.1.404\Microsoft.Common.CurrentVersion.targets(1177,5): error MSB3644: The reference assemblies for .NETFramework,Version=v4.7.2 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [C:\src\ClassLibrary1\ClassLibrary1.csproj]

So now the question is how to build the runnable app without building the net framework project, just taking the already built binaries?

Use multi-stage builds. With multi-stage builds, you use multiple FROM statements in your Dockerfile.

https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds

Each FROM instruction can use a different base, and each of them begins a new stage of the build.

You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.

# 1st build for .Net framework classlibrary
FROM microsoft/dotnet-framework:4.7.2-sdk AS build
...
...
...
RUN dotnet build "ClassLibrary1.csproj" -c Release

# 2nd build for core app
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

did you solved ? because i have the same problem !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM