Writing a Dockerfile

The instructions FROM, WORKDIR, COPY, ADD, RUN, ENV, ARG, EXPOSE, USER, CMD and ENTRYPOINT; the build context and .dockerignore; one layer per instruction, the build cache and docker build -t.

  • 30 cards
  • 6 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The steps
  5. Scenarios
  6. Understand

A few cards from the deck

  • Dockerfile: what is it?

    Show the answer

    A text file that describes, instruction by instruction, how to build an image from a base image.

    It makes the build reproducible and versionable: whoever runs docker build gets the same image, instead of depending on the steps of whoever made it.

  • What is the difference between COPY and ADD?

    • ADD can also unpack a local archive and download a URL
    • COPY only copies single files, whereas ADD also copies folders
    • COPY runs when the container starts, ADD when the image is built
    • ADD adds a file to the image leaving the original in the context, COPY moves it
    Show the answer

    ADD can also unpack a local archive and download a URL

    Both copy from the context into the image, folders included. ADD has two extra behaviours, sometimes surprising; COPY is preferred when you only want to copy.

  • The … instruction runs when the image is built, whereas … defines the command run when the container starts.

    Show the answer

    The RUN instruction runs when the image is built, whereas CMD defines the command run when the container starts.

    RUN npm ci installs the dependencies once and for all in a layer; CMD ["node", "server.js"] only runs at docker run.

  • Image layer: what is it?

    Show the answer

    The set of file-system changes produced by one build instruction, stacked on the previous ones.

    RUN, COPY and ADD instructions each create a layer. Layers are read-only and shared between images, which saves downloads and disk.

  • A Node Dockerfile that benefits from the cache: which step comes after "FROM node:24-alpine"?

    • WORKDIR /app
    • COPY package*.json ./
    • RUN npm ci
    • COPY . .
    Show the answer

    WORKDIR /app

    The base image first, then the working folder, then what rarely changes (dependencies), then what often changes (the code), and the start command last.