Skip to content
RED SAGSkills

Docker interview questions and answers

Docker questions check how you package and run applications in containers, and keep images small and secure.

Below are 30 Docker questions with the correct answer and a short explanation, grouped from fresher to experienced level. Try each one before you open the answer.

What Docker interviews usually check

  • ✓Images, containers and layers
  • ✓Writing a good Dockerfile
  • ✓Volumes and networking
  • ✓Docker Compose
  • ✓Image size and security

Docker interview questions for freshers easy

1. Docker image versus container

What is the difference between a Docker image and a Docker container?

  1. A.An image is a read-only template; a container is a running or stopped instance created from it
  2. B.An image is a running process; a container is the file stored on disk
  3. C.They are two names for the same thing
  4. D.A container is a lightweight virtual machine with its own guest kernel; an image is the shell script that boots it
Show answer

Answer: A. An image is a read-only template; a container is a running or stopped instance created from it

An image is a layered, read-only package of your app and its dependencies. Starting it creates a container, which adds a thin writable layer and runs as an isolated process sharing the host kernel. One image can run as many containers.

2. Container exits right after start

You run docker run -d myimage and the container shows "Exited (0)" a second later. What is the most likely reason?

  1. A.The CMD or ENTRYPOINT process finished, and a container stops when its main process ends.
  2. B.Detached mode (-d) stops containers after one second unless a TTY is attached with -t and -i.
  3. C.The image is too large, so the Docker daemon stops it to protect host memory.
  4. D.Exit code 0 means Docker could not find the image locally or in the registry.
Show answer

Answer: A. The CMD or ENTRYPOINT process finished, and a container stops when its main process ends.

A container runs one main process and stops when that process exits. Exit code 0 means it finished normally, for example a script that ran and ended, or a server that was started in the background instead of the foreground. Run the service in the foreground, and use docker logs to see what happened.

3. ENV password in a Dockerfile

A Dockerfile contains `ENV DB_PASSWORD=Sup3r$ecret`. Why is this a problem even if the image is private?

  1. A.ENV values are sent to Docker Hub analytics
  2. B.Anyone who can pull the image can read it from the image config
  3. C.The password is shown on the container's web page
  4. D.ENV makes the image fail security signing
Show answer

Answer: B. Anyone who can pull the image can read it from the image config

ENV values are stored in the image metadata, and docker inspect or docker history shows them to anyone with pull access, now and in every old copy. Secrets should be injected at runtime from a secret store. If one was baked in, rotate it.

4. Keeping database data across restarts

You run Postgres in Docker for local development. Every time you remove and recreate the container, the data is gone. What is the standard fix?

  1. A.Commit the running container to a new image each night
  2. B.Add a HEALTHCHECK to the Dockerfile
  3. C.Mount a named volume at the Postgres data directory
  4. D.Run the container with --restart always
Show answer

Answer: C. Mount a named volume at the Postgres data directory

Files written inside a container live in its writable layer, which is deleted with the container. A named volume (for example -v pgdata:/var/lib/postgresql/data) is stored outside the container and is reattached to the new one. A restart policy only restarts the same container; it does not help after removal.

5. Using :latest in a Deployment

Why is `image: myapp:latest` a poor choice in a production Deployment?

  1. A.You cannot tell which build runs, and nodes may run different builds
  2. B.The latest tag is blocked by Kubernetes in production namespaces
  3. C.Images tagged latest cannot be pulled from private registries
  4. D.The latest tag disables rolling updates for the Deployment
Show answer

Answer: A. You cannot tell which build runs, and nodes may run different builds

latest is just a moving label, so two nodes that pulled at different times can run different code under the same tag. Changing what latest points to does not trigger a rollout, and rollback has nothing distinct to go back to. Use an immutable tag such as the Git SHA, or a digest.

6. Docker restart policy on a VM

You run a small service with plain Docker on a VM. You want it to start again after a crash or a VM reboot, but not if you stopped it on purpose. Which restart policy fits?

  1. A.--restart unless-stopped
  2. B.--restart no
  3. C.--restart on-failure:0
  4. D.--restart never
Show answer

Answer: A. --restart unless-stopped

unless-stopped restarts the container after crashes and when the Docker daemon starts, unless you stopped it by hand. always would also bring back a container you had deliberately stopped once the daemon restarts. no is the default and never restarts it.

7. Local app with a database and cache

A developer wants to start the API, Postgres and Redis together on a laptop with one command and one file in the repo. What is the usual tool?

  1. A.Docker Compose with a compose.yaml file
  2. B.A Kubernetes CronJob for each service
  3. C.A Dockerfile with three FROM lines
  4. D.A Helm chart installed into Docker Desktop
Show answer

Answer: A. Docker Compose with a compose.yaml file

Docker Compose describes several containers, their networks, volumes and env vars in one YAML file, and docker compose up starts them together. Services reach each other by service name. It is the common choice for local development and simple test stacks.

8. Why put a model in a container

Why do teams usually package a model service in a Docker image?

  1. A.Docker makes the model more accurate
  2. B.The image bundles code, libraries and runtime so it runs the same way on a laptop, CI and production
  3. C.Docker images cannot be deployed to the cloud otherwise
  4. D.Containers remove the need for monitoring
Show answer

Answer: B. The image bundles code, libraries and runtime so it runs the same way on a laptop, CI and production

Containers package the exact Python version, libraries, system dependencies and model server together. This avoids "works on my machine" problems and gives a standard unit that Kubernetes and cloud services can run and scale.

9. Same code, different outputs after rebuild

After rebuilding the serving container, outputs changed slightly with no code change. The Dockerfile uses pip install torch transformers with no versions. What should you do?

  1. A.Rebuild daily so versions stay fresh
  2. B.Pin exact library versions and the image base tag
  3. C.Remove the Dockerfile and install by hand
  4. D.Run the model on CPU to avoid changes
Show answer

Answer: B. Pin exact library versions and the image base tag

Unpinned installs pull the latest versions at build time, which can change kernels, defaults or tokenisation. Pinning versions (or a lock file) and the base image makes builds reproducible, and upgrades become deliberate, tested changes.

10. A task that needs its own libraries

One task needs a library version that clashes with what is installed on the Airflow workers. Which operator avoids the clash by running the task in its own container image?

  1. A.KubernetesPodOperator
  2. B.BashOperator
  3. C.PythonOperator
  4. D.ShortCircuitOperator
Show answer

Answer: A. KubernetesPodOperator

KubernetesPodOperator launches a pod from any image you choose, so the task's dependencies live in that image, not on the workers. It works with any executor as long as Airflow can reach a Kubernetes cluster.

Docker intermediate interview questions medium

11. Why use multi-stage builds

What is the main benefit of this multi-stage Dockerfile?

FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app

FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
USER nonroot
ENTRYPOINT ["/app"]
  1. A.It makes the Go compiler run faster.
  2. B.It lets the container run two processes at the same time.
  3. C.It allows the image to be built without a Dockerfile.
  4. D.The final image contains only the compiled binary, not the compiler or source, so it is much smaller and has a smaller attack surface.
Show answer

Answer: D. The final image contains only the compiled binary, not the compiler or source, so it is much smaller and has a smaller attack surface.

Only the last stage becomes the final image. The build stage with the full Go toolchain and source code is thrown away, and just the static binary is copied into a minimal distroless base. This reduces image size, pull time and the number of packages that could have vulnerabilities.

12. Choosing an image tag in CI

Your pipeline pushes images for every merge to main. Which tag makes it easiest to know exactly which code is running in production?

  1. A.The word stable, moved on every release
  2. B.The build date, such as api:2026-10-16
  3. C.The branch name, such as api:main
  4. D.The Git commit SHA, such as api:3f9c2ab
Show answer

Answer: D. The Git commit SHA, such as api:3f9c2ab

A commit SHA is unique and links the image straight to the source, and it never moves to other code. stable and main are moving tags, and a date can have several builds. Many teams add a SemVer tag for releases alongside the SHA.

13. Mounting the Docker socket

A CI build container mounts /var/run/docker.sock from the host so it can run docker build. What does this give to any code running in the job?

  1. A.Read-only access to images on the host
  2. B.Access only to containers the job started
  3. C.Nothing, as the socket is sandboxed by default
  4. D.Root-level control of the host through the Docker daemon
Show answer

Answer: D. Root-level control of the host through the Docker daemon

Whoever can talk to the Docker daemon can start a privileged container that mounts the host filesystem, which is effectively root on the machine. On shared runners, one malicious job can then read other jobs' secrets. Rootless builders like BuildKit rootless or Buildah avoid this.

14. Private registry token during build

`npm ci` inside a Dockerfile needs a private registry token. Passing it with ARG leaves it in the image history. What is the safer method?

  1. A.COPY the .npmrc file and delete it in the next step
  2. B.Hard-code the token but use a multi-stage build
  3. C.RUN --mount=type=secret with docker build --secret
  4. D.Set the token with ENV and unset it at the end
Show answer

Answer: C. RUN --mount=type=secret with docker build --secret

BuildKit secret mounts expose the file only during that RUN step and never write it into a layer or the history. Copying then deleting still leaves it in the earlier layer, and ENV values are saved in the image config. Multi-stage builds help only if the secret never reaches the final stage.

15. Stale packages from layer cache

A Dockerfile has `RUN apt-get update` on one line and `RUN apt-get install -y curl` on the next. Weeks later a new package is added to the install line and the build fails with 404 errors from the mirror. Why?

  1. A.The cached update layer has an old package index
  2. B.apt-get cannot run twice in one Dockerfile
  3. C.The install line must come before the update line
  4. D.Docker deletes the package index after every RUN
Show answer

Answer: A. The cached update layer has an old package index

Docker reuses the cached apt-get update layer because that line did not change, so the install step uses an old index that points to package versions the mirror no longer has. Put both in one RUN, such as apt-get update && apt-get install -y ... && rm -rf /var/lib/apt/lists/*. Then any change to the install list refreshes the index too.

16. COPY or ADD in a Dockerfile

Why do Docker best practices prefer COPY over ADD for copying local files into an image?

  1. A.COPY makes smaller layers because it compresses files
  2. B.ADD cannot copy directories, only single files
  3. C.ADD also unpacks archives and fetches URLs, which can surprise you
  4. D.COPY runs as root while ADD keeps file ownership
Show answer

Answer: C. ADD also unpacks archives and fetches URLs, which can surprise you

ADD has extra behaviour: it auto-extracts local tar archives and can download remote URLs. That makes builds less predictable. COPY only copies, so use it unless you specifically want the extraction.

17. Slow Python builds on Alpine

You switched a Python image from python:3.12-slim to python:3.12-alpine to save space. Now pip install takes 15 minutes and needs gcc. Why?

  1. A.Alpine images block network access while pip install runs
  2. B.Alpine uses musl, so glibc wheels do not fit and pip compiles
  3. C.pip is not supported on Alpine, so it runs under emulation
  4. D.Alpine disables the pip download cache for all packages
Show answer

Answer: B. Alpine uses musl, so glibc wheels do not fit and pip compiles

Alpine uses musl libc. Packages that ship only manylinux (glibc) wheels cannot use them there, so pip compiles from source, which needs compilers and time. For Python, a slim Debian base or distroless is often the better small image.

18. Build ARG missing at runtime

A Dockerfile has `ARG APP_VERSION` and is built with `--build-arg APP_VERSION=2.3.1`. The app reads APP_VERSION from its environment when it runs and finds it empty. Why?

  1. A.ARG exists only during the build; use ENV to keep it in the image
  2. B.Build args are dropped when the image is pushed to a registry
  3. C.ARG values must be quoted to survive the build
  4. D.The app must read build args from /proc/args instead
Show answer

Answer: A. ARG exists only during the build; use ENV to keep it in the image

ARG values are available to later build steps only and are not set in the running container. ENV APP_VERSION=$APP_VERSION after the ARG bakes it into the image config. Never pass secrets through ARG either, since they can show in image history.

19. Dockerfile HEALTHCHECK on Kubernetes

An image has a HEALTHCHECK instruction that marks it unhealthy when /health fails. On Kubernetes, the container stays Running even while it is unhealthy. Why?

  1. A.HEALTHCHECK runs only when the image runs as root
  2. B.Kubernetes reads HEALTHCHECK but only every 10 minutes
  3. C.Kubernetes ignores HEALTHCHECK; it uses Pod probes instead
  4. D.The probe must use port 80 to be honoured
Show answer

Answer: C. Kubernetes ignores HEALTHCHECK; it uses Pod probes instead

The kubelet does not use the Docker HEALTHCHECK instruction. Health in Kubernetes comes from liveness, readiness and startup probes in the Pod spec. Define those probes for the same endpoint.

20. Deleted secret still in the image

A Dockerfile copies .env with a DB password in one step and deletes it in a later RUN step. The image was pushed to a public registry. Is the password exposed?

  1. A.No; the later delete removes it from every layer of the image
  2. B.No; Docker encrypts all files inside public images by default
  3. C.Yes; earlier layers still hold the file, so rotate the password now
  4. D.Only if someone runs the container with root privileges
Show answer

Answer: C. Yes; earlier layers still hold the file, so rotate the password now

Each Dockerfile instruction creates a layer, and images ship all layers. Anyone can pull the image and extract the earlier layer with the file. Rotate the secret, remove the image, and use build secrets (BuildKit --secret) or runtime injection instead.

Docker interview questions for experienced candidates hard

21. Unstyled site in Docker

A Docker image built with output: 'standalone' starts, but CSS and JS return 404. What was likely missed?

  1. A.Running npm install inside the final image
  2. B.Setting NODE_ENV to development in Docker
  3. C.Copying .next/static and public next to server.js
  4. D.Adding 'use client' to the root layout file
Show answer

Answer: C. Copying .next/static and public next to server.js

The standalone folder holds the server and its needed node_modules, but not static assets by design, since they are often served by a CDN. Copy .next/static into .next/standalone/.next/static and copy public too.

22. Container ignores SIGTERM

Pods with this image always take the full 30-second grace period to stop and are then SIGKILLed, dropping requests. What is the most likely cause?

FROM python:3.12-slim
COPY app.py .
CMD python app.py
  1. A.Shell form CMD runs /bin/sh -c as PID 1, which does not forward SIGTERM to the Python process; use exec form CMD ["python", "app.py"] and handle SIGTERM in the app.
  2. B.The python:3.12-slim base image blocks all signals by default.
  3. C.terminationGracePeriodSeconds must be set to 0 for signals to be delivered.
  4. D.Kubernetes sends SIGINT, not SIGTERM, so Python ignores it.
Show answer

Answer: A. Shell form CMD runs /bin/sh -c as PID 1, which does not forward SIGTERM to the Python process; use exec form CMD ["python", "app.py"] and handle SIGTERM in the app.

With shell form, the shell becomes PID 1 and Python is its child. The shell usually does not pass SIGTERM on, and PID 1 has no default signal handlers, so nothing stops until the kubelet sends SIGKILL. Exec form makes Python PID 1 directly; the app should still handle SIGTERM, or you can use an init such as tini.

23. Same commit, different build result

Rebuilding last month's commit now fails, though no code changed. The Dockerfile uses FROM node:20 and the build runs `npm install` without a lockfile. What is the root problem?

  1. A.The CI runner clock is set to a different time zone
  2. B.Docker images expire after 30 days in the registry
  3. C.Git commits change their content after a month
  4. D.Unpinned inputs: the base image and packages changed since then
Show answer

Answer: D. Unpinned inputs: the base image and packages changed since then

The build depends on moving inputs: node:20 points to new images over time, and without a lockfile npm resolves new package versions. Pin the base image by digest, commit the lockfile, use npm ci, and record tool versions. Then the same commit gives the same result.

24. Docker cache on fresh runners

You moved to ephemeral CI runners, and Docker builds that took 2 minutes now take 12 because every layer is rebuilt. How do you keep layer caching?

  1. A.Use docker build --no-cache so builds stay consistent
  2. B.Turn the ephemeral runners back into long-lived ones
  3. C.Export the BuildKit cache to a registry with --cache-to and reuse it
  4. D.Squash the image into one layer before pushing
Show answer

Answer: C. Export the BuildKit cache to a registry with --cache-to and reuse it

Each fresh runner starts with an empty local cache. BuildKit can push cache to a registry or the CI cache service (--cache-to type=registry,ref=..., or type=gha) and load it with --cache-from on the next build. You keep clean runners and get fast builds back.

25. SBOM from source vs from the image

An SBOM generated from your repo lists 120 npm packages. An SBOM generated from the final container image lists 310 components. Which one better shows what runs in production, and why?

  1. A.The source SBOM, since images add only harmless files
  2. B.The image SBOM, as it includes OS packages and build output
  3. C.Both are equal, and the count differs only by format
  4. D.Neither, because SBOMs cannot list OS packages
Show answer

Answer: B. The image SBOM, as it includes OS packages and build output

A source SBOM sees your manifests and lockfiles. The image SBOM also sees the base OS packages, system libraries and anything added during the build, which is what actually ships. Generate SBOMs from the final artifact for vulnerability response, and keep source ones for development.

26. Scale-out slowed by image pulls

During a traffic spike, new nodes join quickly but new Pods take about 4 minutes to start because the 7 GB image is pulled first. The app itself starts in 20 seconds. What helps most?

  1. A.Raise the readiness probe initialDelaySeconds value
  2. B.Shrink the image and pre-pull it or use lazy pulling
  3. C.Set imagePullPolicy: Always on the Deployment spec
  4. D.Give the Pods a higher PriorityClass than others
Show answer

Answer: B. Shrink the image and pre-pull it or use lazy pulling

Image pull time is part of startup for every Pod on a fresh node. Removing build tools and unused layers, baking the image into the node image, or using lazy or streaming pulls (such as SOCI or GKE image streaming) cuts that time. imagePullPolicy: Always makes it worse, and priority does not speed downloads.

27. Container takes 10 seconds to stop

docker stop always waits the full grace period and then kills your container. The app runs as PID 1 via a shell script and has no SIGTERM handler. Why does SIGTERM not stop it?

  1. A.Containers block all signals until the grace period ends
  2. B.The kernel gives PID 1 no default action for signals it has not handled
  3. C.SIGTERM is only delivered to processes with a terminal
  4. D.Docker sends SIGTERM to the host, not the container
Show answer

Answer: B. The kernel gives PID 1 no default action for signals it has not handled

For the init process of a PID namespace, signals without a handler are ignored, so default "terminate on SIGTERM" does not apply. Also a shell wrapper may not forward signals to its child. Use exec in the script so the app is PID 1 with a handler, or an init like tini (docker run --init).

28. Same tag, different image

Two pods of the same deployment behave differently. Both use image "scorer:latest", but one was pulled before a new push. What prevents this?

  1. A.Restarting pods every hour
  2. B.Using a larger node
  3. C.Setting imagePullPolicy to Never
  4. D.Deploying immutable image references by digest (or unique version tags) recorded with the model release
Show answer

Answer: D. Deploying immutable image references by digest (or unique version tags) recorded with the model release

Tags such as latest are mutable, so the same name can point to different images over time. A digest (sha256) identifies exact content, which makes deployments reproducible and auditable. Combine this with signed images and recorded model artifact hashes for supply chain safety.

29. Image tag now points elsewhere

Training runs in "trainer:latest". A rerun months later gives different results, though the tag is the same. What makes the environment fixed?

  1. A.Use the tag "stable" instead of "latest"
  2. B.Rebuild the image before every run
  3. C.Reference the image by its sha256 digest
  4. D.Store the Dockerfile in a different folder
Show answer

Answer: C. Reference the image by its sha256 digest

Tags are movable labels and can point to new builds. A digest such as trainer@sha256:… identifies exact image content. Logging the digest with each run makes the environment reproducible.

30. Old run cannot be rebuilt

You must reproduce a model trained 8 months ago. Its Dockerfile used "FROM python:3.10-slim", and that build of the base image is no longer the same. What should the team have done?

  1. A.Used the "latest" tag so it was always kept current
  2. B.Rebuilt the image weekly from the very same Dockerfile
  3. C.Kept only the Dockerfile in Git and never the image
  4. D.Pinned the base image digest and mirrored it internally
Show answer

Answer: D. Pinned the base image digest and mirrored it internally

Public tags move as new patches ship, and old builds can disappear. Pinning base images by digest, keeping copies in an internal registry and storing the final image with the run make old environments recoverable.

Ready to test yourself?

Take a 15-question Docker MCQ test with a score at the end, or practise the full Docker question bank and earn points on the leaderboard.