Back to sections

Docker Interview Questions & Answers

A sample of real Docker interview questions with full answers and explanations - practice for interviews or certification exams.

easy

1. Which instruction in a Dockerfile creates a layer that can be cached across builds?

RUN apt-get update && apt-get install -y curl
COPY . /app
ENV VERSION=1.0
All of the aboveCorrect

It doesn't matter whether an instruction is RUN, COPY, or ENV, EVERY single one produces its own distinct image layer, and Docker caches all of them the same way. As long as nothing upstream of a given instruction has changed, that layer's cache can be reused on the next build.

medium

2. A container running a Java app with a slow memory leak keeps consuming more and more memory until the Linux kernel eventually kills it for using too much RAM, and in the process it also takes down other unrelated containers on the same host. What should you configure to contain the damage to just that one container?

A restart policy of always
Host networking mode
A larger base image
A memory limit on the container (like --memory) so only it gets capped and killed when it exceeds its own capCorrect

Without a memory limit, a leaking container can eat up all the memory on the host, and the kernel may end up killing other innocent processes and containers to free up RAM. Setting a per-container memory limit means only that container gets capped and eventually killed for exceeding it, protecting everything else running on the same machine.

hard

3. Your team deploys by pushing new code and re-tagging the image as 'latest' each time. You discover that different replicas of the same service are quietly running different versions of the code, because some nodes had already cached an older layer under the same 'latest' tag before it was overwritten. What's the right fix?

Increase the size of the base image
Always run docker pull manually on every node before every deploy
Tag and deploy images using an immutable identifier, like a commit SHA or the image's content digest, instead of relying on the mutable 'latest' tagCorrect
Turn off layer caching entirely across the cluster

Because 'latest' is just a movable label that can point to a different image at different times, nodes that already have an older image cached under that same name have no reason to know it's now stale. Tagging each build with something immutable, like the git commit SHA or the image digest, means every node is referencing one specific, unambiguous version, so there's no room for this kind of silent mismatch.

easy

4. You're building a local development setup where your web app needs to talk to a Postgres database and a Redis cache, and you want all three to start together with a single command using one configuration file. What tool is designed for exactly this?

docker-composeCorrect
A single Dockerfile
docker save
docker network create

docker-compose lets you describe multiple related services, like your app, a database, and a cache, in one YAML file and bring them all up together with one command, which matches this scenario exactly. A Dockerfile only describes how to build a single image, it doesn't coordinate multiple running services at once.

medium

5. Why does COPY package*.json . before COPY . . speed up Node.js builds?

Avoids copying node_modules
Leverages layer caching: npm install layer reused if package.json unchangedCorrect
Reduces network calls
Smaller layer size

Copying just package.json first means Docker's cache for the 'install dependencies' layer only gets invalidated when that FILE actually changes, not whenever any application code changes. Since app code changes far more often than dependencies do, this ordering means the (often slow) install step gets skipped and reused on most rebuilds instead of rerunning every time.

hard

6. You're running a Docker Swarm service and push a rolling update to a new version. One replica's health check starts failing right after the new version comes up, but Swarm keeps right on replacing the remaining replicas anyway, and the whole service ends up down. What should have been configured to prevent this?

A higher replica count for the service
An update_config with a failure_action of pause or rollback, combined with health checks, so Swarm halts or reverts the rollout instead of plowing through failuresCorrect
Removing the health check so it doesn't interfere with deployments
More CPU allocated to each replica

By default, Swarm's rolling update behavior can continue replacing replicas even after failures show up, unless you've explicitly configured a failure_action, like pausing the rollout or automatically rolling back to the previous working version once health checks start failing. Removing the health check would make this worse, not better, since it's the very thing that would have caught the bad deployment in the first place.

easy

7. What is the purpose of docker-compose?

To manage Docker registries
To build Docker images faster
To define and run multi-container applicationsCorrect
To monitor container health

Real applications are rarely just one container, they're usually several services working together (an app, a database, a cache). docker-compose lets you describe all of those services, plus how they network and store data, in one YAML file, so the whole group can be started or stopped together with a single command instead of managing each container by hand.

medium

8. In a docker-compose setup, you have a batch job container that's meant to run a task once and then exit successfully. Instead, you notice Docker keeps restarting it over and over in an endless loop right after it finishes. What's the likely cause?

The restart policy is set to always or unless-stopped, which restarts the container even after a normal, successful exitCorrect
The container doesn't have a health check configured
The container needs a volume attached
The image is too large

The always and unless-stopped restart policies bring the container back up no matter why it exited, including a perfectly normal, successful completion, which is the wrong behavior for a one-shot job. Switching the policy to no, or on-failure if you only want retries after actual errors, lets the job run once and stay stopped.

hard

9. Container starts, but HEALTHCHECK always fails with “connection refused”. App logs show it’s listening on 127.0.0.1:8080. Why?

Health check runs in same network namespace, but localhost = container loopback
App bound to 127.0.0.1 only → not reachable via container IPCorrect
Port not exposed
HEALTHCHECK uses host networking

Binding to 127.0.0.1 specifically means 'only reachable from THIS exact loopback interface', which inside a container is its OWN internal loopback, not something reachable from outside via the container's real IP address. A health check hitting the container's IP address gets refused because the app was never actually listening on that address, only on its own internal localhost.

easy

10. Which Docker daemon configuration mitigates container breakout?

Enabling --userns-remapCorrect
Increasing --log-level=debug
Using bridge network driver
Setting --mtu=1500

--userns-remap works by mapping container UIDs, including what LOOKS like root inside the container, to a completely unprivileged UID range on the actual host. That means even if an attacker somehow breaks out of container isolation entirely, they land as an unprivileged host user, not real root, which is exactly the containment this setting buys you.

Want to practice under real conditions?

Try the full timed Docker quiz.

    Welcome to OpsQuiz!

    Real scenario-based DevOps questions, hands-on practice, and clear explanations for every answer.