Bitzu

KCNA ยท Containers and images

Building images with a Dockerfile

Dockerfile instructions (FROM, RUN, COPY, CMD vs ENTRYPOINT), layer caching, multi-stage builds, small base images.

Key points

  • A Dockerfile is a text recipe for building an image.
  • FROM picks the base image, RUN executes build commands and adds a layer, COPY brings in files.
  • ENTRYPOINT is the executable; CMD is its default arguments.
  • Layers are cached, so put rarely changing steps first.
  • Multi-stage builds keep build tools out of the final image.

Source: https://docs.docker.com/reference/dockerfile/

Practice questions

Single choice

Which Dockerfile instruction executes a command during the build and records the result as a new layer?

  1. RUN
  2. CMD
  3. FROM
  4. ENTRYPOINT
Show answer

Answer: A. RUN

RUN runs at build time. CMD only declares what runs at container start and does not execute during the build.

Select all that apply

Which are benefits or features of multi-stage builds?

  1. The final image can exclude build tools used in earlier stages
  2. Stages are named with FROM ... AS name
  3. COPY --from copies files out of an earlier stage
  4. Each stage must be a separate Dockerfile
  5. Each stage runs on a different node of the cluster
Show answer

Answer: A. The final image can exclude build tools used in earlier stages, B. Stages are named with FROM ... AS name, C. COPY --from copies files out of an earlier stage

A later stage can copy artifacts from a named earlier stage with COPY --from, leaving compilers and sources behind and giving a smaller final image. Multiple Dockerfiles are not required, and stages are not separate hosts.

True or false

True or false: arguments given after the image name in docker run replace an exec-form ENTRYPOINT.

Show answer

Answer: False

False. They replace CMD and are appended to an exec-form ENTRYPOINT; the ENTRYPOINT itself stays unless --entrypoint is used.

Practice 8 questions on this topic in Bitzu

Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.

Download on the App StoreGet it on Google Play

More KCNA topics in this module