liveBook · Manning

Docker

As explained in .NET in Action, Second Edition

Docker is a platform and command-line tool for building, distributing, and running containerized applications.

Docker provides tools for building container images and running containers locally. Containers predate Docker, and production hosting can use other platforms, including Kubernetes. Docker remains a practical way to host .NET containers during development, even though .NET 7 and later can create container images without Docker’s build tools. section 13.2

The Docker command-line tool reads a Dockerfile to build an image. A Dockerfile contains comments and instructions; it begins with FROM, which selects a base image. Because images are immutable, modifications produce a new image rather than changing the base. section 13.3.1

For a .NET 6 service, a Dockerfile can specify:

  • An SDK base image, a copied source directory, and a working directory. section 13.3.1
  • An exposed HTTP port and an ENTRYPOINT that starts the application. section 13.3.1
  • A separate build stage that publishes the application, followed by an ASP.NET image that receives the published files. section 13.3.1
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 named-stage Dockerfile restores and publishes the service in an SDK image, then copies the published output into an ASP.NET image for execution. section 13.3.1

Build and tag a service image

From the service project directory, run `docker build -t asciiartsvc:latest -t asciiartsvc:0.1 .`. Keep local build output out of the image with a `.dockerignore` file containing `/bin/` and `/obj/`. The first build may need to retrieve base images; later builds can use cached images. section 13.3.1

How .NET in Action, Second Edition covers it

How deep it goes
Works through Dockerfile instructions, ignored files, tagging, and multi-stage builds without detailing Docker’s container internals.
Examples
Uses a .NET 6 service Dockerfile, a `.dockerignore` file, build-and-tag commands, and a named-stage publishing Dockerfile.
What it assumes
Assumes service project code and an installed, working Docker command; introduces the required Dockerfile and `.dockerignore` files.
What you can do after
Apply it via the Docker CLI to build and tag a .NET service image from a Dockerfile.
How it connects
Connects Docker to container images, Dockerfiles, .NET publishing, image caching, and Kubernetes as a production-hosting alternative.

Also explained in

Compare how 26 books explain Docker →

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.