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
Which Dockerfile instruction executes a command during the build and records the result as a new layer?
- RUN
- CMD
- FROM
- 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.
Which are benefits or features of multi-stage builds?
- The final image can exclude build tools used in earlier stages
- Stages are named with FROM ... AS name
- COPY --from copies files out of an earlier stage
- Each stage must be a separate Dockerfile
- 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: 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.
