Dockerfile Best Practices Questions with Answers

17 practice questions on Dockerfile Best Practices (DevOps & Kubernetes) with answers and step-by-step solutions. Try 10 samples below, then practise all 17 free in the AptiNest app.

Sample questions

Question 1Easy

A Go service is compiled in one stage and copied into a tiny final stage. What does this multi-stage build achieve?

  1. It runs several containers at once
  2. It splits one image into several registries automatically, one per stage
  3. It builds faster by skipping the cache
  4. The final image ships only runtime files, not build tools

Answer: D. The final image ships only runtime files, not build tools

Explanation

Compilers, dev dependencies and sources stay out of the final stage, giving smaller, safer images.

Step-by-step solution

  1. The build stage contains compilers and build tools and produces the binary.
  2. The final stage copies in only the binary or other runtime files.
  3. Build tools and source files are left out of the final stage, so the image is smaller and safer.
  4. The correct answer is option D, the final image ships only runtime files, not build tools.

Why the other options are wrong

  • A. A multi-stage build does not run several containers at once.
  • B. A build produces one image; the stages do not split it into several registries.
  • C. A multi-stage build does not skip the cache; its benefit is a smaller final image.

Tip: Build in a full stage, run in a slim final stage.

Question 2Easy

What is the role of WORKDIR in a Dockerfile?

  1. Sets the user's home directory on the host
  2. Copies the whole build context into the image at that path before each RUN
  3. Sets the working directory for later steps
  4. Creates a volume

Answer: C. Sets the working directory for later steps

Explanation

Later RUN, COPY, CMD and ENTRYPOINT use this directory; it is created if missing.

Step-by-step solution

  1. WORKDIR sets the default working directory for commands in Dockerfile.
  2. Later RUN, COPY, CMD, and ENTRYPOINT use this directory.
  3. It is created if it does not exist.
  4. This makes it easier to organize files and run commands.
  5. Final answer: Sets the working directory for later steps.

Why the other options are wrong

  • A. WORKDIR does not set the home directory of the host user; it sets a path inside the image.
  • B. WORKDIR does not copy the build context; COPY does that, and only for the files it names.
  • D. WORKDIR does not create a volume; volumes come from VOLUME or runtime mounts, not this instruction.

Tip: Use WORKDIR to set a base directory for all following commands.

Question 3Easy

What does EXPOSE 8080 in a Dockerfile do?

  1. Publishes port 8080 on the host
  2. Forces the app to listen on 8080 even if it is configured otherwise
  3. Opens the host firewall
  4. Documents the port; it does not publish it

Answer: D. Documents the port; it does not publish it

Explanation

Publishing needs -p or an orchestrator setting; EXPOSE is metadata.

Step-by-step solution

  1. EXPOSE records which port the app listens on.
  2. It is metadata only, so it does not open or publish the port.
  3. Publishing needs -p at run time or an orchestrator setting.
  4. The correct answer is option D, it documents the port and does not publish it.

Why the other options are wrong

  • A. Publishing to the host needs -p at run time; EXPOSE does not do that.
  • B. EXPOSE does not change which port the app listens on.
  • C. EXPOSE does not touch the host firewall.

Tip: EXPOSE is documentation; -p publishes the port.

Question 4Easy

What does a .dockerignore file do?

  1. Excludes files from the build context
  2. Lists containers Docker should not start
  3. Ignores errors during the build
  4. Marks layers that should not be cached between builds on the same machine

Answer: A. Excludes files from the build context

Explanation

Excluding node_modules, .git and secrets keeps builds faster and avoids leaking files into images.

Step-by-step solution

  1. A .dockerignore file tells Docker which files to exclude from the build context.
  2. Excluded files are not sent to the Docker daemon, making builds faster.
  3. This prevents sensitive or unnecessary files from being included in the image.
  4. Excluding node_modules, .git and secrets keeps builds faster and avoids leaking files into images.
  5. Final answer: Excludes files from the build context.

Why the other options are wrong

  • B. A .dockerignore does not list containers; it only controls which files go into the build context.
  • C. It does not hide build errors; a failing step still stops the build as before.
  • D. Layer caching is not controlled by .dockerignore; it only filters files from the build context.

Tip: Use .dockerignore to speed up builds and secure your images.

Question 5Medium

Why add a USER instruction to switch to a non-root user in a Dockerfile?

  1. Docker refuses to start images that run as root unless --privileged is passed
  2. To limit damage if the app is compromised
  3. Root users cannot run network servers
  4. It makes the image smaller

Answer: B. To limit damage if the app is compromised

Explanation

Running as root inside the container increases the impact of a breakout or exploit; least privilege is safer.

Step-by-step solution

  1. By default, many containers run their processes as root.
  2. If the app is compromised, an attacker gets root privileges inside the container.
  3. A non-root USER instruction limits what the attacker can do.
  4. The correct answer is option B, to limit damage if the app is compromised.

Why the other options are wrong

  • A. Docker does not refuse to start images that run as root, and --privileged is not required.
  • C. Root users can run network servers; being root is not a requirement for that.
  • D. The USER instruction does not change the image size.

Tip: Run as a non-root USER; least privilege limits the damage of a breach.

Question 6Medium

ARG vs ENV in a Dockerfile:

  1. ENV is only for build time; ARG persists into running containers
  2. Both are visible only at runtime
  3. ARG is build-time only; ENV persists at runtime
  4. ARG values are encrypted in the image metadata

Answer: C. ARG is build-time only; ENV persists at runtime

Explanation

Use ARG for build parameters and ENV for runtime configuration defaults.

Step-by-step solution

  1. ARG is used to pass build-time variables to the Dockerfile.
  2. ENV sets environment variables that are available at runtime.
  3. ARG values do not persist in the final image.
  4. ENV values are written into the container's environment and last through runtime.
  5. So, ARG is build-time only; ENV persists at runtime.

Why the other options are wrong

  • A. ENV is not build-time only; ENV values persist into running containers, while ARG is build-time only.
  • B. ARG is not visible at runtime; only ENV values are available to the running container's environment.
  • D. ARG values are not encrypted; they are plain text in the build history and should not hold secrets.

Tip: Remember: ARG disappears after building, ENV stays with the container.

Question 7Medium

COPY vs ADD in a Dockerfile:

  1. COPY can download from URLs; ADD cannot
  2. ADD is the newer replacement for COPY and should be used everywhere
  3. They are identical in every case
  4. Prefer COPY; ADD also fetches URLs and unpacks tars

Answer: D. Prefer COPY; ADD also fetches URLs and unpacks tars

Explanation

ADD's extra behaviours can surprise; COPY is explicit and recommended for plain file copies.

Step-by-step solution

  1. COPY copies local files.
  2. ADD can also fetch URLs and extract tars.
  3. Best practice is to use COPY unless you need that extra behaviour.

Why the other options are wrong

  • A. It is ADD that fetches URLs.
  • B. ADD is not preferred.
  • C. They differ.

Tip: Prefer COPY.

Question 8Medium

Why write RUN apt-get update && apt-get install -y curl in a single RUN?

  1. It makes the package download faster
  2. Because separate RUNs are not allowed
  3. So a stale cached 'update' layer is never reused
  4. apt-get refuses to run in more than one layer of the same image during a build

Answer: C. So a stale cached 'update' layer is never reused

Explanation

If update is its own cached layer, a later change to the install line could reuse a stale package index.

Step-by-step solution

  1. Combining commands ensures no cached update layer is reused.
  2. A separate RUN would cache the update step.
  3. Stale cache can lead to incorrect package installation.
  4. Merging avoids reusing outdated package lists.
  5. Final answer: So a stale cached 'update' layer is never reused.

Why the other options are wrong

  • A. The single RUN is not about download speed; it is about cache reuse so stale package lists are avoided.
  • B. Separate RUN lines are allowed; the reason to combine them is cache behaviour, not a rule.
  • D. apt-get can run in several layers; the problem is that a cached update layer can be reused and go stale.

Tip: Merge update and install to avoid stale package caches.

Question 9Hard

CMD ["node", "server.js"] (exec form) is preferred over CMD node server.js (shell form) because:

  1. The app runs as PID 1 and receives SIGTERM
  2. Exec form uses less disk space
  3. Shell form is not valid Dockerfile syntax
  4. Shell form runs the command twice: once at build time and once at runtime

Answer: A. The app runs as PID 1 and receives SIGTERM

Explanation

Shell form wraps the process in /bin/sh -c, which may not forward stop signals, so containers fail to shut down gracefully.

Step-by-step solution

  1. In exec form, the app is started directly and becomes PID 1 in the container.
  2. Docker stop sends SIGTERM to PID 1, so the app can shut down cleanly.
  3. Shell form wraps the command in /bin/sh -c, which may not forward the signal to the app.
  4. The correct answer is option A, the app runs as PID 1 and receives SIGTERM.

Why the other options are wrong

  • B. Disk space is not the reason; both forms store the same command text.
  • C. Shell form is valid Dockerfile syntax.
  • D. The CMD command does not run at build time; it runs once when the container starts.

Tip: Use exec form CMD [...] so the app gets stop signals and shuts down cleanly.

Question 10Hard

Why must you not pass a secret token with ARG and use it in a RUN step?

  1. ARG cannot hold strings
  2. Build args can show up in image history
  3. RUN steps cannot read ARG values at all, so the token would be empty at build time
  4. ARG values are uploaded to every registry

Answer: B. Build args can show up in image history

Explanation

Values used in layers can leak via docker history; use build-time secret mounts instead.

Step-by-step solution

  1. A value passed with ARG and used in a RUN step is recorded in the image build history.
  2. Docker history can show those values to anyone who has the image.
  3. So the token can leak even if a later step deletes it.
  4. The correct answer is option B, build args can show up in image history.

Why the other options are wrong

  • A. ARG can hold strings; the problem is the history, not the type of value.
  • C. RUN steps can read ARG values, so the token is not empty at build time.
  • D. ARG values are not uploaded to every registry; they stay in the image history.

Tip: Use build-time secret mounts for secrets, never ARG or ENV.

Practise all 17 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all DevOps & Kubernetes topics · All topics