Ravindra BagaleCourses & study guides Track your progress

Guides

Docker Interview Questions and Answers (30, Freshers and Scenario Based)

These are the Docker interview questions asked most often, from fresher basics to scenario rounds, with short answers and commands you can run to prove them. Practise on a free-tier EC2 instance or your laptop. When you can explain image → container → ports, volumes and networks and walk through a scenario like "the container exits right away", you are ready for most Docker rounds.

Five commands to know cold

docker build -t orders-api:1.0 .
docker run -d --name api -p 8080:3000 orders-api:1.0
docker ps -a
docker logs api
docker exec -it api sh

The flow every answer comes back to

Docker build and run flow A Dockerfile is built into an image, pushed to a registry and run as a container with ports, volumes and networks. Docker build and run flow DockerfilestepsbuildlayersImageread-onlyrun-p -vContainerprocess you Most Docker questions are about one box in this flow

Most questions come back to this flow: Dockerfile → image → container, plus ports, volumes and networks.

Basics

Q1. What is Docker?

A tool to package an app with everything it needs (runtime, libraries, config) into an image, and run it as an isolated container on any machine with a container engine.

Q2. Image vs container?

An image is a read-only template, like a class. A container is a running instance of it, like an object. docker run nginx creates a container from the nginx image.

Q3. Container vs virtual machine?

A VM has its own full OS kernel on a hypervisor. Containers share the host kernel and isolate processes with namespaces and cgroups, so they start in seconds and use less memory.

Q4. What is a Dockerfile?

A text file with build steps (FROM, COPY, RUN, CMD) that docker build turns into an image, one layer per instruction.

Q5. CMD vs ENTRYPOINT?

ENTRYPOINT is the fixed program. CMD gives default arguments that docker run image args can replace. Example: ENTRYPOINT ["python"], CMD ["app.py"].

Q6. COPY vs ADD?

COPY copies files. ADD can also unpack local tar files and download URLs. Use COPY unless you need those extras, because it is clearer.

Q7. What is a Docker volume?

Storage managed by Docker that lives outside the container's writable layer, so data survives when the container is removed. Databases like MySQL and PostgreSQL need one.

Q8. Bind mount vs volume?

A bind mount maps a host folder (-v /home/ravi/site:/usr/share/nginx/html). A named volume (-v dbdata:/var/lib/mysql) is managed by Docker. Use volumes for data, bind mounts for local development.

Networking and commands

Q9. What does -p 8080:80 mean?

Host port 8080 forwards to container port 80. Open http://server:8080 to reach nginx listening on 80 inside.

Q10. How do two containers talk to each other?

Put them on the same user-defined network (docker network create app). They reach each other by container name, for example mysql:3306.

Q11. How do you see why a container stopped?

docker ps -a for the status and exit code, then docker logs <name>, then docker inspect <name> for details such as OOMKilled.

Q12. How do you get a shell in a running container?

docker exec -it <name> sh (or bash if the image has it).

Q13. What does docker system prune do?

It removes stopped containers, unused networks, dangling images and build cache to free disk. -a also removes unused images, and --volumes removes unused volumes, which can delete data.

Building better images

Q14. What is a multi-stage build?

Using one stage with build tools (Maven, Go, npm) and copying only the result into a small runtime stage. The final image is smaller and has fewer vulnerabilities.

Q15. How do you make builds faster?

Order the Dockerfile so rarely changing steps come first: copy package files and install dependencies before copying the source, so the dependency layer is cached.

Q16. Why use .dockerignore?

To keep .git, node_modules, .env files and test data out of the build context and the image. It speeds builds and avoids leaking secrets.

Q17. Why not run containers as root?

If the app is broken into, root inside the container makes escaping to the host and changing files easier. Add a USER line with a normal user.

Q18. Why avoid the latest tag?

latest changes without warning, so two servers can run different code. Pin versions like nginx:1.27 and use the git SHA for your own images.

Scenario round

Q19. Your container exits right after it starts. What do you check?

docker ps -a for the exit code, then docker logs. Exit 0 often means the main process ran in the background and finished; exit 1 is an app error; 137 is memory.

Q20. The image is 1.5 GB. How do you shrink it?

Use a slim or Alpine base, a multi-stage build, npm ci --omit=dev or pip --no-cache-dir, clean package caches in the same RUN line, and add a .dockerignore.

Q21. A container cannot reach MySQL on localhost. Why?

Inside a container, localhost is the container itself. Use the MySQL container's name on a shared network, or the host's IP.

Q22. The disk fills up on a Docker host. What do you do?

docker system df to see what uses space, prune images and build cache, and set log rotation (max-size, max-file) for the json-file driver.

Q23. How do you pass secrets to a container safely?

Not in the Dockerfile or image. Use environment values from a secret store at runtime (AWS Secrets Manager, Kubernetes Secrets), or BuildKit --secret for build-time needs.

Q24. Docker Hub returns toomanyrequests in CI. Fix?

Log in with an account, or mirror base images to Amazon ECR (pull-through cache) or another private registry.

Q25. How do you run the same app on dev, test and prod?

One image, different config through environment variables or mounted config files. Never build a separate image per environment.

A live example you can show

This compose file runs a Node.js API with MySQL, a shared network and a volume, which covers questions 7 to 10 in one go:

services:
  api:
    image: orders-api:1.0
    ports: ["8080:3000"]
    environment:
      DB_HOST: mysql
    depends_on: [mysql]
  mysql:
    image: mysql:8.4
    environment:
      MYSQL_ROOT_PASSWORD: Lab@12345
      MYSQL_DATABASE: orders
    volumes: ["dbdata:/var/lib/mysql"]
volumes:
  dbdata:

Run docker compose up -d, then docker compose down and up again, and show the data is still there because of the volume.

Summary table

Area Must-know
Basics Image vs container, Dockerfile, CMD vs ENTRYPOINT
Storage Volume vs bind mount
Network -​p host:​container, user-defined networks, names as hostnames
Debug ps -a, logs, exec, inspect, exit codes
Images Multi-stage, caching, .dockerignore, pinned tags
Security Non-root, no secrets in images, scan with Trivy

Quick quiz: check what you learned

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

    1. A container runs from an image
    2. An image runs inside a container
    3. They are two names for one thing
    4. A container builds the Dockerfile
    Show answer

    A. The image is the template; the container is the running instance.

  2. What does -p 8080:80 do?

    1. Maps host 8080 to container 80
    2. Maps container 8080 to host 80
    3. Opens 8080 to 80 in the firewall
    4. Limits the container to 80 MB
    Show answer

    A. The host port comes first, the container port second.

  3. Inside a container, what does localhost point to?

    1. The container itself
    2. The Docker host machine
    3. The Docker Hub registry
    4. The default gateway router
    Show answer

    A. Each container has its own network namespace.

  4. Why use a multi-stage build?

    1. Smaller and safer final image
    2. Two apps in one container
    3. Faster Docker Hub uploads
    4. Root access during runtime
    Show answer

    A. Build tools stay out of the image that runs.

Learn it properly

These pages go deeper, with labs:

Frequently asked questions

What Docker questions are asked to freshers?

Image vs container, Dockerfile instructions, CMD vs ENTRYPOINT, ports, volumes, basic commands like ps, logs and exec, and why containers are lighter than VMs.

What do experienced interviews add?

Multi-stage builds, layer caching, networking between containers, image security, registries, and troubleshooting scenarios.

Is Docker still used with Kubernetes?

Kubernetes runs containers with containerd now, but Docker is still the most common tool to build images, and those images run on Kubernetes unchanged.

Should I learn Docker Compose for interviews?

Yes. Be ready to explain a compose file with an app and a database, a shared network and a volume.

How do I show hands-on proof?

Keep a GitHub repo with a multi-stage Dockerfile, a compose file and a CI job that builds and scans the image.

What is the most common mistake in answers?

Saying containers are "small VMs". Explain kernel sharing, namespaces and cgroups instead.