ASP.NET Core cookbook · Beginner

Publish a container image without a Dockerfile

Maintaining Dockerfiles for many services is repetitive, and images run as root.

Create a free account to save this page, take notes and track your progress. Sign in

Approach

Use the .NET SDK's built-in container publishing: set the image name and base image in the project file and run dotnet publish /t:PublishContainer. .NET images run as a non-root user by default.

Solution

# Build and push a multi-arch image straight from the SDK
dotnet publish src/Orders.Api -c Release \
  -p:ContainerRegistry=registry.example.com \
  -p:ContainerRepository=shop/orders-api \
  -p:ContainerImageTags='"1.4.0;latest"' \
  -p:ContainerRuntimeIdentifiers='"linux-x64;linux-arm64"' \
  /t:PublishContainer

Pitfalls

  • Pushing latest only, so deployments can't be pinned or rolled back.
  • Forgetting ASPNETCORE_HTTP_PORTS when the platform expects a port other than the default 8080.
  • Including development appsettings or secrets in the image.

Variations

  • Set <ContainerFamily>noble-chiseled</ContainerFamily> for smaller, shell-less runtime images.
  • Native AOT (<PublishAot>true</PublishAot>) with runtime-deps base images for very small, fast-starting APIs.

Further reading: Containerize a .NET app with dotnet publish

Reference: https://learn.microsoft.com/en-us/dotnet/core/containers/sdk-publish

More ASP.NET Core recipes

Was this page helpful?