dotnet / sdk-container-builds

Libraries and build tooling to create container images from .NET projects using MSBuild
https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container
MIT License
179 stars 34 forks source link

Warning about explicit reference for console apps (8.0.200) #554

Closed sualitu closed 7 months ago

sualitu commented 7 months ago

Hey! Since 8.0.200, I now get a warning when explicitly referencing Microsoft.NET.Build.Containers, similar how it changed for web applications. That's great if it's now fully included in the SDK.

However, if I remove the reference, when I publish a project, no container is published (but there are also no errors).

Project file (simply just dotnet new console):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

</Project>

When publishing:

λ dotnet publish --os linux --arch x64 -c Release /t:PublishContainer
MSBuild version 17.9.4+90725d08d for .NET
  Determining projects to restore...
  Restored /.../Container/Container.fsproj (in 147 ms).
  Container -> /.../Container/bin/Release/net8.0/linux-x64/Container.dll
  Container -> /.../Container/bin/Release/net8.0/linux-x64/publish/

As you can see no container is published. If I readd the reference, things work as I expect:

λ dotnet add package Microsoft.NET.Build.Containers
dotnet publish --os linux --arch x64 -c Release /t:PublishContainer
  Determining projects to restore...
  Writing /var/folders/g5/tmdb3_294sb_zfk2nd474nsc0000gp/T/tmpYwCkLT.tmp
info : X.509 certificate chain validation will use the fallback certificate bundle at '/usr/local/share/dotnet/sdk/8.0.201/trustedroots/codesignctl.pem'.
info : X.509 certificate chain validation will use the fallback certificate bundle at '/usr/local/share/dotnet/sdk/8.0.201/trustedroots/timestampctl.pem'.
info : Adding PackageReference for package 'Microsoft.NET.Build.Containers' into project '/.../Container/Container.fsproj'.
info :   CACHE https://api.nuget.org/v3/registration5-gz-semver2/microsoft.net.build.containers/index.json
info : Restoring packages for /.../Container/Container.fsproj...
info :   CACHE https://api.nuget.org/v3/vulnerabilities/index.json
info :   CACHE https://api.nuget.org/v3-vulnerabilities/2024.02.29.05.28.27/vulnerability.base.json
info :   CACHE https://api.nuget.org/v3-vulnerabilities/2024.02.29.05.28.27/2024.02.29.23.28.32/vulnerability.update.json
info : Package 'Microsoft.NET.Build.Containers' is compatible with all the specified frameworks in project '/.../Container/Container.fsproj'.
info : PackageReference for package 'Microsoft.NET.Build.Containers' version '8.0.200' added to file '/.../Container/Container.fsproj'.
info : Generating MSBuild file /.../Container/obj/Container.fsproj.nuget.g.props.
info : Generating MSBuild file /.../Container/obj/Container.fsproj.nuget.g.targets.
info : Writing assets file to disk. Path: /.../Container/obj/project.assets.json
log  : Restored /.../Container/Container.fsproj (in 71 ms).
MSBuild version 17.9.4+90725d08d for .NET
  Determining projects to restore...
  Restored /.../Container/Container.fsproj (in 120 ms).
  Container -> /.../Container/bin/Release/net8.0/linux-x64/Container.dll
/usr/local/share/dotnet/sdk/8.0.201/Containers/build/Microsoft.NET.Build.Containers.targets(194,5): warning : Microsoft.NET.Build.Containers NuGet package is explicitly referenced. Consider removing the package reference to Microsoft.NET.Build.Containers as it is now part of .NET SDK. [/.../Container/Container.fsproj]
  Container -> /.../Container/bin/Release/net8.0/linux-x64/publish/
  Building image 'container' with tags 'latest' on top of base image 'mcr.microsoft.com/dotnet/runtime:8.0'.
  Pushed image 'container:latest' to local registry via 'docker'.

However, as you can see, I get the warning.

λ dotnet --version
8.0.201

This is also reflected on "real" projects in our CI, so not just locally for me.

What am I doing wrong? Or are my expectations wrong?

baronfel commented 7 months ago

Hello! The tl;dr is that console apps will need to add a property to their project files:

<PropertyGroup>
...
  <EnableSdkContainerSupport>true</EnableSdkContainerSupport>
...
</PropertyGroup>

The slightly longer form is that the container publishing target needs to be available but not necessarily enabled for all project types in order to support calling dotnet publish /t:PublishContainer at a solution level and having all relevant projects containerize themselves. Web Projects and Worker Projects have a hook to automatically set this property, but console apps don't (because otherwise all library/etc. projects would have the ability to containerize, which doesn't make sense). So there's a bit more of a 'process' to enabling console apps. We hope to find a good way to default this property to true for them in a later release.

sualitu commented 7 months ago

Aha! Thanks a lot! Just to confirm, this fixes things.

Also wanted to add that we are super happy publishing containers this way, it has cleaned up both CI and local development a lot since .NET 7. This minor hiccup does not change that.