Container build and runtime
Container build and runtime
A multi-stage container separates the tools that build an application from the smaller environment that runs it.
For a framework-dependent ASP.NET Core deployment, build with the .NET SDK image and copy published output into the ASP.NET Core runtime image for the same target platform. Choose reviewed immutable digests for both images; a major tag is not an immutable version. Specify framework-dependent or self-contained publishing explicitly: since .NET 8, selecting a RuntimeIdentifier does not by itself imply self-contained deployment. Check the target architecture and operating-system ABI, including glibc versus musl on Linux. A matching tag or runtime identifier is not proof that native dependencies can load. The supplied Dockerfile is a deliberately gated template with unresolved digest arguments. A nonroot runtime identity reduces privilege but does not replace isolation, vulnerability scanning or patching. Use explicit internal port 8080, a private lab network and loopback host mapping only. EXPOSE documents a port; it does not publish it. Set environment configuration explicitly, never bake credentials into layers or build arguments. Use a read-only filesystem with explicit writable mounts if required; verify permissions and resource limits. Kestrel can run behind Nginx or IIS; trust only known forwarding proxies, validate host and terminate TLS at an approved boundary. Multiple instances need a shared, protected Data Protection key store if they share cookie authentication, and compatible session/cache policies. Build, image scan, runtime smoke test and actual deployment are distinct checks.
Baseline: .NET 10 / C# 14. All C# results below are reasoned, not executed. Synthetic calculations are not performance measurements.
Resolve an explicit port setting
Parse an offline configuration value without opening a socket.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;static int Port(string? value) => int.TryParse(value, out int n) && n is >= 1024 and <= 65535 ? n : throw new ArgumentException("invalid lab port");foreach (string? value in new string?[] {"8080", "80", "bad", null}) { try { Console.WriteLine(Port(value)); } catch (ArgumentException) { Console.WriteLine("rejected"); }}Expected result (not executed):
8080rejectedrejectedrejectedThe minimum is this lab policy, not a universal Kestrel requirement. Missing configuration fails closed.
Select a trusted proxy explicitly
Model an exact peer-address allowlist. This is not a replacement for forwarded headers middleware.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;using System.Net;var trusted = IPAddress.Parse("192.0.2.10");foreach (string peer in new[] {"192.0.2.10", "192.0.2.11"}) Console.WriteLine(IPAddress.Parse(peer).Equals(trusted) ? "eligible proxy" : "ignore forwarded headers");Expected result (not executed):
eligible proxyignore forwarded headersDocumentation-only addresses are used. Configure actual KnownProxies and forwarding limits in an approved topology, and test spoofed headers from untrusted peers.
Check an image policy offline
A candidate passes only when immutable identity, nonroot and no host socket are declared. This does not inspect a real image.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;foreach (var image in new[] { (Name:"A", Digest:true, NonRoot:true, Socket:false), (Name:"B", Digest:false, NonRoot:true, Socket:false), (Name:"C", Digest:true, NonRoot:false, Socket:true) }) Console.WriteLine($"{image.Name}: {(image.Digest && image.NonRoot && !image.Socket ? "review candidate" : "blocked")}");Expected result (not executed):
A: review candidateB: blockedC: blockedThese are fictional declarations, not image attestations. Independently inspect actual image metadata, SBOM, signatures and advisories.
Apply the concept
Review the companion Dockerfile and propose a local smoke-test plan for a reviewed digest. Explain which stage needs an SDK, where port 8080 is used, and why a tag is insufficient.
Use the manual practice and knowledge check. Do not deploy or publish these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Container build and runtime — knowledge check
Apply the lesson concepts to the following scenarios.
Which stage needs compiler tools?
What does EXPOSE do?
Why use immutable digests?
Which forwarded headers are trusted?
Does nonroot guarantee security?