Docker Interview Questions and Practical Exercises

Review how images, containers and build layers work. Then build a small service, diagnose a port that fails from the host and verify that a Compose database is ready and keeps its data. In a live round, Hiintly can use spoken questions and screenshots to help you reason through the next check.

Docker interview questions and answers

A technical screen may start with a definition and then ask you to predict what will happen after a build, a restart or a deployment. Explain which state changes and where it lives. In a practical round, use a command that confirms your answer.

What is the difference between an image and a container?

An image contains filesystem layers and defaults such as the command to run. A container is an instance created from that image with its own configuration and writable layer. You can run two containers from the same image with different environment variables and port mappings.

Stopping and starting the same container preserves its writable layer. Recreating the container discards that layer. Keep data that must outlive the container in a volume or another durable store.

What is the difference between CMD and ENTRYPOINT?

ENTRYPOINT sets the executable in an image designed to run a particular program. CMD can supply its default arguments. With an exec-form ENTRYPOINT, arguments after the image name in docker run replace CMD and are passed to the entrypoint. Use --entrypoint to override the executable itself.

If ENTRYPOINT is ["myapp"] and CMD is ["serve"], docker run myimage inspect runs myapp inspect. With no entrypoint, CMD can define the whole default command.

When do you choose a volume or a bind mount?

Choose a named volume for data Docker should manage, such as a local database directory. A bind mount connects a specific host path, which suits source files you edit in development. A mount over an existing image path can hide the files that were built into that path.

If a container starts but an expected file is missing, inspect its mounts before rebuilding the image. The same image can work without the mount and fail with it.

What invalidates the Docker build cache?

Docker can reuse a build step when its instruction and relevant inputs match a previous build. If COPY changes an input used by that step, downstream steps in the same build chain need to run again. Copy dependency manifests before application source so routine source edits can reuse the dependency-install step.

An unchanged RUN instruction can remain cached even if a package registry has newer packages. Use locked inputs for reproducible builds and an explicit update policy for packages and base images.

Three practical Docker exercises

Use a local Docker installation with Compose v2. Run the commands in a Bash-compatible terminal. The first two exercises share one image. The Compose exercise uses its own directory and a disposable database.

Build a runtime image for a small API

The interviewer wants an image that serves /health, runs as a non-root user and ships without the Go compiler. Put the two files in an empty directory. Docker builds the program, so this exercise requires no local Go installation.

What should pass

The health request prints ok and the inspection prints 10001:10001. The final image starts from Alpine and receives the compiled binary from the build stage.

main.go

package main

import (
    "log"
    "net/http"
    "os"
)

func main() {
    addr := os.Getenv("LISTEN_ADDR")
    if addr == "" {
        addr = "0.0.0.0:8080"
    }
    http.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
        w.Write([]byte("ok\n"))
    })
    log.Printf("listening on %s", addr)
    log.Fatal(http.ListenAndServe(addr, nil))
}

Dockerfile

FROM golang:1.25-alpine AS build
WORKDIR /src
COPY main.go .
RUN CGO_ENABLED=0 go build -o /src/interview-api main.go

FROM alpine:3.22
WORKDIR /app
COPY --from=build /src/interview-api /app/interview-api
USER 10001:10001
EXPOSE 8080
ENTRYPOINT ["/app/interview-api"]

Build, run and verify

docker build -t interview-api .
docker run --rm -d --name interview-api \
  -p 127.0.0.1:8080:8080 interview-api
curl -fsS --retry 4 --retry-connrefused http://127.0.0.1:8080/health
docker inspect --format '{{.Config.User}}' interview-api
docker stop interview-api

A common answer is to delete the compiler in a later layer of the build image. The compiler would still be present in the earlier image layers. A separate final stage gives you a fresh base and copies only the artifact you need from the builder. Alpine retains a shell and basic tools for this lab. A smaller runtime can reduce shipped contents, with fewer tools for diagnosis.

Diagnose a published port that fails

The container stays up and Docker reports a published port. The browser or host terminal still cannot reach the API. Reproduce that failure by binding the app to loopback inside the container. Keep the host mapping loopback-only for the lab.

What should pass

The request inside the broken container prints ok. A host request to the published port fails. When you remove the LISTEN_ADDR override and run the first example again, the host request succeeds.

docker run --rm -d --name interview-loopback \
  -e LISTEN_ADDR=127.0.0.1:8080 \
  -p 127.0.0.1:8080:8080 interview-api
docker port interview-loopback 8080/tcp
docker logs interview-loopback
docker exec interview-loopback wget -qO- http://127.0.0.1:8080/health
curl -fsS http://127.0.0.1:8080/health
docker stop interview-loopback
  1. docker port shows 127.0.0.1:8080 on the host. docker logs shows the process also listens on 127.0.0.1:8080, but that address is inside the container.
  2. The internal request reaches the container’s loopback listener. Docker’s forwarding path reaches the container through its network interface, where the process is currently not accepting requests.
  3. Let the app listen on 0.0.0.0:8080 inside the container. Keep -p 127.0.0.1:8080:8080 so the published port binds to host loopback.

Adding EXPOSE 8080 only documents a port. Publishing it again cannot change the socket the app opened. If the application already listens on the right interface, compare its actual port with the container-side port in the mapping before changing either.

If this comes up during a live interview, capture the logs and port mapping in Hiintly and include where each request ran. Ask which observation isolates the failure. Then repeat the host request yourself to show the fix.

Docker Compose exercise: wait for the database and keep its data

A client runs a query as soon as it starts. It should wait for Postgres to accept connections, use the internal service address and find the same data after the project comes down and back up. Use a new directory for this compose.yaml. The credentials are for the local exercise.

What should pass

The client exits successfully after printing a row with ready equal to 1. After recreating the database container, the query still returns kept.

compose.yaml

services:
  db:
    image: postgres:17-alpine
    environment:
      POSTGRES_DB: interview
      POSTGRES_USER: interview
      POSTGRES_PASSWORD: interview-local
    healthcheck:
      test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "interview", "-d", "interview"]
      interval: 2s
      timeout: 2s
      retries: 15
    volumes:
      - interview_data:/var/lib/postgresql/data
  client:
    image: postgres:17-alpine
    depends_on:
      db:
        condition: service_healthy
    environment:
      PGHOST: db
      PGDATABASE: interview
      PGUSER: interview
      PGPASSWORD: interview-local
    command: ["psql", "-X", "-v", "ON_ERROR_STOP=1", "-c", "SELECT 1 AS ready;"]
volumes:
  interview_data:

Verify readiness and persistence

docker compose up -d
docker compose wait client
docker compose logs client

docker compose exec -T db psql -U interview -d interview \
  -c "CREATE TABLE IF NOT EXISTS notes (message text PRIMARY KEY);"
docker compose exec -T db psql -U interview -d interview \
  -c "INSERT INTO notes VALUES ('kept') ON CONFLICT DO NOTHING;"

docker compose down
docker compose up -d --wait db
docker compose exec -T db psql -U interview -d interview \
  -c "SELECT message FROM notes;"
docker compose down

Why can the database start and still fail the first query?

A started container can still be initializing. The short form of depends_on gives you startup order. The service_healthy condition waits for the dependency’s health check. The probe tests whether Postgres accepts connections. The client’s own query checks its credentials and its path from a different container.

Why should the client use db?

localhost inside the client refers to the client itself. Compose gives both services a shared network and resolves the service name db. A host port mapping serves a different purpose and is unnecessary for this query.

What happens when the database is replaced?

The named volume holds the database files beyond the life of a container. docker compose down leaves it available for reuse. Adding -v removes the project’s declared named volumes, including the exercise database, so leave it off for the persistence check.

A fixed sleep guesses how long startup takes. A health check observes readiness at startup. For a long-running application, explain how it retries connections if the database becomes unavailable after startup.

Docker interview questions for experienced candidates

For a senior backend or platform role, expect follow-ups about production constraints and how you validated a decision. Bring a real build or incident you can explain, including what you observed, what you changed and how you confirmed the outcome.

The image takes four minutes to build on every commit. Where do you start?

Use the build output to identify the first step that misses the cache. If installing dependencies follows COPY . ., move the lockfile and manifest ahead of the install and copy application code later. Keep logs, build output and local dependency directories out of the build context with .dockerignore. If CI uses fresh runners, check whether it imports and exports a shared BuildKit cache.

Time a rebuild after a source-only edit, then change the lockfile and confirm the dependency step runs again. A warm build and a clean build both need to produce the expected artifact.

A new image starts locally, then exits in CI with code 137. What do you check?

Code 137 is consistent with SIGKILL. Inspect .State.OOMKilled on the stopped container and check job timeout, memory limits and runner events before choosing an explanation. If memory is the issue, reproduce under a comparable limit and test whether the application needs less concurrency or more memory.

Avoid relying on the exit code alone. A job can be killed externally, with the same exit code, even when the container did not hit its memory limit.

You can redeploy the previous image, but the release also applied a schema migration. Can you roll back?

First check whether the previous binary works against the new schema. Verify the exact previous image digest, runtime configuration and current data state. Deleting a column or rewriting data can make an image rollback fail even when the older image starts.

Explain how you would validate compatibility with a representative read and write. For a planned change, an additive migration can let both application versions work during the rollout. Recovery from a destructive migration needs a separate data recovery plan.

If the round continues into CI/CD, Kubernetes rollout failures or incident response, see how a DevOps interview can develop from a technical question into a troubleshooting exercise.

Use Hiintly during a Docker interview

Choose Software Engineer in session setup and add your resume and the job description. In Extra Instructions, describe the stack and the kind of round you expect. If the interviewer asks about an image you owned, include the runtime, the deployment environment and the change you actually made. Hiintly can use those details with the spoken question to suggest a concise answer.

For a shared terminal exercise, capture the visible Dockerfile or command output and send the missing context in a message. In the Compose exercise, for example, share the healthcheck status and where the failing client runs. Ask Hiintly to suggest which check distinguishes readiness, connectivity and authentication.

“The database is healthy, and psql works inside the database container. The client gets connection refused when it uses localhost. What should I check next, and what result would confirm the cause?”

Use your terminal to verify the suggested step, then share any new output if the interviewer changes the constraint. For tasks that move from configuration to implementing the app itself, see the coding interview copilot walkthrough.

Docker interview FAQ

What does a Docker practical interview usually ask you to do?

A practical round might ask you to containerize an existing app, review a Dockerfile, diagnose a failing request or wire up services in Compose. Clarify which files you can change and how success will be checked. Be ready to explain a command’s expected result before running it.

How should I structure a scenario-based Docker interview answer?

Name the symptom and the boundary you are testing. For example, verify the listener inside the container before changing a host port mapping. Say what each possible result would rule out, make the smallest relevant change, then repeat the original failing check from the same place.

Do containers in Docker Compose need published ports to reach each other?

Services on the same Compose network can generally use the service name and container port, such as db:5432. Publish a port if a client outside that network needs it. A host port mapping is a different setting from the address the process listens on inside its container.

Which Docker commands should I know before a technical screen?

Practice docker build and docker run to start an image, then use docker ps -a, docker logs, docker inspect, docker port and docker exec to observe it. Know which commands work on a stopped container. For a Compose task, add docker compose config, ps, logs and exec.

Can I try Hiintly on a Docker question for free?

Start a free 10-minute session and share a spoken question or a screenshot of your Dockerfile, Compose configuration or terminal output. Ask for the next diagnostic step and verify it in your own terminal. You can start another free session after the five-minute cooldown.