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.
In a multi-container development environment, one service can't connect to another using its service name on a Docker network, even though both containers are running (for example an API can't reach its database in a docker-compose stack). Walk through how you'd debug DNS resolution and network attachment to find the root cause, and explain the difference between the default bridge network and a user-defined network for this kind of service discovery.
Sample Answer
Direct answer
If an API cannot reach its database by service name inside the same Compose stack, the near-universal cause is that the two containers are not actually on a network with Docker's embedded DNS server enabled, most commonly because they ended up on the default bridge network instead of a user-defined one: Docker's default bridge network intentionally does not do name-based service discovery, while any user-defined network (which is what Compose creates for you automatically, unless you have overridden it) runs an embedded DNS resolver that resolves container and service names to their current IP addresses automatically.
Structured elaboration
Debugging DNS resolution and network attachment, in order
- Confirm both containers are actually attached to the same network:
docker network inspect <network-name>and look for both container names in itsContainerssection. A Compose stack with a mis-scopednetworks:block, or a container started withdocker runoutside the Compose-managed network entirely, is invisible to the other side no matter what DNS is doing. - From inside the calling container, attempt resolution directly:
docker exec <api-container> getent hosts <db-service-name>(ornslookup, if available in that image). A clean IP address back means DNS resolution itself works and the problem is elsewhere (the port, the database not actually listening yet, a firewall rule); a resolution failure means the DNS layer is the actual problem. - Check which network the containers landed on:
docker network lsanddocker inspect <container> --format='{{json .NetworkSettings.Networks}}'. If it shows the network named literallybridge, that is Docker's default network, not a Compose-created user-defined one, and name resolution by container name is not expected to work there at all. - If both containers are confirmed on the same user-defined network and resolution still fails, check for a typo or mismatch between the service name used in code and the actual Compose service name (Compose registers the service name, and, for the container's own hostname, the container name, as DNS entries on the network it creates; a hardcoded old container name or a renamed service is a common, easy-to-miss mismatch).
Default bridge network versus a user-defined network, for service discovery specifically
- The default
bridgenetwork (what you get from plaindocker runwith no--networkflag) does not run an embedded DNS server for container name resolution at all; containers on it can only reach each other by IP address (historically, the deprecated--linkflag patched hostnames into/etc/hostsas a workaround, which is not how modern setups should discover services). - Any user-defined network, including the ones Compose creates automatically for a project, runs Docker's embedded DNS resolver (at the well-known address
127.0.0.11inside each container on that network), which resolves both container names and Compose service names to the correct current IP address, and updates automatically if a container restarts and gets a new IP. - This is precisely why Compose "just works" for service-to-service calls by name out of the box: it always creates a user-defined network for a project unless you explicitly override that, so the embedded DNS behavior is present by default, not something you have to opt into separately.
Worked example
Two containers, api and db, are both started, and api's logs show connection refused or "unknown host" errors when it tries to reach db by name. Running docker exec api getent hosts db returns nothing and exits non-zero, confirming DNS resolution itself is failing, not just the connection. docker network ls shows both containers only ever attached to the default bridge network (the Compose file had accidentally used network_mode: bridge on the api service, which pins it to Docker's default bridge network instead of letting Compose create and use its own project network). Removing that override lets both services join the Compose-created user-defined network instead; getent hosts db then resolves to the container's current IP address, and the application connects successfully on the very next attempt, with no code change at all.
Trade-offs & pitfalls
- It is easy to "fix" this the wrong way by hardcoding the database's current IP address once resolution is confirmed working; container IPs on a bridge network are not stable across restarts, so this reintroduces the same failure the first time the database container restarts and is assigned a different address.
- A resolution failure and a "resolves fine but the connection is refused anyway" failure look similar from the application's point of view (both surface as some flavor of connection error) but have entirely different causes; always separate the DNS step (
getent hosts) from the actual connection step before concluding which layer is broken. - Legacy guidance mentioning the
--linkflag as a way to make one container resolve another by name is outdated; a user-defined network's embedded DNS server does that automatically for every container on the network, with no per-pair linking needed.
Compare three ways to deliver runtime secrets (API keys, database credentials) to a running container: plain environment variables, a mounted secret file, and fetching from an external secrets provider at startup. Discuss the security and operational trade-offs of each.
Sample Answer
Direct answer
All three approaches get a secret into a running container, and they trade off along the same two axes every time: how easily the secret leaks to somewhere it should not, process listings, logs, crash dumps, and how much operational machinery, rotation, an external dependency at startup, the approach requires. Environment variables are the easiest to wire up and the easiest to leak. A mounted file is a meaningfully better default for most services. An external secrets provider fetched at startup is the strongest option once rotation and centralized audit actually matter enough to justify the added dependency.
Comparison
| Method | How it works | Leak surface | Rotation | Operational cost |
|---|---|---|---|---|
| Environment variable | Set via a run flag or the orchestrator's own secret-as-env-var feature | Visible in docker inspect, often readable from the process's own environment file under /proc, frequently and accidentally logged by frameworks that dump their configuration on startup or in a crash report, inherited automatically by every child process the application spawns | Requires a full container restart with a new value | Lowest; no extra tooling |
| Mounted secret file | Secret content written to a file, for example /run/secrets/db_password, via a volume, a tmpfs mount, or the orchestrator's native secrets object | Confined to a filesystem path with normal file permissions, not exposed in docker inspect or the process environment, not automatically inherited by child processes, but still readable in plaintext by anything with filesystem access inside the container | The orchestrator can often update the file's content in place without restarting the container, if the application re-reads the file | Low; most orchestrators provide this as a first-class primitive |
| External secrets provider fetched at startup | The application, or an init process, calls a secrets manager over the network at startup and holds the value only in memory | Never touches disk or a layer at all if handled carefully; centralizes access logging, who fetched which secret and when, at the provider itself; a bug that dumps process memory is still a real risk | Best: can rotate centrally and have the application re-fetch on an interval or a signal, with no container restart at all | Highest; a network dependency at container startup, plus its own availability and authentication concerns |
The decision
For most services, a mounted file beats a plain environment variable for the same delivery need, because it closes off the two most common accidental-leak paths, inspect output and inherited child-process environment, for close to zero extra operational cost; most orchestrators already support this as a first-class primitive. Paying for an external provider makes sense once rotation genuinely matters operationally, credentials that must rotate on a schedule or be revoked immediately after an incident, or once there are enough services that centralized access auditing is worth more than the added startup dependency. For a small number of low-change-frequency secrets, an external provider is often more machinery than the risk actually justifies.
What none of these three replace
Choosing among them is never a substitute for restricting who can execute into the container or read its filesystem and inspect output in the first place. All three assume some baseline of access control around the host and orchestrator; the delivery mechanism narrows the leak surface, it does not eliminate the need for that baseline.
Worked example
Take one database password, CorrectHorseBattery9, delivered three ways to a running container; the environment-variable and mounted-file cases below were built and run directly to confirm the claims above.
As a plain environment variable (docker run -e DB_PASSWORD=CorrectHorseBattery9 ...): docker inspect <container> --format='{{json .Config.Env}}' prints it straight back in cleartext, ["DB_PASSWORD=CorrectHorseBattery9", ...]. A shell spawned inside that same container inherits it with no extra step: running sh -c "env | grep DB_PASSWORD" from inside the container shows the child shell already has it, which is the concrete evidence behind "inherited automatically by every child process" in the table above.
As a mounted file instead (docker run -v ./db_password:/run/secrets/db_password:ro ...): the identical docker inspect --format='{{json .Config.Env}}' on this container comes back with no trace of the secret at all, and docker inspect's Mounts entry shows only the path, /run/secrets/db_password, never the file's content. Inside the container, ls -l /run/secrets/db_password shows whatever permission the source file had on the host, -rw-r--r-- for a file created with a plain shell redirect under a default umask; a bind mount does not tighten this on its own, so getting a genuinely restrictive -rw------- needs an explicit chmod 600 on the host file before mounting it, or a secrets primitive that manages file modes itself. Only an explicit cat of that exact path reveals the value regardless of the exact mode bits; a spawned child shell's own env | grep db_password comes back empty, confirming the file is not inherited the way an environment variable is.
An external secrets provider fetched at startup does not appear in docker inspect output or on any container filesystem path at all; the value exists only in the application's own process memory after it calls out over the network at startup, which is exactly why it closes off the two leak paths the other options cannot, at the cost of a real network dependency the moment the container starts.
Trade-offs and pitfalls
An external provider fetched at startup adds a genuine availability coupling: if the secrets service is unreachable when the container tries to start, the container fails to start too, which needs its own retry and alerting story, exactly the kind of failure mode that stays invisible until the one time it matters. A mounted file is only as good as the orchestrator's own storage of it before mounting; if the orchestrator's own secret store is itself an unencrypted value at rest, mounting it into the container is a real improvement over an environment variable, but not a complete solution on its own.
Compare strategies for managing Python dependencies inside a container: conda environments versus pip with virtualenv or Poetry, including how you'd handle compiled dependencies like CUDA wheels or custom native extensions. How do you keep dependency resolution reproducible and upgrades safe across CI runs?
Sample Answer
Direct answer
For a service running in a container, pip with a lockfile-generating tool (Poetry, or in 2026 increasingly uv) is the better default: it produces small, fast-to-build images and plays well with Docker's layer caching. Reach for conda only when you specifically need conda-forge's precompiled, ABI-matched native stack (BLAS/CUDA combinations that don't ship as pip wheels) and are willing to pay for a larger image and a slower dependency solve. Whatever you pick, compiled dependencies (CUDA wheels, custom C/C++ or Rust extensions) get built once in a dedicated build stage and shipped as an artifact, never compiled fresh inside the runtime image or at container start.
Structured elaboration
Terms first: a wheel is Python's prebuilt binary package format (.whl); manylinux is the tagging convention that marks a wheel as portable across Linux distributions with a compatible glibc, which is what lets pip install torch fetch a working binary instead of compiling from source; the ABI (application binary interface) is the low-level contract, function layouts, calling conventions, that a compiled extension and the interpreter or CUDA runtime it links against must agree on (mismatch it and you get an import-time crash, not a helpful error message); CUDA is NVIDIA's GPU programming toolkit and runtime, and BLAS is a standard, low-level library interface for linear-algebra operations like matrix multiplication; a lockfile pins every resolved dependency, direct and transitive, to an exact version and ideally a hash, so the same input always resolves to the same set of packages.
| conda | pip + virtualenv | Poetry | |
|---|---|---|---|
| What it manages | Python interpreter, native libraries (BLAS, CUDA runtime pieces), and Python packages, in one solver | Just Python packages; the interpreter and system libraries are the base image's job | Same scope as pip, but adds a resolver and a lockfile on top of pyproject.toml |
| Best fit for compiled deps | Strong: conda-forge ships prebuilt, cross-linked native binaries (MKL, CUDA-aware NumPy/SciPy) that are painful to build from source | Fine if manylinux/CUDA wheels exist for your platform; weak if they don't | Same as pip since it installs from the same wheel index; no native-package solver of its own |
| Reproducibility | Depends on pinning exact build strings, not just versions; conda-forge channel drift can change results on a re-solve | Reproducible with pip-compile --generate-hashes or equivalent; a bare requirements.txt with loose versions is not | poetry.lock pins exact versions and hashes by default |
| Image size / build speed | Larger images, slower dependency solve | Small, fast | Small, fast; resolution can be slow on large dependency graphs |
| CI ergonomics | Needs conda-lock (or a pinned, exported environment.yml) to get lockfile behavior at all | Native fit for pip install -r requirements.lock.txt --require-hashes | Native fit: poetry install --no-root against a committed lockfile |
By 2026 a fourth option is common enough to name even though the question didn't ask for it: uv, a Rust-based, pip-compatible installer and resolver that produces a single cross-platform lockfile (uv.lock) and resolves noticeably faster than Poetry on large dependency graphs. It doesn't change the underlying trade-off above (it's still "pip-family," so the conda-vs-pip split still applies), it's simply a faster, more current implementation of the pip and Poetry side of that split, and it's a reasonable default for new projects.
Handling compiled dependencies (CUDA wheels, custom native extensions)
- Match the wheel's CUDA build to your runtime, not the other way around. A wheel like
torch==2.x+cu124is compiled against CUDA 12.4's ABI; installing it on an image whose CUDA driver or toolkit is older will fail at import or, worse, silently fall back to running on CPU. Pin the CUDA version in your base image tag (for examplenvidia/cuda:12.4.1-runtime-ubuntu22.04) and the wheel's CUDA suffix together, and treat that pair as one unit to upgrade, not two independent version bumps. - For a custom native extension (a C, C++, or Rust module wrapped for Python), use a multi-stage Dockerfile: a builder stage on the CUDA devel image (which has the compiler toolchain and headers) builds the extension into a wheel; the runtime stage, based on the much smaller CUDA runtime image (no compiler, no headers), only ever installs that already-built wheel. This keeps compilers and headers out of the production image, shrinking it and reducing its attack surface, and it turns the compiled artifact into something you can cache and reuse across builds instead of recompiling every time.
flowchart LR
A[Builder stage: CUDA devel image plus compilers] --> B[Compile native extension into a wheel]
B --> C[Runtime stage: CUDA runtime image, no compilers]
D[Locked requirements file: poetry.lock or uv.lock] --> C
C --> E[Slim production image]
Keeping resolution reproducible and upgrades safe in CI
- Never let a CI build re-resolve dependencies from loose version ranges. Resolve once (locally, or in a dedicated update job), commit the lockfile, and have every ordinary CI run simply install from that lockfile with hash verification. That turns "does this build reproducibly" into a solved problem: the lockfile is the single source of truth for what "this build" means.
- Pin the base image by digest, not just a mutable tag, so a tag repoint upstream can't silently change your build.
- Separate the update path from the build path: a scheduled job (Renovate, Dependabot, or a manual
poetry updateoruv lock --upgrade) opens a pull request that bumps the lockfile and nothing else, that pull request runs the full test matrix, including any CUDA-dependent tests on a GPU runner, and a human or a required check approves the merge. Ordinary feature-branch CI never touches dependency versions at all.
Worked example
A team ships a FastAPI inference service with a custom CUDA kernel wrapped via pybind11. The Dockerfile has two stages: FROM nvidia/cuda:12.4.1-devel-ubuntu22.04 AS builder installs the CUDA toolkit headers and a compiler, then runs pip wheel . -w /wheels to produce custom_op-1.0.0-cp311-cp311-linux_x86_64.whl. The second stage, FROM nvidia/cuda:12.4.1-runtime-ubuntu22.04, copies only /wheels/*.whl and the project's uv.lock, then runs uv pip install --no-cache /wheels/*.whl -r requirements.lock.txt. The devel image's compiler toolchain and headers never make it into the shipped image; the runtime stage only carries the CUDA runtime libraries the compiled kernel links against at inference time, plus the pure-Python dependencies pinned in the lockfile. A nightly job runs uv lock --upgrade on a separate branch, and only that branch's CI is allowed to change what the lockfile pins.
Trade-offs & pitfalls
- Mixing
conda installandpip installin the same environment is a common wrong turn: conda's solver doesn't know about packages pip installed afterward, so a laterconda updatecan silently break binary linkage that pip had put in place (a NumPy build expecting conda's BLAS, next to a pip-installed package expecting a different one). - A
requirements.txtwith unpinned or loosely pinned transitive dependencies isn't reproducible even if the top-level packages are pinned; a transitive C-extension dependency can float to a new version with a different ABI and only fail on the CUDA-enabled CI runner, not a developer's laptop. - Recompiling a native extension inside the final production build step, instead of shipping a pre-built wheel from a builder stage, means what CI tested and what got deployed are two different compiled artifacts even if the source was identical, because compiler flags, library versions, or the timing of an upstream release can differ between the two builds.
- Both
conda-lockand hash-pinned pip files solve reproducibility, but neither protects against a conda channel or PyPI index quietly yanking a version; vendoring or mirroring critical wheels internally is the fix once that has caused an outage.
What role do image registries (Docker Hub, AWS ECR, GCR, Azure ACR) play in the container workflow, and why might an organization run a private registry instead of only using a public one? Cover tagging conventions (semantic versions vs. commit SHAs), and authentication/access control for pushing and pulling in production.
Sample Answer
Direct answer
An image registry stores, versions, and serves container images the way a package repository serves libraries: docker push uploads layers and a manifest to it, docker pull (or a deployment step) downloads them. Docker Hub is the default public registry; AWS ECR (Elastic Container Registry), Azure Container Registry (ACR), and Google Artifact Registry are the major cloud-managed alternatives. An organization runs a private registry, rather than relying only on a public one, mainly for access control, reliability guarantees it does not get from a shared public service, and to keep proprietary images out of a public namespace entirely.
Naming note: Google Container Registry is retired
The question's third option, GCR (Google Container Registry, gcr.io), is retired for new use: Google shut down write access on March 18, 2025, so docker push to a Container Registry-hosted repository no longer works. Read access was not cut off on a fixed date the way write access was; Google migrated existing gcr.io image references to be served through Artifact Registry under that same hostname, so images already published there keep pulling successfully rather than disappearing. Google's current offering for new work is Artifact Registry, which also supports legacy gcr.io-style hostnames for images that were already there. A candidate who has used the older name should recognize it, but new work should target Artifact Registry directly, and the operational risk to plan for is push/CI credentials configured against Container Registry breaking, not already-published images becoming unreachable.
Why run a private registry
A public registry like Docker Hub is a fine default for open-source or non-sensitive images, but production teams usually want: access control (who can push a new version of a production image, and who can pull it, tied into the same identity system as everything else); rate-limit and reliability guarantees a shared public service does not promise for free-tier pulls at production scale; and simply not publishing proprietary application code as an image anyone on the internet can pull. Cloud-managed private registries (ECR, ACR, Artifact Registry) add tight integration with each cloud's identity and access management (IAM) system, so a compute service can be granted pull access without embedding long-lived credentials.
Tagging conventions
Two conventions dominate, and most mature pipelines use both for different purposes: semantic version tags (v1.4.2) are meant for humans and for deliberate promotion between environments (staging tests v1.4.2, then production is told to run v1.4.2), while commit-SHA tags (myapp:a1b2c3d) are generated automatically on every build and give perfect traceability back to the exact source commit, which a semantic version alone does not (two builds of "the same" v1.4.2 from a hotfixed branch would otherwise be indistinguishable). Many pipelines push both tags to the same underlying image so a human-readable release name and an exact build provenance trail both exist.
Authentication and access control in production
Pushing and pulling from a private registry requires authenticating first (docker login, or a cloud CLI's registry-login helper that exchanges cloud credentials for a short-lived registry token). In production, the strong pattern is to avoid long-lived, shared credentials entirely: a compute service (a CI runner, a Kubernetes node, a serverless container platform) is granted an IAM role or workload identity scoped to pull (and, for CI, push) specific repositories, and the registry issues short-lived tokens against that identity rather than a static username and password baked into a config file.
Design a health-check strategy for a containerized service whose startup can take several minutes and whose dependencies may be temporarily unavailable. How would you configure HEALTHCHECK (start period, interval, retries) and restart policy so deployments are safe without being overly fragile, and what would you still want in logs and metrics on top of the health check itself?
Sample Answer
Direct answer
For a service with a genuinely slow startup and flaky dependencies, size HEALTHCHECK's start_period to comfortably exceed the worst realistic startup time (including retries against a slow dependency), keep interval and retries tight enough that a true failure is caught within your deployment tool's own timeout, and keep the restart policy focused on crashes, not on health status, since plain Docker does not restart a container just because it reports unhealthy. On top of the health check itself, add startup-phase logging and a small number of metrics (time to first healthy, dependency-connect latency, restart count) so you can tell "slow but fine" apart from "actually stuck" without staring at docker inspect.
Structured elaboration
Sizing the HEALTHCHECK knobs
start_period: failures during this window do not count towardretriesand do not flip the container tounhealthy; the status showsstartinginstead. Set it to comfortably exceed your measured p99 startup time (dependency waits included), not your typical case. If startup can occasionally take three minutes when a dependency is slow, a sixty-secondstart_periodwill produce exactly the flapping this design is meant to avoid.intervalandtimeout: once paststart_period, these control how quickly you detect a real regression. A shorter interval catches problems sooner but adds load; atimeoutshorter than the check's own worst-case latency will produce false failures under normal load, not just real outages.retries: the number of consecutive failures required to flip tounhealthy. Combined withinterval, this is your total detection latency afterstart_periodends (interval x retries, roughly). Tune it against whatever consumes the status: a deployment tool waiting for containers to become healthy before proceeding, or an operator dashboard.
Restart policy, and the boundary it does not cross
- A restart policy (
on-failure[:max-retries],always,unless-stopped) reacts to the container's process exiting, not to the health status. A container can sit atunhealthyindefinitely without ever being restarted by Docker itself. This matters here specifically: if a dependency is temporarily unavailable, you want the health check to sayunhealthy(so the status is honest and load balancers or an orchestrator can act on it) without the container being torn down and restarted, which would only add restart churn on top of an already-degraded dependency and could reset any warm state (caches, pooled connections) for no benefit. - Use
on-failure:Nfor genuine crash protection (the process actually exits), and let the health status, not the restart policy, communicate "temporarily degraded, dependency down."
What still belongs in logs and metrics, beyond the health check
- A single structured log line per startup phase (config loaded, dependency connected, ready to serve) so a human can see exactly where a slow startup is spending its time, rather than staring at a binary
unhealthyflag. - A startup-duration metric (time from process start to first successful health check) so a slow trend is visible before it becomes an outright failure.
- A dependency-connect-attempt counter or histogram, so "dependency flaky" is a graph, not a guess reconstructed from timestamps in raw logs after the fact.
- The count of consecutive health-check failures and the current status, exported as a metric, so dashboards and alerting do not have to poll
docker inspectto know what Docker already knows internally.
Worked example
A service depends on a message broker that occasionally takes up to 90 seconds to accept connections during a rolling broker upgrade, and the service itself otherwise starts in about 5 seconds. Setting start_period=120s, interval=5s, timeout=3s, retries=3 gives the connection up to two minutes to succeed before any failure counts at all, and once past that window, a real failure is confirmed within about 15 seconds (3 retries x 5s). The restart policy is on-failure:5: if the process actually crashes (a bug, not a slow dependency), it gets a bounded number of automatic restarts, but a container that is merely waiting on a slow broker is never killed by Docker itself; it stays starting, then unhealthy if the broker genuinely does not come back, and an operator or orchestrator layered on top decides what to do about that, informed by the startup-phase logs showing exactly which dependency it was still waiting on.
Trade-offs & pitfalls
- A
start_periodset too long delays real detection: a container that is actually broken (not just slow) will look identical to a healthy one for that entire window. - A
start_periodset too short reintroduces the original problem: legitimate slow starts get flagged as failures, and if something downstream (a deployment tool, an orchestrator) does act on that status, you get exactly the premature restart churn this design is trying to prevent. - Relying on the restart policy to "fix" a container stuck at
unhealthyis a common but wrong instinct on plain Docker: it will not restart on health status alone, so the actual remediation (alerting a human, or letting an orchestrator's own readiness/liveness model act) has to live somewhere else.
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.