Docker Interview Questions

Master Docker with these real-world interview questions and answers.

Switch Topic:

Beginner Questions

Core concepts, syntax, and foundational command-line knowledge.

Easy Associate Level Docker
Q:

What is the difference between Docker COPY and ADD instructions?

Both copy files into the image, but ADD has extra functionality that makes it unpredictable:

  • ADD can fetch files from a URL
  • ADD auto-extracts tar archives into the destination

Best practice: Always use COPY unless you specifically need the URL or auto-extraction features. COPY is explicit and predictable, which is better for reproducible builds.

Easy Associate Level Docker
Q:

What is the purpose of ENTRYPOINT vs CMD in a Dockerfile?

CMD provides default arguments for the container. It can be overridden by passing arguments to docker run.

ENTRYPOINT defines the fixed command that always runs. It cannot be overridden without --entrypoint flag.

Best practice: Use ENTRYPOINT for the executable and CMD for default arguments, making the container behave like a command-line tool:

ENTRYPOINT ["python", "app.py"]
CMD ["--port", "8080"]
# docker run myapp --port 9090  ← overrides CMD only
Easy Associate Level Docker
Q:

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

A Docker image is a read-only template built from a Dockerfile. Think of it as a class definition. A container is a running instance of that image — a class instantiation. You can run many containers from the same image, each isolated from the others.

# Build an image
docker build -t my-app:1.0 .

# Run a container from that image
docker run -d -p 8080:80 my-app:1.0

Intermediate Questions

Infrastructure management, deployment strategies, and delivery flows.

Medium Senior Level Docker
Q:

Explain the concept of a distroless image and its security benefits.

A distroless image contains only your application and its runtime dependencies — no shell, no package manager, no OS utilities. This comes from Google’s distroless project.

Security benefits: You cannot exec into a distroless container and run arbitrary commands. The attack surface is dramatically reduced because there are no standard Unix tools an attacker could use to move laterally.

# Distroless multi-stage example
FROM golang:1.22 AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -o server .

FROM gcr.io/distroless/static-debian12
COPY --from=builder /app/server /server
CMD ["/server"]
Medium Senior Level Docker
Q:

How do you reduce Docker image size? Walk through your optimization strategy.

Image size directly affects pull times and attack surface. Key strategies:

  1. Use minimal base images: alpine or distroless instead of ubuntu.
  2. Multi-stage builds: Build in a full image, copy only the binary/artifact to a slim final image.
  3. Combine RUN commands: Each RUN creates a layer. Chain commands with && and clean up in the same layer.
  4. Use .dockerignore: Exclude node_modules, .git, test files from the build context.
# Multi-stage example
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm ci && npm run build

FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/index.js"]

Advanced Questions

Enterprise orchestration, deep architectural concepts, and scaling issues.

Hard Lead / Architect Level Docker
Q:

How do you implement health checks in Docker and why are they important for orchestration?

The HEALTHCHECK instruction tells Docker how to test if a container is working correctly. Without it, Docker considers a container healthy as soon as the process starts — even if the app inside has crashed.

HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:8080/health || exit 1

In Kubernetes, this is replaced by Liveness and Readiness probes. In Docker Compose or standalone Docker, HEALTHCHECK is critical for orchestration tools to know whether to send traffic to a container.

Hard Lead / Architect Level Docker
Q:

How would you run containers as a non-root user for security hardening?

Running containers as root is a significant security risk. If an attacker escapes the container, they have root on the host. Harden your images:

FROM node:20-alpine

# Create a non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Set working directory and permissions
WORKDIR /app
COPY --chown=appuser:appgroup . .

# Switch to non-root user
USER appuser

CMD ["node", "index.js"]

Also enforce this at the Kubernetes level with a SecurityContext: runAsNonRoot: true.

Real Production Scenarios

Real-world architecture, system migration, and design challenges.

Easy Associate Level Docker
Q:

What is Docker Compose and when would you use it?

Docker Compose is a tool for defining and running multi-container applications using a YAML file. It is ideal for local development and testing where you need to spin up interdependent services (app + database + cache) with a single command.

docker compose up -d

It handles networking (all services in the same file can reach each other by service name), volume management, and environment variables. For production orchestration, use Kubernetes instead.

Medium Senior Level Docker
Q:

How do Docker volumes differ from bind mounts?

Docker Volumes are managed by Docker, stored in /var/lib/docker/volumes/, and are the recommended way to persist data. They are portable, easy to back up, and work well with Docker Compose.

Bind Mounts map a specific host path directly into the container. They are useful in development to sync source code in real-time but are host-dependent and harder to manage in production.

# Volume (recommended for production)
docker run -v mydata:/app/data myapp

# Bind mount (recommended for development)
docker run -v $(pwd)/src:/app/src myapp
Medium Senior Level Docker
Q:

Explain Docker layer caching and how it impacts build speed.

Docker builds images layer by layer. If a layer hasn’t changed since the last build, Docker reuses the cached version. The trick is layer ordering:

Bad: COPY all files first, then run npm install. Any code change invalidates the npm install cache.

Good: COPY package.json first, run npm install, then COPY the rest of the source. Dependency installation only re-runs when package.json changes.

# Optimized layer order
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
Hard Lead / Architect Level Docker
Q:

How do you scan Docker images for vulnerabilities in a CI/CD pipeline?

Image scanning should be a mandatory gate before pushing to production. Tools and integration steps:

  • Trivy (Aqua): Fast, comprehensive, easy CI integration. trivy image myapp:latest
  • Snyk: Deep dependency scanning with developer-friendly output.
  • Docker Scout: Built into Docker Hub.
  • Grype: From Anchore, works well with SBOM workflows.
# GitHub Actions example
- name: Scan image with Trivy
  uses: aquasecurity/trivy-action@master
  with:
    image-ref: myapp:${{ github.sha }}
    severity: CRITICAL,HIGH
    exit-code: 1  # Fail the pipeline on critical vulnerabilities

Troubleshooting Scenarios

Live system debugging, incident diagnostics, and latency resolution.

Medium Senior Level Docker
Q:

What are dangling Docker images and how do you clean them up?

Dangling images are layers that have no associated tag — they appear as <none>:<none> in docker images. They accumulate over time from rebuilds and waste disk space.

# List dangling images
docker images -f dangling=true

# Remove all dangling images
docker image prune

# Nuclear option — remove all unused images, containers, networks, volumes
docker system prune -a --volumes

In CI/CD pipelines, always run docker system prune -f as a post-step to keep agents clean.