Skip to content
Laviyx Learn

Try

Docker and Kubernetes Interview Questions

Frequently asked Docker and Kubernetes interview questions on images, containers, Dockerfiles, layers, volumes, networking, Compose, security, pods, deployments and troubleshooting, with hints and answers.

22 questions · Updated

  1. 1.What is the difference between an image and a container?

    BeginnerConceptBasics
    Show hint

    Template and running instance.

    Show solution

    An image is a read-only template that holds the app and its dependencies. A container is a running instance of an image, with its own writable layer.

  2. 2.How do containers differ from virtual machines?

    BeginnerConceptBasics
    Show hint

    What do they share?

    Show solution

    A virtual machine runs a full guest operating system. Containers share the host's kernel, so they are much smaller and start in seconds, though the isolation is weaker.

  3. 3.What does -p 8080:80 do?

    BeginnerConceptNetworking
    Show hint

    Which side is the host?

    Show solution

    It forwards port 8080 on the host to port 80 in the container. The left side is always the host.

  4. 4.What is the difference between RUN and CMD?

    BeginnerConceptDockerfile
    Show hint

    Build time or start time?

    Show solution

    RUN executes during the build, and saves the result in a layer. CMD sets the default command when a container starts.

  5. 5.How do you keep a database's data when its container is replaced?

    BeginnerConceptStorage
    Show hint

    Storage outside the container.

    Show solution

    Attach a named volume to the database's data folder, such as -v pgdata:/var/lib/postgresql/data.

  6. 6.Write a Dockerfile for a Python app that runs app.py.

    BeginnerCodingDockerfile
    Show hint

    Dependencies before code.

    Show solution
    DOCKERFILE
    FROM python:3.13-slim
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    COPY . .
    USER nobody
    CMD ["python", "app.py"]
  7. 7.A container exits as soon as it starts. Why might that be, and how do you check?

    BeginnerDebuggingDebugging
    Show hint

    What keeps a container alive?

    Show solution

    A container lives only as long as its main process. The process may have finished or crashed. Check docker ps -a for the exit code, and read docker logs.

  8. 8.Why does the order of Dockerfile instructions matter?

    IntermediateConceptDockerfile
    Show hint

    What happens after a changed step?

    Show solution

    A changed step rebuilds itself and everything after it. Put slow, rarely changing steps such as dependency installs first, and copy your code last.

  9. 9.What is a multi-stage build, and why use one?

    IntermediateConceptDockerfile
    Show hint

    Build tools are not needed at run time.

    Show solution

    It uses several FROM stages: one to build, and a final one that copies in only the result. The image is much smaller, and contains less that can be attacked.

  10. 10.How do you reduce an image's size?

    IntermediateConceptDockerfile
    Show hint

    Base image, stages and context.

    Show solution

    Use a slim or Alpine base, multi-stage builds and .dockerignore, and clean up in the same RUN step that installs packages.

  11. 11.What is the difference between a volume and a bind mount?

    IntermediateConceptStorage
    Show hint

    Who chooses the location?

    Show solution

    Docker manages a volume, which suits production data. A bind mount uses a host path that you choose, which suits development.

  12. 12.Why can't my app connect to the database at localhost?

    IntermediateDebuggingNetworking
    Show hint

    What does localhost mean inside a container?

    Show solution

    localhost is the container itself. Put both containers on one network, and connect using the database's service or container name.

  13. 13.What does exit code 137 mean?

    IntermediateDebuggingDebugging
    Show hint

    128 plus a signal number.

    Show solution

    The process received SIGKILL (128 + 9), usually from the out-of-memory killer. Check OOMKilled in docker inspect.

  14. 14.Why might an app fail at start-up in Compose, even with depends_on?

    IntermediateDebuggingCompose
    Show hint

    Started is not the same as ready.

    Show solution

    Plain depends_on waits only until the container starts, not until the database accepts connections. Add a health check, and use condition: service_healthy.

  15. 15.How should you handle secrets in Docker?

    IntermediateConceptSecurity
    Show hint

    Not in the image.

    Show solution

    Never put them in a Dockerfile or an image, because docker history can reveal them. Pass them at run time, ideally as mounted secret files, and use BuildKit secrets during builds.

  16. 16.Why run containers as a non-root user?

    IntermediateConceptSecurity
    Show hint

    What if the app is compromised?

    Show solution

    If an attacker gets into the app, they have fewer rights in the container, and less chance of escaping to the host. Add USER to the Dockerfile.

  17. 17.Which Linux features make containers work?

    AdvancedConceptInternals
    Show hint

    Isolate, limit, layer.

    Show solution

    Namespaces isolate processes, networks and mounts. cgroups limit CPU and memory. Union file systems such as overlay2 provide layered storage.

  18. 18.Why might docker stop take 10 seconds?

    AdvancedDebuggingInternals
    Show hint

    Who receives the signal?

    Show solution

    The app is not handling SIGTERM, often because it runs under a shell as PID 1. Docker waits, then kills it. Use the exec form of CMD, handle signals, or add --init.

  19. 19.What is the difference between a pod and a Deployment?

    AdvancedConceptKubernetes
    Show hint

    What brings a pod back?

    Show solution

    A pod is one or more containers, and does not come back if deleted. A Deployment keeps a set number of pods running, and handles rolling updates and rollbacks.

  20. 20.What is the difference between liveness and readiness probes?

    AdvancedConceptKubernetes
    Show hint

    Restart, or stop sending traffic?

    Show solution

    A failing readiness probe removes the pod from Service traffic. A failing liveness probe restarts the container.

  21. 21.Why do you need a Kubernetes Service?

    AdvancedConceptKubernetes
    Show hint

    Pod addresses change.

    Show solution

    Pods are replaced, and their IP addresses change. A Service gives a stable name and address for a group of pods, chosen by label, and balances traffic among them.

  22. 22.A pod is in CrashLoopBackOff. How do you investigate?

    AdvancedDebuggingKubernetes
    Show hint

    Previous logs and events.

    Show solution

    Run kubectl logs --previous and kubectl describe pod. Look for a bad config, a missing secret, a failing probe, or an out-of-memory kill.

Review the lessons