Skip to content

Lesson 8 of 9 · 25m

Publishing .NET Applications Across Linux, Windows, and Containers

Choose framework-dependent, self-contained, trimmed, or container packaging deliberately and make runtime identity explicit before deployment.

A deployment usually breaks long before the application logic runs: wrong runtime architecture, missing ICU dependency, unwriteable data directory, development-only configuration, or a container running as root because nobody said otherwise. Modern .NET is genuinely cross-platform, but portability is engineered at publish time. It does not arrive by accident.

Know the publishing choices

Framework-dependent deployment assumes the target machine has the runtime:

dotnet publish src/OrderApi/OrderApi.csproj   --configuration Release   --output artifacts/order-api

Self-contained deployment carries the runtime and targets a runtime identifier:

dotnet publish src/OrderApi/OrderApi.csproj   --configuration Release   --runtime linux-x64   --self-contained true   -p:PublishSingleFile=true   --output artifacts/order-api-linux-x64

Framework-dependent output is smaller and easier to patch through runtime updates. Self-contained output is operationally predictable and useful when the host cannot install the runtime. Neither is morally superior; choose based on ownership of the host and patch process.

Runtime identifiers are a contract with the machine

linux-x64, linux-arm64, win-x64, and osx-arm64 are not interchangeable labels. A native dependency compiled for x64 will not run on ARM64. Build in CI for every runtime you ship, and smoke-test the artifact on that target or in the closest honest container. Cross-compilation confidence without execution evidence is a hope, not a release process.

Containers should be boring and small

A straightforward multi-stage Dockerfile looks like this:

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY OrderApi/OrderApi.csproj OrderApi/
RUN dotnet restore OrderApi/OrderApi.csproj
COPY . .
RUN dotnet publish OrderApi/OrderApi.csproj -c Release -o /app --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine
WORKDIR /app
ENV ASPNETCORE_URLS=http://0.0.0.0:8080
USER app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "OrderApi.dll"]

Do not run as root unless you can explain why. Pin base image tags at least by runtime major/minor, scan them, and rebuild when the runtime patches. Alpine is small but musl-based; if native dependencies misbehave, the Debian image may be the correct operational choice.

NativeAOT and trimming are sharp tools

NativeAOT reduces startup time and memory use, and trimming reduces deployment size. Both expose reflection, dynamic serialization, and plugin-style discovery problems. Annotate or design around reflection-heavy paths, test the published artifact rather than only dotnet run, and do not enable these modes because a benchmark post told you to. Compatibility evidence beats binary-size bravado.

Configuration must survive the artifact boundary

Do not bake environment-specific secrets into images. Bind-mount or inject configuration through platform-supported mechanisms. Set explicit environment names:

ASPNETCORE_ENVIRONMENT=Production
ConnectionStrings__Orders='Host=db.internal;Database=orders;Username=orders_app'

Also make the application write logs to stdout/stderr in containers. Writing to local files inside an ephemeral container is a slow way to lose the evidence you needed during the incident.

Field notes

  • dotnet publish defaults can change behavior across SDK versions. Read dotnet publish --help for the SDK in use and encode important flags explicitly.
  • Time zone data differs by base image. Alpine containers often need explicit timezone packages if business logic depends on local time zones.
  • globalization-invariant mode reduces image dependencies but changes culture-sensitive behavior. Do not discover that through customer names.
  • A single-file artifact is still not a static guarantee for every dependency. Native libraries and probing behavior deserve smoke tests.

Was this lesson helpful?

Sign in to rate