Back to results for “Project Structure”
.NET › CI/CD, Publishing & Deployment

What is the difference between dotnet publish's --self-contained and framework-dependent options in the context of container image builds?

Intermediate

Answer

For containerized deployments, framework-dependent publishing (the common default) relies on the base image already including the matching .NET runtime (e.g., the aspnet:8.0 image), producing smaller application layers since the runtime isn't duplicated in your app's own files. Self-contained publishing bundles the runtime into your application output itself, which is less useful in containers (since the base image typically already provides the runtime) but can be combined with a minimal runtime-deps or even scratch base image for highly specialized, ultra-minimal container scenarios.
# Framework-dependent (typical for containers, relies on base image's runtime)
FROM mcr.microsoft.com/dotnet/aspnet:8.0
COPY --from=build /app/publish .

# Self-contained (rare in containers, bundles runtime; could use a leaner base)
RUN dotnet publish -c Release -r linux-x64 --self-contained true -o /app/publish
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0  # no .NET runtime at all needed
Real-world example A team experimenting with ultra-minimal container images tests self-contained publishing against a runtime-deps base image (which has OS dependencies but no .NET runtime), achieving a smaller final image than the standard aspnet base for a specific CLI tool use case.

Common follow-ups: When does self-contained publishing actually help in containerized scenarios?;How does this interact with Native AOT for even smaller images?