Start learning free

LESSON 151 OF 173 · BACKEND: SHIPPING IT

Install Before You Copy

Practice

The base image has .NET, but nothing of the shop. COPY copies files from the shop's folder on your computer, where you run docker build, into the image: COPY source destination. A . as the destination is the image's current folder, /app thanks to WORKDIR. RUN runs a command inside the image while it is being built, and keeps what the command changed. dotnet restore downloads the packages listed in the project file, shop.csproj, and dotnet publish compiles the shop into a folder, ready to run.

Each line of a Dockerfile makes a layer, a saved change to the image, and Docker keeps the layers in a cache. On the next build it reuses a layer, CACHED, as long as the line and the files it copies have not changed. From the first line that changed, every line runs again. So the order matters: what changes rarely goes at the top, what changes often at the bottom.

Here is the same idea for a program built on Node.js: its package list, package.json, is copied and installed first, and the code last.

var dockerfile = new List<string> {
    "FROM node:22-slim",
    "WORKDIR /srv",
    "COPY package.json .",
    "RUN npm install",
    "COPY . .",
};

The check builds your image, then changes Program.cs and builds again. When your lines are right, it prints:

$ docker build -t shop .
[1/7] FROM mcr.microsoft.com/dotnet/sdk:8.0
[2/7] WORKDIR /app
[3/7] ENV ASPNETCORE_HTTP_PORTS=8000
[4/7] COPY shop.csproj .: copies shop.csproj into /app
[5/7] RUN dotnet restore: downloads Microsoft.Data.Sqlite 8.0.11 and the packages it needs
[6/7] COPY . .: copies Program.cs, shop.csproj, Dockerfile into /app
[7/7] RUN dotnet publish -c Release -o out: compiles the shop into /app/out/shop.dll
In the image: /app/shop.csproj, /app/Program.cs, /app/Dockerfile, /app/out/shop.dll
You change Program.cs and build again:
[1/7] FROM mcr.microsoft.com/dotnet/sdk:8.0: CACHED
[2/7] WORKDIR /app: CACHED
[3/7] ENV ASPNETCORE_HTTP_PORTS=8000: CACHED
[4/7] COPY shop.csproj .: CACHED
[5/7] RUN dotnet restore: CACHED
[6/7] COPY . .: runs again, Program.cs changed
[7/7] RUN dotnet publish -c Release -o out: runs again, because a line above it ran again
dotnet restore was CACHED: a change to the code does not download the packages again

Your task: after the three lines of the base, add four lines to the list dockerfile: copy shop.csproj into the current folder, download the packages with dotnet restore, copy the whole shop folder into the current folder, then compile the shop with dotnet publish -c Release -o out. A change to Program.cs must leave the restore CACHED.


Assignment
Follow the Coding Tutorial to add the COPY, RUN, COPY and RUN lines to the shop's Dockerfile, in that order.


Hint
In COPY source destination, a dot as the source means everything in the shop's folder, and a dot as the destination means /app. dotnet restore can only read shop.csproj once a line above it has copied the file in.


STEP 1 OF 4
    Ctrl + Enter to check