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

























