Typorithm · easy

Writing a Dockerfile

dockerfiledevopslayersbuild
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Writing a Dockerfile

0%

Images

A Dockerfile declares how to build an image: FROM picks a base, WORKDIR sets the directory, COPY brings files in, RUN executes build steps, and CMD is the default process. Each instruction is a cached layer, so copy dependency manifests and install before the source to maximize cache hits.

A Dockerfile is a recipe for building an image. FROM chooses the base image, WORKDIR sets the directory commands run in, COPY brings files from your context into the image, and RUN executes build-time commands.

Every instruction produces a layer, and Docker caches layers. If an instruction and everything before it are unchanged, the cached layer is reused, so ordering matters for build speed.

Copying package*.json and running npm ci before COPY . . means the dependency layer only rebuilds when the manifests change, not on every source edit. CMD sets the default process the container runs.

01

FROM sets the base; CMD sets the default process.

02

Each instruction is a cached layer.

03

Copy manifests and install before the source.

FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["node", "server.js"]

Read the implementation

Connect each part of the code to the concept before you type.

1. Base image

FROM picks the base image every later instruction builds on top of.

FROM node:22-alpine

2. Working directory

WORKDIR sets the directory for the following COPY, RUN, and CMD instructions.

WORKDIR /app

3. Copy manifests

Copying just the package manifests first lets the install layer stay cached when only source changes.

COPY package*.json ./

4. Build step

RUN executes a command during the build, here installing dependencies into a new layer.

RUN npm ci

5. Default process

CMD declares the default command the container runs when it starts.

CMD ["node", "server.js"]

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Writing a Dockerfile: Lesson & Practice · Typorithm | ApplyDjinn