Docker interview questions: answers with example Dockerfiles
Images, layers, volumes and networks, explained in plain words with the Dockerfiles and commands interviewers ask you to read.
Docker interviews test whether you understand images versus containers, how a Dockerfile builds layers (and how caching works), how data persists (volumes and bind mounts), how containers talk to each other, and how to build small, secure images with multi-stage builds. Below are 19 questions from basic to advanced, each with a short answer and, where it helps, an example.
Basic docker interview questions
Basic1. What is the difference between an image and a container?
An image is a read-only template: a stack of filesystem layers plus metadata such as the default command. A container is a running (or stopped) instance of an image with its own writable layer, process space and network settings. One image can start many containers.
Basic2. What is a Dockerfile?
A Dockerfile is a text file of instructions that builds an image: a base image (FROM), files to copy (COPY), commands to run at build time (RUN), and the default command (CMD or ENTRYPOINT).
Dockerfile · Example (not executed)FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER 1000
EXPOSE 8000
CMD ["python", "-m", "app"]Basic3. How do image layers and the build cache work?
Each instruction that changes the filesystem creates a layer. Docker reuses a cached layer if the instruction and its inputs are unchanged, but once one layer changes, every later layer is rebuilt. That is why you copy the dependency list and install dependencies before copying the rest of the source code.
Basic4. What is the difference between CMD and ENTRYPOINT?
ENTRYPOINT sets the executable that always runs; CMD sets default arguments (or a default command) that docker run arguments replace. A common pattern is ENTRYPOINT for the program and CMD for its default flags. Use the JSON (exec) form so signals reach the process.
Basic5. What is the difference between COPY and ADD?
COPY copies files from the build context into the image. ADD can also extract local tar archives and fetch URLs. Docker's guidance is to prefer COPY unless you need ADD's extra behavior, because it is more predictable.
Basic6. What is the difference between a volume and a bind mount?
A volume is storage managed by Docker, kept outside the container's writable layer and independent of the host's directory layout; it is the usual choice for databases. A bind mount maps a specific host path into the container, which suits local development. Data written only to the container's writable layer is lost when the container is removed.
Basic7. How do containers communicate?
On a user-defined bridge network, containers reach each other by container name through Docker's built-in DNS. The default bridge network does not provide that name resolution. host networking shares the host's network stack, and none disables networking. Publishing a port (-p 8080:80) exposes a container port on the host.
Shell · Example (not executed)docker network create shop
docker run -d --name db --network shop -e POSTGRES_PASSWORD=example postgres:16
docker run -d --name api --network shop -p 8080:8000 example/api:1.0
# inside api, the database is reachable at db:5432Basic8. Which docker run options do you use most often?
-d runs in the background, -p publishes a port, -e sets an environment variable, -v mounts a volume or path, --name names the container and --rm removes it when it exits.
Shell · Example (not executed)docker run --rm -it -p 8000:8000 -e APP_ENV=dev -v "$PWD":/app --name dev example/api:1.0Intermediate docker interview questions
Intermediate9. What is a multi-stage build?
A multi-stage build uses several FROM stages in one Dockerfile: one stage with compilers and build tools produces the artifact, and the final stage copies only that artifact onto a small runtime base. The result is a smaller image with fewer tools an attacker could use.
Dockerfile · Example (not executed)FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/server ./cmd/server
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/server /server
USER nonroot
ENTRYPOINT ["/server"]Intermediate10. How do you make an image smaller?
Start from a slim or distroless base, use a multi-stage build, keep build tools out of the final stage, combine related RUN steps and clean package caches in the same step, and use .dockerignore so the build context excludes files such as .git and local test data.
Intermediate11. What is Docker Compose for?
Compose defines a multi-container application in one YAML file: services, their images or build settings, ports, environment, volumes and networks. docker compose up starts everything together, which makes it a common tool for local development and simple deployments.
YAML · Example (not executed)services:
api:
build: .
ports: ["8080:8000"]
environment:
DATABASE_URL: postgresql://app:example@db:5432/app
depends_on: [db]
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: example
volumes: ["dbdata:/var/lib/postgresql/data"]
volumes:
dbdata: {}Intermediate12. How are containers different from virtual machines?
A virtual machine runs a full guest operating system on a hypervisor. Containers share the host's kernel and isolate processes with Linux namespaces and cgroups, so they start in seconds and use less memory, but the isolation boundary is thinner than a VM's.
Intermediate13. What does .dockerignore do?
It lists files and folders to leave out of the build context sent to the builder, such as .git, node_modules, logs and secrets. That speeds up builds, keeps the cache stable and avoids copying sensitive files into images by accident.
Intermediate14. A container exits right after starting. How do you find out why?
Read its logs, check the exit code and state with docker inspect, and run the image interactively with a shell to test the command by hand. Common causes are a missing environment variable, a wrong command, a process that runs in the background (so the main process ends), or a crash on startup.
Shell · Example (not executed)docker ps -a --filter name=api
docker logs api
docker inspect api --format '{{.State.ExitCode}} {{.State.Error}}'
docker run --rm -it --entrypoint sh example/api:1.0Intermediate15. How should you handle secrets with Docker?
Never bake secrets into an image: anything in a layer can be read by anyone who has the image. Pass them at runtime through the orchestrator's secret mechanism or environment injected by the platform, and use build secrets (RUN --mount=type=secret) when a build step needs a credential.
Advanced docker interview questions
Advanced16. What happens when you run docker run?
The client sends the request to the Docker daemon, which pulls the image if it is not present, creates a container with a writable layer, sets up namespaces (process, network, mount and others) and cgroups for resource limits, attaches networking, and starts the configured process as the container's main process.
Advanced17. How do you secure a container image?
Run as a non-root user, start from a minimal and regularly updated base, scan images for known vulnerabilities, pin versions, keep secrets out of layers, and at runtime drop unneeded Linux capabilities and use a read-only root filesystem where the app allows it.
Advanced18. What does HEALTHCHECK do?
HEALTHCHECK tells Docker how to test whether the app inside a container is working, so docker ps shows healthy or unhealthy. Orchestrators such as Kubernetes ignore it and use their own probes instead.
Dockerfile · Example (not executed)HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD wget -qO- http://localhost:8000/healthz || exit 1Advanced19. Why should you avoid the latest tag in production?
latest is just a tag name: it moves whenever someone pushes, so two deployments of image:latest can run different code and a rollback cannot target a known version. Deploy a specific version tag, or the image digest (image@sha256:...), which never changes.
How to practice with this page
- Containerize a small app you know, then shrink the image with a multi-stage build.
- Explain each line of your Dockerfile and why it is in that order.
- Run two containers on a user-defined network and connect them by name.
- Be ready to compare containers with virtual machines in one minute.
Related interview prep
Disclosure: TryHired makes HIRED, a paid job-search program, so we have a conflict of interest when we mention it. This question bank is free, with no TryHired signup.
Questions people ask
What Docker questions come up in interviews?
Do I need Kubernetes knowledge for a Docker interview?
How do I practice Docker for an interview?
Is Docker still used if Kubernetes uses containerd?
What is the difference between docker stop and docker kill?
How this page is built
TryHired wrote these questions and answers. Blocks marked Example (not executed) are configuration, commands or formulas that cannot run on the build machine: no output is claimed for them. The question list is general practice: it does not predict what a particular employer will ask.
- Interviewers ask different questions for different roles and levels.
- Answers are short explanations, not complete references.
- Docker Desktop, Docker Engine and other runtimes (Podman, containerd) differ in details.
- Docker documentation, Dockerfile reference: https://docs.docker.com/reference/dockerfile/
- Docker documentation, build best practices: https://docs.docker.com/build/building/best-practices/
- Docker documentation, volumes: https://docs.docker.com/engine/storage/volumes/
- Docker documentation, networking overview: https://docs.docker.com/engine/network/