Start learning free

LESSON 150 OF 173 · BACKEND: SHIPPING IT

A Dockerfile: The Base Image

Practice

To run the shop the same way on any server, you pack it into a Docker image: a snapshot of a small Linux system with .NET, the packages and your code in it. A container is that image running. The recipe for an image is a file named Dockerfile, one instruction per line. Here you write its lines as the C# list of strings dockerfile: one string per line of the file, each followed by a comma.

A Dockerfile starts with FROM, the image you build on, written name:tag: the tag after the colon is the version. WORKDIR picks the folder where the next lines work and the app runs later; Docker creates it if it is missing. ENV sets an environment variable, the kind Environment.GetEnvironmentVariable reads, written NAME=value. Here is the start of a Dockerfile for another program, built on Node.js:

var dockerfile = new List<string> {
    "FROM node:22-slim",
    "WORKDIR /srv",
    "ENV TZ=UTC",
};

The .NET images live on Microsoft's own registry, so their names start with mcr.microsoft.com. dotnet/sdk holds the tools that build an app and run it, while dotnet/aspnet only runs an app that is already built. The shop is built inside the image, so it needs the SDK. One variable matters for ASP.NET Core in a container: ASPNETCORE_HTTP_PORTS, the port the shop listens on. The .NET images set it to 8080, and the shop will use 8000.

The check reads your lines the way docker build would. When they are right, it prints:

$ docker build -t shop .
[1/3] FROM mcr.microsoft.com/dotnet/sdk:8.0: starts from a Debian Linux with the .NET 8.0 SDK installed: the tools that build the shop and run it
[2/3] WORKDIR /app: makes the folder /app and works in it; the next lines and the app run there
[3/3] ENV ASPNETCORE_HTTP_PORTS=8000: sets ASPNETCORE_HTTP_PORTS for every program in the image, so the shop listens on port 8000 (the .NET images set 8080)
The base is ready: mcr.microsoft.com/dotnet/sdk:8.0, working in /app. The files of the shop come next.

Your task: fill the list dockerfile with three lines: start FROM Microsoft's .NET SDK image, mcr.microsoft.com/dotnet/sdk, version 8.0; work in the folder /app; and set ASPNETCORE_HTTP_PORTS to 8000.


Assignment
Follow the Coding Tutorial to write the first three lines of the shop's Dockerfile: FROM, WORKDIR and ENV.


Hint
Each string in the list is one line of the file, with a comma after it. Write the image's full name, registry included, then a colon and the tag. Write the folder as a full path that starts with /, and NAME=value with no spaces around =.


STEP 1 OF 3
    Ctrl + Enter to check