Courseiva

CKAD Application Design and Build Practice Question

Which TWO practices optimize Docker image size? (Select 2)

⚠ Common exam trap

A common pitfall tested in the CKAD exam is that running cleanup commands like `apt-get clean` in a separate RUN layer is ineffective; cleanup must occur in the same layer as the install to avoid persisting cached data in the intermediate layer.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Including a .dockerignore file to exclude unnecessary files

A `.dockerignore` file prevents unnecessary files (e.g., local logs, node_modules, .git) from being sent to the Docker daemon during the `docker build` context, reducing the build context size and avoiding bloating the image with irrelevant data. Option D is correct because multi-stage builds allow you to compile or install dependencies in a temporary stage and then copy only the final runtime artifacts (e.g., compiled binaries) into a minimal final image, discarding build tools and intermediate layers.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Running 'apt-get upgrade' in the Dockerfile

    Why it's wrong here

    Running 'apt-get upgrade' in a Dockerfile is an anti-pattern because it indiscriminately updates packages in the base image, producing a large new layer with outdated and updated file versions. Since the base image is already pinned, upgrading adds unpredictable changes and leaves package manager cache unless explicitly cleaned, which only inflates the image and violates reproducible builds.

  • ✗

    Using a full OS base image like ubuntu:latest

    Why it's wrong here

    Using a full OS base image like ubuntu:latest is wasteful because it includes a complete userspace with shells, libraries, and utilities irrelevant to most applications, directly increasing image size. The 'latest' tag also makes builds non-deterministic; a slim variant such as alpine or a distroless image provides only essential runtime binaries, reducing both size and attack surface.

  • ✓

    Including a .dockerignore file to exclude unnecessary files

    Why this is correct

    Including a .dockerignore file excludes unnecessary files such as .git, node_modules, or build outputs from the Docker build context, so the daemon never sends them. This prevents those bulky files from being accidentally copied into the image with COPY or ADD, directly reducing final image size and also substantially speeding up the build by shrinking context.

  • ✓

    Using multi-stage builds to copy only necessary artifacts

    Why this is correct

    Using multi-stage builds lets you compile or install dependencies in a temporary stage with full toolchains, then copy only the needed runnable artifacts into a minimal final stage based on a slim image. This keeps compilers, headers, and intermediate caches out of the final image, dramatically reducing its size while ensuring the shipped artifact is clean.

  • ✗

    Installing all packages in one layer without cleanup

    Why it's wrong here

    Installing all packages in a single RUN command without cleanup still leaves apt cache files and temporary downloads in the layer, because every file written during the command persists. While combining installs reduces layer count, it does nothing to remove leftovers; you must also run 'rm -rf /var/lib/apt/lists/*' in the same RUN to prevent those caches from inflating image size.

About these practice questions

Courseiva writes every CKAD question from scratch — 826 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This CKAD practice question is part of Courseiva's free CNCF certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the CKAD exam.