Containerization and Docker Fundamentals Questions
Packaging applications into containers: images and layers, Dockerfiles, registries, image optimization and security, container networking and storage, and the container runtime model. Covers how containers differ from virtual machines, image build and management, and the fundamentals that underpin any orchestration platform. The container primitive before orchestration.
How do you set CPU and memory limits for a Docker container, both at runtime (--memory, --cpus, --memory-swap) and in Compose? Why do these limits matter for reliability when many containers share a host, and what's the practical difference between limiting resources in Docker versus relying on the host OS alone?
Sample Answer
Direct answer
At runtime you cap a container's resource use with --memory (a hard cap on RAM the container's cgroup, the Linux kernel mechanism that accounts and limits a group of processes' resource use, may use), --cpus (a fractional cap on CPU cores, for example 1.5 for one and a half cores' worth of CPU time), and --memory-swap (the combined cap on RAM plus swap; setting it equal to --memory disables additional swap for that container entirely). Compose expresses the same caps declaratively under a service's deploy.resources.limits (and, for guaranteed minimums, reservations). These limits matter because a host is shared: without them, one noisy or leaking container can starve every other container's CPU or trigger the kernel's out-of-memory killer against processes that had nothing to do with the problem. Setting limits in Docker, rather than trusting the host OS alone, means each container's ceiling is explicit, enforced per-container by the kernel's cgroup mechanism, and visible in the container's own configuration, instead of being an emergent, unpredictable property of whatever else happens to be running alongside it.
Structured elaboration
The three runtime flags
--memory=256m: a hard ceiling on the container's RAM. Exceeding it does not throttle the container gracefully; the kernel's out-of-memory killer terminates a process inside it, which is why containers hitting this limit exit with code 137 (128 plus signal 9,SIGKILL).--cpus=1.5: a fractional CPU cap enforced by the kernel's CPU scheduler quota mechanism (internally, a quota and period pair, for example 150 milliseconds of CPU time allowed per 100-millisecond period, which works out to one and a half cores' worth of time). The container can still use more than one core briefly if the host has spare capacity within that period, but its average use over time is capped.--memory-swap: the ceiling on RAM plus swap combined. Set it equal to--memoryto give the container no extra swap at all (predictable, fails fast under memory pressure instead of degrading into slow, swapped-out behavior); leave it unset or higher to allow some swap as a cushion, at the cost of unpredictable latency when the container is actually swapping.
The same caps in Compose
services:
app:
image: myapp:latest
deploy:
resources:
limits:
cpus: "1.5"
memory: 256M
reservations:
memory: 128M
limits is the hard ceiling, equivalent to the runtime flags above. reservations is a soft guarantee used for scheduling decisions (more relevant in a multi-node Swarm context, but harmless to declare locally). As of current Compose versions, plain docker compose up applies deploy.resources.limits directly to the container, the same as if you had passed --memory and --cpus on the command line; this is worth knowing explicitly because older documentation associated deploy.resources with Swarm mode only.
Why this matters for reliability with many containers on one host
- CPU is a compressible resource: a container that goes over its fair share slows its neighbors down (added latency) but does not crash them. Memory is not compressible: a container that overshoots gets killed, and without a per-container ceiling, the kernel's OOM killer picks a victim using its own heuristics across the whole host, which can land on an unrelated, otherwise healthy container instead of the actual offender.
- A hard memory limit turns "some process on this host eventually gets killed, chosen by a heuristic" into "this specific container gets killed, predictably, when it exceeds the budget you gave it," which is a much easier failure mode to detect, alert on, and attribute correctly.
Docker limits versus relying on the host OS alone
Without container-level limits, isolation depends entirely on however many containers happen to be co-located and how they individually behave that day; a single runaway process can degrade or take down everything sharing the host, and there is no per-workload accounting to tell you which container caused it. Explicit per-container limits make capacity planning a per-service decision (this service gets this much CPU and RAM, always) rather than an emergent property of the day's neighbor mix, and they make a container's own docker inspect output an honest record of what it is entitled to.
Worked example
A host runs four API containers with no memory limits set. A memory leak in one of them slowly consumes RAM until the host is under pressure; the kernel's OOM killer fires and terminates a process in a different, healthy container, because that container's process happened to score worse on the kernel's heuristics at that moment. After adding --memory=512m --memory-swap=512m to every container, the leaking one instead gets its own process killed once it crosses 512 MB, its docker inspect shows OOMKilled: true against that specific container, and the other three keep running untouched. The failure is now attributable and comes with a hard, checkable number instead of a shared, host-wide guess.
Trade-offs & pitfalls
- Setting
--memorytoo tight causes healthy workloads to be killed under normal, non-leaky peak load (a burst in traffic, a large batch job); size it from measured peak usage with headroom, not from idle-state observation. - Relying only on
--cpuswithout ever measuring actual usage under load can silently throttle a service during real traffic spikes, showing up as added latency that is easy to misdiagnose as a code regression. --memory-swapequal to--memory(no swap) is usually the right default for latency-sensitive services, since swapping trades a hard, visible failure for a slow, hard-to-diagnose one; leaving swap available can be appropriate for batch or background work where an occasional slowdown is preferable to being killed outright.
For a stateful database service, would you use a named Docker volume or a host bind mount in development versus in production, and why? Discuss portability, performance, backup strategy, and permission issues you might hit (including SELinux on some Linux hosts).
Sample Answer
Direct answer
In development, a bind mount (a direct mapping of a path on the host's own file system into the container) is usually the better fit for a stateful database, because it puts the data somewhere a developer can see, back up, and delete with ordinary host tools, at the cost of portability and occasional permission friction. In production, a named Docker volume (storage that Docker itself creates and manages, identified by a name rather than a host path) is the better fit, because it is portable across hosts, does not depend on a specific host directory existing with the right permissions, and integrates with Docker's own backup and volume-driver tooling rather than whatever ad hoc scripts a bind mount setup would need.
Structured elaboration
Portability
A bind mount hardcodes a host path into the container's configuration; moving the workload to a different host, or a different developer's machine with a different directory layout, means updating that path everywhere it is referenced. A named volume is referenced only by name; Docker manages where its data actually lives on disk, and that mapping does not need to change when you move the container definition to a new host, or attach the same volume to a replacement container after an upgrade.
Performance
On Linux hosts, both a bind mount and a named volume are ordinary file system access with negligible overhead. The gap shows up specifically on Docker Desktop for macOS or Windows, where a bind mount crosses into a virtualized Linux environment and back, and can be noticeably slower for I/O-heavy workloads (a database doing many small reads and writes is exactly this case) than a named volume, which lives natively inside that virtualized environment and avoids the cross-boundary file-sharing layer entirely.
Backup strategy
A bind mount's data is just files at a known host path, so host-level backup tools (a cron job running rsync or a snapshotting file system) work without any Docker-specific knowledge, which is convenient for a single developer's machine. A named volume's data lives inside Docker's own storage area, so backing it up properly means either running a helper container that mounts the volume and streams a tar archive out (docker run --rm -v myvolume:/data -v $(pwd):/backup alpine tar czf /backup/myvolume.tar.gz -C /data .), or using a volume driver that has its own backup integration. This is a small amount of extra ceremony that is worth it in production for the portability and permission benefits above.
Permission issues, including SELinux
A bind mount exposes the container to the host's actual file ownership and permission model directly: a container process running as a different user ID (UID) than whatever owns the host directory can get permission-denied errors immediately, since the container is not exempt from ordinary Unix file permissions just because it is a container. On a host with SELinux (Security-Enhanced Linux, a mandatory access control system that restricts what a process may do to specific files beyond ordinary Unix permissions, common on Red Hat family distributions) enforcing, a bind-mounted directory additionally needs the right SELinux label before a container process can access it at all, even if the Unix permission bits look correct; Docker's :z and :Z mount flags request that label automatically (:z shares the label across multiple containers that need to read the same host directory; :Z marks it private to just this one container). A named volume mostly avoids this class of problem, since Docker creates and manages its storage directly and typically already applies a compatible label, rather than depending on whatever an existing host directory happened to be labeled for some other purpose.
Worked example
A developer runs Postgres locally with -v ./pgdata:/var/lib/postgresql/data (a bind mount) so they can browse the raw data files, delete the directory to reset state instantly, and back it up by just copying the folder; on a Fedora workstation with SELinux enforcing, the container fails to start with a permission error until the mount is changed to -v ./pgdata:/var/lib/postgresql/data:Z, which applies the correct private SELinux label to that directory for this container. In production, the same service instead uses -v pgdata:/var/lib/postgresql/data (a named volume): the same container definition now works unchanged on any host in the fleet, regardless of that host's specific directory layout or SELinux configuration, and backups run through a scheduled helper container rather than a host-specific script tied to one machine's file paths.
Trade-offs & pitfalls
- Using a bind mount in production because "it worked in development" reintroduces exactly the portability and permission problems a named volume exists to avoid, the first time the container needs to run on a different host.
- Using a named volume in development removes the convenience of browsing and editing the data directly with ordinary host tools, which is often exactly what a developer wants while debugging.
- Forgetting the SELinux label on a bind mount on an enforcing host produces a permission error that looks identical to a plain Unix permissions problem; check
getenforceon the host before assuming the fix is a UID orchmodchange.
Why is it recommended for a containerized application to write logs to stdout and stderr instead of a local file? What operational benefits does that give the team running it?
Sample Answer
Direct answer
Writing logs to stdout and stderr (the two standard output streams every process has, one conventionally for normal output and one for errors) instead of a file inside the container lets Docker's own logging machinery capture them automatically and uniformly, without the application needing to know anything about where it is running or how its output will ultimately be collected. This is the containerized expression of a wider principle (sometimes called the twelve-factor app approach to logging, from an influential set of practices for building portable, cloud-friendly services): treat logs as an event stream the environment is responsible for routing, not a file the application itself manages.
Structured elaboration
- Docker captures it for free. Anything written to stdout or stderr is captured by Docker's logging driver automatically;
docker logs <container>shows it with no extra configuration, and the same output can be forwarded to a centralized logging system (a log-shipping agent, a cloud logging service) purely through Docker's own driver configuration, without the application changing a single line of code. - A container's own file system is usually short-lived. If a container writes logs to a local file instead, those logs disappear the moment the container is removed (its writable layer, the container's own file system changes on top of its read-only image layers, does not survive
docker rm), unless someone separately arranges a volume mount just to persist that file, which is extra setup only needed because of the choice to write to a file in the first place. - It decouples the application from log routing entirely. The same container image can run in a developer's terminal (where stdout just prints to the screen) or in a production fleet forwarding to a centralized log system, with zero code or configuration change inside the application; the routing decision belongs entirely to whoever operates the container, not to the application's own source code.
- It composes cleanly with many containers on one host. Every container's stdout and stderr are tagged by Docker with that container's identity automatically; an operator does not need every application to independently implement consistent file naming, rotation, or a shared log directory convention to avoid one service's log files colliding with another's.
Worked example
Two services, an API and a background worker, both log to stdout in a structured, one-JSON-object-per-line format containing a timestamp, a severity level, and a message. An operator runs docker logs -f api during a live incident and sees the API's output immediately, correctly separated from the worker's own output, with no shared log file or naming convention either service had to agree on in advance. Later, when the same containers are redeployed onto a fleet with a centralized logging system, the only change needed is Docker's own logging-driver configuration (pointing it at the new destination); neither service's code changes at all, because neither one ever assumed anything about where its own output would end up.
Trade-offs & pitfalls
- This does not mean an application can never write anything to disk; it means log output specifically should go to the standard streams, while genuinely disk-oriented data (a temporary working file, a cache) can still legitimately use the file system, ideally somewhere explicitly marked as ephemeral or backed by a volume if it needs to survive a restart.
- A high-volume, chatty stdout stream still needs sensible log levels and rate limiting at the application level; routing logs correctly does not fix a service that is simply too noisy, it only ensures that noise ends up somewhere sensible instead of nowhere at all.
- Multi-line output (a stack trace, for example) needs care in a structured-logging setup, since a naive line-by-line capture can split one logical error event into many separate log lines unless the application or logging driver handles multi-line records deliberately.
Design a Docker image build strategy for a Java Spring Boot application to optimize build cache and startup time. Consider Spring Boot layered JARs versus jlink versus a buildpacks-based build, provide an example of how dependencies get cached across builds under your chosen approach, and explain the trade-offs against using a GraalVM native image.
Sample Answer
Direct answer
For a typical Spring Boot service, I would default to Spring Boot's own layered-jar (Java ARchive, the packaged format a Java application ships as) extraction pattern inside a multi-stage Dockerfile, because it directly targets the two things the question asks to optimize, build cache reuse and startup time, with the least operational complexity. I would reach for a GraalVM native image only once startup latency and baseline memory become a hard requirement the JVM (Java Virtual Machine) cannot meet, since native image trades a much heavier build for a much lighter runtime, and reach for Cloud Native Buildpacks mainly when I want to avoid maintaining a Dockerfile at all rather than for a technical advantage over layered jars.
The three approaches
- Spring Boot layered jars. A Spring Boot fat jar (one file containing the application classes and all its dependencies) is not itself cache-friendly to
COPYas a single unit, since any code change invalidates the whole layer. Spring Boot's own extraction tooling splits it into separate directories, dependencies (which change rarely), the Spring Boot loader itself (changes only on a Spring Boot version bump), snapshot dependencies, and the application's own classes (which change on every commit), so that a Docker layer built from the rarely-changing directories is reused across builds, and only the layer built from the application's own classes needs to be re-pulled after a typical code change. jlink, a JDK (Java Development Kit) tool that builds a custom, minimal Java runtime containing only the modules an application actually uses, instead of shipping a full general-purpose JRE (Java Runtime Environment) base image. This can meaningfully shrink the runtime image's baseline size and attack surface, but Spring Boot's heavy use of reflection and dynamic class loading (core to how its dependency injection and auto-configuration work) makesjlink's module-dependency analysis (viajdeps) less reliable than for a simple, mostly-static application:jdepscan miss modules only reached through reflection, and the fix is to either whitelist extra modules manually or accept a runtime failure only surfaces at whatever code path exercises the missed reflection, not at build time.- Cloud Native Buildpacks (a standard, defined by the Cloud Native Buildpacks project, for turning source code directly into a runnable OCI (Open Container Initiative, the industry standard defining the container image and runtime format that Docker and other tools implement) image without writing a Dockerfile), invoked from Spring Boot via
./mvnw spring-boot:build-imageor./gradlew bootBuildImage. This produces a layered image using largely the same layering principle as the manual approach above, but the build logic, base image selection, and layer boundaries are owned by the buildpack rather than a Dockerfile you maintain yourself, trading control and transparency for less Dockerfile maintenance burden across many services that all want the same pattern.
Worked example: how dependencies get cached under the layered-jar approach
This is Spring Boot's own current documented pattern (verified directly against the live Spring Boot reference documentation), using the tools jarmode (the current name; older Spring Boot versions and older tutorials use the now-superseded layertools jarmode, worth recognizing if you encounter it in existing pipelines):
FROM bellsoft/liberica-openjre-debian:25-cds AS builder
WORKDIR /builder
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted
FROM bellsoft/liberica-openjre-debian:25-cds
WORKDIR /application
COPY --from=builder /builder/extracted/dependencies/ ./
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
COPY --from=builder /builder/extracted/application/ ./
ENTRYPOINT ["java", "-jar", "application.jar"]
Each COPY --from=builder line becomes its own Docker layer. On a typical code change (only the application directory's contents differ from the previous build), Docker reuses the cached dependencies, spring-boot-loader, and snapshot-dependencies layers unchanged, and only rebuilds and re-pushes the small application layer, rather than the whole fat jar as one monolithic unit. The base image here also matters for startup time specifically: bellsoft/liberica-openjre-debian:25-cds ships with CDS (Class Data Sharing, a JVM feature that pre-parses and caches class metadata to speed up JVM startup) enabled, which Spring Boot's current layered layout is explicitly designed to be compatible with.
Trade-offs against GraalVM native image
A GraalVM native image ahead-of-time (AOT) compiles the application into a standalone native executable with no JVM startup cost at all, giving near-instant startup and substantially lower baseline memory, which matters for workloads that scale to zero or spin up many short-lived instances. The cost is a much heavier and slower build (AOT compilation analyzes the whole reachable call graph, which takes meaningfully longer and more build memory than packaging a jar), and Spring Boot's reflection-heavy dynamic behavior needs explicit AOT processing support (Spring Boot's own native-image integration, introduced to generate the reachability metadata GraalVM needs) to work at all; a library or a piece of application code that does something dynamic GraalVM's static analysis cannot see can fail only at native-image build time or, worse, only at runtime if the reachability metadata is incomplete.
Trade-offs and pitfalls
The layered-jar approach is a strict improvement over shipping the fat jar as one COPY for both stated goals (cache reuse and startup, via CDS-friendly layout) with essentially no downside, which is why I would default to it. jlink's benefit is real but harder to fully realize on a framework as reflection-heavy as Spring Boot without careful validation of what jdeps actually captured. GraalVM native image is the right call only once the JVM's startup and memory profile is an actual, measured problem for the deployment shape (very short-lived instances, extremely tight memory budgets), since committing to it means committing to a slower, more fragile build pipeline and Spring's native-specific AOT support surface, not just flipping a build flag.
You need to ship a container for a Python model that depends on NumPy, SciPy, and a custom C++ or CUDA extension compiled from source. Explain how you'd structure a multi-stage build to compile the native dependencies in one stage and copy only the runtime artifacts into a minimal final stage, and how you'd avoid runtime linker or ABI mismatches between the two stages.
Sample Answer
Direct answer
Put the compiler toolchain, headers, and native build (NumPy, SciPy, and the custom C++ or CUDA, NVIDIA's GPU programming toolkit, extension) in a builder stage, then copy only the compiled artifacts, the extension's shared object file and whatever Python packages depend on it, into a separate, minimal final stage that never installs a compiler. The one rule that prevents runtime linker and ABI (application binary interface, the low-level contract, such as function calling conventions and symbol layout, that a compiled extension and the interpreter or C library it links against must agree on) failures is that both stages must share the same base distribution and C library, so a .so built in one loads cleanly in the other; mixing an Alpine (musl C library) builder with a Debian or Ubuntu (glibc) final stage is the single most common way this breaks, because the two C libraries are not binary-compatible.
Structured elaboration
Structuring the multi-stage build
FROM python:3.12-slim AS builder
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc python3-dev && rm -rf /var/lib/apt/lists/*
# For a CUDA-enabled extension, use an NVIDIA CUDA "devel" base here instead
# (it ships the compiler and headers); the final stage below would then use
# the matching CUDA "runtime" base, not a plain python:slim image.
WORKDIR /build
COPY setup.py fastmath.c ./
RUN pip install --no-cache-dir setuptools \
&& python3 setup.py build_ext --inplace
FROM python:3.12-slim AS final
WORKDIR /app
RUN pip install --no-cache-dir numpy scipy # plain runtime deps, prebuilt
# wheels, nothing to compile here
COPY --from=builder /build/fastmath*.so ./
COPY app.py .
CMD ["python3", "app.py"]
The final stage never installs gcc or python3-dev; it only ever receives the already-compiled .so file via COPY --from=builder. This is what actually shrinks the image and reduces its attack surface: the compiler, headers, and any build-only tooling simply never exist in the artifact that ships to production. NumPy and SciPy themselves need no build stage at all here: on a standard glibc-based image they install from prebuilt wheels with no compilation, so only the genuinely custom, source-compiled extension needs the multi-stage treatment.
Why the ABI mismatch happens, specifically
A compiled Python extension is tagged with the exact platform it was built for, encoded right into its filename, for example fastmath.cpython-312-aarch64-linux-musl.so when built on Alpine (musl) versus ...-linux-gnu.so (or a bare .so) when built on Debian or Ubuntu (glibc). If the builder stage is Alpine and the final stage is Debian-based, the compiled extension links against musl's C library at build time; the glibc-based final stage does not have that library at all, so loading the extension fails at import time with an error naming the missing shared library, not a helpful "wrong platform" message. The fix is not a build flag; it is choosing the same base distribution family for both stages, or, if a smaller final image than the builder's own base is wanted, choosing a final base from the same family (for example both Debian-derived) rather than crossing between musl and glibc.
Avoiding the mismatch in practice
- Pin both stages to the same base image family and, ideally, the same major version, so their C libraries match exactly.
- If the final image genuinely needs to be smaller than the builder's base, use a slimmer image from the same family (
python:3.12-slimas both builder and final, rather thanpython:3.12-alpineas builder andpython:3.12-slimas final) instead of crossing library implementations. - For CUDA specifically, match the CUDA toolkit's minor version between the builder's "devel" image (compiler and headers) and the final stage's "runtime" image (no compiler, just the shared libraries the compiled code links against at run time); a CUDA extension compiled against one minor version can fail to load, or silently misbehave, against a runtime image pinned to a different one.
- Any Python-level runtime dependency the extension itself imports (NumPy, SciPy) still needs to be installed in the final stage too; copying the compiled
.sofile alone does not bring its Python-level dependencies with it.
Worked example
This exact scenario was built and run directly. A team builds a custom extension in an Alpine-based builder stage; the compiled artifact lands as fastmath.cpython-312-aarch64-linux-musl.so, its platform tag baked into the filename exactly as the build tool reports it. Copying that file as-is into a python:3.12-slim (Debian, glibc) final stage and importing it does not even get as far as the dynamic linker: Python's import system matches a compiled extension's filename against the interpreter's own list of recognized platform suffixes before ever trying to load it, and -linux-musl is not one of the suffixes a glibc-based interpreter recognizes, so the import fails with ModuleNotFoundError: No module named 'fastmath', the same error you would get if the file were simply missing entirely. If that filename check is bypassed, for example by renaming the file to a bare fastmath.so so the interpreter actually attempts to load it, the real, deeper failure surfaces: ImportError: libc.musl-aarch64.so.1: cannot open shared object file: No such file or directory, because the extension was in fact linked against musl and the glibc-based image has no such library at any path. Either way the fix is the same: switching the builder stage to python:3.12-slim as well (installing gcc and python3-dev via apt-get instead of apk) produces fastmath.cpython-312-aarch64-linux-gnu.so, which loads cleanly in the Debian-based final stage and runs correctly, computing the same result it would on any other correctly matched glibc setup. The only Dockerfile change that mattered was making both stages share the same base family; no code changed.
Trade-offs & pitfalls
- Chasing the smallest possible final image by reaching for Alpine is a common instinct, but for anything with compiled native extensions it trades a real, hard-to-diagnose runtime failure for a modest size saving; measure whether the size difference actually matters before accepting that risk.
- A
ModuleNotFoundErroror a missing-shared-library error at import time, for an extension that clearly built successfully in the builder stage, should immediately raise the question of a base-image mismatch between stages, before looking anywhere else. - Forgetting to install the extension's own Python-level dependencies (NumPy, SciPy) in the final stage is a separate, equally common mistake: the compiled
.sofile loads, but importing the module still fails because the pure-Python packages it depends on at import time were never installed there.
Unlock Full Question Bank
Get access to all Containerization and Docker Fundamentals interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.