Skip to article content
FrontendXLesson 05 / 16

Container images and layers

Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.

intermediate · about 55 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Distinguish an image from a running container and describe image layers.
  • Explain how the build cache decides whether a layer is rebuilt.
  • Choose base images and tags that balance size, security and reproducibility.

Overview

#

A container image is a read-only template made of stacked layers plus metadata. A container is a running instance with a thin writable layer on top. Images are built from a Dockerfile, stored in a registry, and pulled by tag.

Layers and caching

Each instruction that changes the filesystem creates a layer. The builder caches a layer and reuses it when the instruction and all parent layers are unchanged. Order instructions from least to most frequently changing so cache hits are maximised.

snippet.docker
FROM node:20-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "server.js"]

Copying the manifests and installing dependencies before copying source means a source-only change still reuses the dependency layer.

Tags and digests

A tag such as node:20-alpine is a mutable pointer; the digest such as node@sha256:... is immutable. For reproducible builds, pin the digest or at least a specific version. The latest tag can change under you.

Registries

A registry stores images by repository and tag. docker push uploads layers the registry does not already have; docker pull downloads only missing layers. Layer deduplication is why sharing a base image across services is fast.

Common pitfalls

  • Copying source before installing dependencies, which invalidates the dependency cache on every code change.
  • Using latest, so two builds of the same commit can differ.
  • Shipping build tools and dev dependencies in the final image, inflating size and attack surface.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    Given the Dockerfile above, you edit only server.js and rebuild. Which layers are rebuilt?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.docker
    FROM node:20-alpine
    WORKDIR /app
    COPY package.json package-lock.json ./
    RUN npm ci --omit=dev
    COPY . .
    CMD ["node", "server.js"]
    
    Choose the best answer
  2. Exercise 2Implement

    Write a multi-stage Dockerfile that builds a TypeScript project in one stage and copies only the compiled output and production dependencies into a slim final image.

    starter.dockerfile
    # stage 1: build; stage 2: runtime
    

    Reveal the reference answer
    reference.dockerfile
    FROM node:20-alpine AS build
    WORKDIR /app
    COPY package.json package-lock.json ./
    RUN npm ci
    COPY . .
    RUN npm run build
    
    FROM node:20-alpine AS runtime
    WORKDIR /app
    ENV NODE_ENV=production
    COPY package.json package-lock.json ./
    RUN npm ci --omit=dev
    COPY --from=build /app/dist ./dist
    CMD ["node", "dist/server.js"]

    Authored expected output: A final image containing dist and production node_modules only, without the TypeScript toolchain.

    Multiple FROM stages let the heavy build toolchain live in an intermediate stage that is discarded, so the shipped image is smaller and has fewer packages to patch.

  3. Exercise 3Debug

    Two builds of the same commit produced different image digests and one is missing a dependency. The base image tag is node:latest. What is the cause and fix?

    starter.dockerfile
    FROM node:latest
    

    Reveal the reference answer
    reference.dockerfile
    node:latest is a moving tag, so the two builds started from different base images. Pin a specific version or digest, for example FROM node:20.11.1-alpine@sha256:..., and commit the lockfile so dependency resolution is deterministic.

    Authored expected output: Both builds start from the same base and resolve the same dependencies, producing identical digests.

    Reproducibility requires every input to be pinned. A floating base tag is an input that silently changes.

Common misconceptions

#
  • Changing a line in the Dockerfile only rebuilds that line: every later layer is rebuilt too.
  • A tag uniquely identifies an image: tags are mutable and can point to a different image later.

Follow-up practice

#
  • devops-docker-workflow

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.