skip to content

Dockerfile & Images

The build recipe: the instruction set from FROM through ENTRYPOINT, multi-stage builds, the build context, and the BuildKit engine that runs them. A Dockerfile is short enough to read in an interview and instantly shows how you think about size and root.

part ofDockeroverview, primer and where to startread it →
on this pageshow

questions

page 2 of 2

A Python service builds and runs fine on a Debian-based image, but after someone switches the Dockerfile's FROM instruction to an Alpine base the build gets much slower and a native library fails to load at runtime. Explain what is going on and how you would decide whether to stay on Alpine.

level: seniorimportance: should knowfreq 45%

basics

~20 s

Alpine uses musl libc instead of glibc, so precompiled binaries and Python manylinux wheels built for glibc do not apply. Package managers fall back to compiling from source, which is slow and needs toolchains, and mismatched native libraries fail to load. Unless you have verified the native dependency chain, prefer a glibc -slim base.

open as a page

A `docker buildx build` on a docker-container builder succeeds, yet `docker images` lists nothing new. Why, and how do you get the image out?

level: seniorimportance: should knowfreq 45%

basics

~20 s

The docker-container buildx driver runs BuildKit outside the engine's image store and exports nothing by default, so the result stays in the builder. Add --load to import it into the local engine, or --push to publish it.

open as a page

You must publish one container image tag that runs on both linux/amd64 and linux/arm64. How do you build and publish it with docker buildx, and what are the trade-offs between emulation and native or cross-compiled builds?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Use docker buildx build --platform linux/amd64,linux/arm64 --push, which produces one manifest list (image index) referencing a per-architecture image. The foreign architecture can run under QEMU emulation (simple but slow), on native builder nodes (fast, needs machines of both architectures), or via cross-compilation using BUILDPLATFORM and TARGETARCH (fastest, needs a cross-compiling toolchain).

open as a page

Describe the entrypoint wrapper-script pattern used by images such as the official Postgres image: what work belongs in that script, how it is wired up in the Dockerfile, and why the script must end with `exec "$@"`?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Set ENTRYPOINT to a small script and CMD to the real command. The script does start-up work — render config, fix permissions, first-run initialisation — then runs exec "$@", replacing itself with the CMD so the real process becomes PID 1 and receives signals.

open as a page

A team reports that `docker build` pauses for over a minute before the first instruction runs, and that any one-line source change rebuilds nearly everything. How would you diagnose and fix this?

level: seniorimportance: should knowfreq 44%

basics

~20 s

That pause is the build context being walked and transferred. Measure it with --progress=plain, find the bulk with du, and exclude version-control, dependency and build-output directories via .dockerignore at the context root — which also stops unrelated files from invalidating the COPY layer's cache.

open as a page

A Docker container exits immediately with `exec format error` on an arm64 host but runs on amd64. How do you diagnose and fix it?

level: seniorimportance: should knowfreq 38%

basics

~20 s

The kernel could not execute the binary because it was compiled for another architecture and no emulation handler is registered. Check the image's recorded architecture, rebuild the image for the host's platform, or run it with docker run --platform and accept the emulation cost.

open as a page

How does splitting a Dockerfile into several build stages change build parallelism and layer-cache invalidation compared with one long linear Dockerfile, and how should you order instructions inside each stage to benefit?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Stages form a dependency graph, not a list. Independent stages build concurrently, unreferenced stages are skipped, and a cache miss invalidates only the rest of that stage's chain — not sibling stages. Inside each stage, put rarely-changing steps first.

open as a page

What does the STOPSIGNAL instruction in a Dockerfile control, and when would you set it to something other than SIGTERM?

level: seniorimportance: should knowfreq 30%

basics

~20 s

STOPSIGNAL sets which signal the runtime sends to the container's main process on a stop request; the default is SIGTERM, and SIGKILL follows after the grace period. Change it when the application's graceful shutdown uses a different signal, as nginx does with SIGQUIT.

open as a page

How do you decide which buildx driver and exporter every team's image builds should use, from laptops to CI?

level: principalimportance: should knowfreq 36%

basics

~20 s

Decide by where the result must land and how insulated the build must be from the host: the built-in docker driver for laptop iteration, and a docker-container or remote builder in CI with push as the default exporter.

open as a page

What does the `# syntax=docker/dockerfile:1` line at the top of a Dockerfile do?

level: juniorimportance: nice to knowfreq 30%

basics

~20 s

It is a BuildKit parser directive naming the frontend image that parses the Dockerfile. BuildKit pulls docker/dockerfile:1 and uses it instead of its built-in parser, so newer Dockerfile syntax works without upgrading the Docker Engine.

open as a page

How do you make the image referenced by a Dockerfile's FROM instruction configurable at build time, and what is the scoping rule for an ARG declared before the first FROM?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

Declare ARG above the first FROM and use it in the FROM line, for example ARG BASE=debian:bookworm-slim then FROM ${BASE}. Such an ARG lives in a global scope usable only by FROM lines; to use its value inside a build stage you must re-declare a bare ARG with the same name after that stage's FROM.

open as a page

Your CI runs Docker builds on ephemeral runners, so every job starts with an empty builder and rebuilds everything. How would you design the build cache strategy, and what are the trade-offs of the options BuildKit gives you?

level: principalimportance: nice to knowfreq 28%

basics

~20 s

Export the build cache to somewhere shared. BuildKit supports --cache-to/--cache-from with an inline cache (embedded in the pushed image, final stage only), a registry cache (separate tag, mode=max keeps intermediate stages), or a local or cloud backend. Weigh cache hit rate against push/pull cost and storage, and scope caches per branch and per platform.

open as a page

You own the shared base images for dozens of services. How would you standardise the ENTRYPOINT/CMD contract, PID 1 behaviour, and shutdown-signal handling across all of them, and what trade-offs would you weigh?

level: principalimportance: nice to knowfreq 26%

basics

~20 s

Mandate exec-form instructions, decide one convention for whether ENTRYPOINT is the app or a thin wrapper, guarantee the real process ends up as PID 1 with a SIGTERM handler, standardise grace periods against measured drain time, and enforce it with lint plus an automated termination test.

open as a page

In a monorepo with a dozen deployable services that share internal libraries, how would you structure build contexts and ignore files so each service image builds from the smallest correct input, and what trade-offs would you weigh?

level: principalimportance: nice to knowfreq 24%

basics

~20 s

Pick one of three shapes: per-service contexts (fast, but shared code must be published), one root context with per-Dockerfile ignore files (simple, pays the walk), or a small context plus BuildKit named contexts for shared libraries. Optimise for cache hit rate and transfer cost, and enforce the ignore files automatically.

open as a page

Your Docker images must now run on both arm64 and amd64 hosts. How do you decide which ones ship both, and keep the two from diverging?

level: principalimportance: nice to knowfreq 27%

basics

~20 s

Decide per image from where it actually runs, not by default: dual-architecture for shared base images and anything developers run locally, single-architecture where a workload is pinned to one host pool. Then test each architecture on native hardware, because build success is not behavioural parity.

open as a page

For a fleet of services built in CI, when is a single multi-stage Dockerfile per service the right structure, versus a shared prebuilt builder image, versus having an external build system hand the Dockerfile a finished artifact? What tradeoffs drive that call?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Self-contained multi-stage Dockerfiles maximise reproducibility and local parity but rebuild toolchains everywhere. A shared prebuilt builder image centralises the toolchain and speeds cold builds at the cost of a versioned dependency. Copying in an externally built artifact is fastest but loses hermeticity.

open as a page

showing 31–47 of 47