Docker & Containers

Images, Layers & Dockerfiles

Build images with Dockerfiles and understand the layer cache.

40 min read beginner 3 objectives

Status

Not started

What you will learn

  • Write a Dockerfile
  • Understand layer caching
  • Order instructions for cache hits

New to this? Start here

The basics, in plain English

An image is built from a recipe called a Dockerfile. The Dockerfile lists steps: start from a base, copy your code in, install things. Each step becomes a cached layer, which makes rebuilds fast.

Dockerfile
A text recipe with step-by-step instructions for building an image.
Base image
The starting point you build on, such as a minimal Linux or a Node.js image.
Layer
One step’s result, saved and reused so unchanged steps do not rebuild.
Build
The act of turning a Dockerfile into a finished image.
Tag
A label like myapp:v1 that names a specific version of an image.
01

Layers

Each Dockerfile instruction creates a cached layer. Order from least to most frequently changing so dependency installs stay cached.

Try it yourself

dockerfile
FROM node:20-slim
Start from a small official Node.js base image.
WORKDIR /app
Set the working directory inside the image.
COPY package*.json ./
Copy just the manifests first to cache dependencies.
RUN npm ci
Install exactly the locked dependency versions.
COPY . .
Copy the rest of the source code in.
CMD ["node", "server.js"]
The command that runs when the container starts.
↳ lines explain what each command does — only the commands get copied

Finished this topic?

Mark it done to earn 100 XP and keep your streak alive.