liveBook · Manning

Container image

As explained in .NET in Action, Second Edition

A container image is an immutable package containing an application, its dependencies, and the filesystem data needed to create containers.

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

How .NET in Action, Second Edition covers it

How deep it goes
Works through Dockerfile builds, multistage publishing, and CLI publishing, while treating built-in publishing as a basic image that still needs a Dockerfile for customization.
Examples
Uses an ASP.NET Core service, Dockerfile and .dockerignore listings, tagged docker build commands, and a dotnet publish command.
What it assumes
Assumes an existing .NET application and Docker Desktop; introduces Dockerfile instructions and image-building commands.
What you can do after
Build it from scratch with a Dockerfile, or apply it via dotnet publish, then distinguish image settings from runtime configuration.
How it connects
Connects images to containers, Dockerfiles, base images, build stages, port mapping, and application configuration.

Also explained in

Compare how 25 books explain container image →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.