A container image packages an application with the filesystem and dependencies needed to create a container. It provides a predictable runtime instead of relying on whichever .NET version happens to be installed on a host. Images are immutable: to change one, start from a base image, make the changes, and build a new image. section 13.1 section 13.3.1
There are two routes to a .NET image:
- Use a Dockerfile for .NET 6 and earlier, or when the image needs customization. FROM selects a base image; COPY, WORKDIR, EXPOSE, and ENTRYPOINT prepare and run the application. Keep local bin and obj artifacts out with .dockerignore, then build and tag the image with docker build --tag asciiartsvc . section 13.3.1
- For .NET 7 and later, publish a basic image with dotnet publish /t:PublishContainer --os linux --arch x64. Set image properties in the project file; use -c Release for a production build. Projects without the required container-build support may need Microsoft.NET.Build.Containers. section 13.3.2
Listing 13.7 Dockerfile with named stages
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-stage #1 WORKDIR /src #2 COPY AsciiArtSvc.csproj . #3 RUN dotnet restore #4 COPY . . #5 RUN dotnet publish -o /publish FROM mcr.microsoft.com/dotnet/aspnet:6.0 WORKDIR /publish COPY --from=build-stage /publish . #6 EXPOSE 80 ENTRYPOINT ["dotnet", "AsciiArtSvc.dll"]
This Dockerfile builds and publishes with the .NET SDK, then copies the published output into an ASP.NET runtime image. Copying the project file and restoring before copying the remaining source separates those build steps. section 13.3.1
An image also needs runtime settings that suit a container. For a web service, a localhost-only launch URL is unsuitable; the Docker launch profile uses http://*:5101. Exposing a port in an image is distinct from optionally mapping that port when starting its container. Avoid baking environment-specific values into separate images: configuration can vary by environment, while secrets require separate handling rather than being added to an immutable image. section 13.3 section 13.3.1 section 13.4 section 13.6
























