0

I have a project targeting .NET 6 and .NET 7.

My Dockerfile looks like this:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore -p:TargetFramework=net6.0
# Build and publish a release
RUN dotnet publish --framework net6.0 -p:TargetFramework=net6.0 -c Release -o out 

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyApp.dll"]

While the dotnet publish command works on my local machine outside Docker build, it results in this error during the Docker build:

Step 5/9 : RUN dotnet publish --framework net6.0 -p:TargetFramework=net6.0 -c Release -o out
 ---> Running in dfb3663dd3ca
MSBuild version 17.3.2+561848881 for .NET
  Determining projects to restore...
/usr/share/dotnet/sdk/6.0.405/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(144,5): error NETSDK1045: The current .NET SDK does not support targeting .NET 7.0.  Either target .NET 6.0 or lower, or use a version of the .NET SDK that supports .NET 7.0. [/app/MyApp.csproj]
The command '/bin/sh -c dotnet publish --framework net6.0 -p:TargetFramework=net6.0 -c Release -o out' returned a non-zero code: 1

I could even specify abc as TargetFramework and it didn't complain aside the message shown already:

RUN dotnet publish -f:abc -c Release -o out

It also seems to ignore this global.json:

{
  "sdk": {
    "version": "6.0.0",
    "rollForward": "latestMinor",
    "allowPrerelease": false
  }
}
Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
  • 1
    If you want to target .NET 6, is there a reason for targeting .NET 7 in your csproj file(s) (which I assume is where it's getting that target framework from)? – Hans Kilian Jan 31 '23 at 21:24
  • 1
    It's actually caused by this one (for this particular project I currently don't want to target .NET 7 but the unresolved issue forces me into it): https://stackoverflow.com/questions/75297718/net-6-sdk-cant-be-used-after-net-7-sdk-is-installed-on-xubuntu – Alexander Zeitler Jan 31 '23 at 21:27
  • Can you show the relevant section of your csproj file for any Framework-related properties? – Matt Thalman Feb 01 '23 at 13:41

0 Answers0