Docker interview questions

13 questions from the Docker Fundamentals: Images, Containers and Compose course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 2: Images & Containers

1. Which command shows containers that have already stopped?

  1. docker ps
  2. docker images
  3. docker ps -a
  4. docker run
Show answer

Answer: docker ps -a

Plain `docker ps` only shows running containers; `-a` includes stopped ones too.

Lesson: Container Lifecycle

Module 5: Data & Volumes

2. What happens to data written inside a container's writable layer after `docker rm`?

  1. It's automatically backed up
  2. It moves to the next container
  3. It is copied to Docker Hub
  4. It's permanently lost
Show answer

Answer: It's permanently lost

Without a volume or bind mount, data lives only in the container's writable layer and is deleted with it.

Lesson: Containers Are Ephemeral

Module 6: Networking

3. Which command opens a shell in a running container?

  1. docker exec -it web sh
  2. docker build -t web .
  3. docker pull web
  4. docker rm web
Show answer

Answer: docker exec -it web sh

exec runs a command inside an existing container.

Lesson: Inspecting & Debugging Containers

Module 7: Docker Compose

4. What does `docker compose down -v` do differently from a plain `docker compose down`?

  1. It also removes named volumes
  2. It runs in verbose mode
  3. It only stops, doesn't remove containers
  4. It only restarts services
Show answer

Answer: It also removes named volumes

The -v flag additionally deletes named volumes declared in the compose file, wiping persisted data.

Lesson: compose up / down

Module 10: Best Practices

5. Why should a container avoid running its process as root?

  1. It makes the image build faster
  2. It limits the damage if an attacker breaks out of the container
  3. Root users can't use volumes
  4. It disables networking
Show answer

Answer: It limits the damage if an attacker breaks out of the container

Running as a non-root user reduces the privileges available if the container is compromised.

Lesson: Security Basics

6. What is a container image?

  1. A running process
  2. A virtual disk drive
  3. A read-only template used to start containers
  4. A network
Show answer

Answer: A read-only template used to start containers

Containers are running instances of images.

Lesson: Security Basics

7. Which flag publishes a container port to the host?

  1. -e
  2. -v
  3. -d
  4. -p
Show answer

Answer: -p

-p host:container maps ports.

Lesson: Security Basics

8. How do you keep database data after a container is removed?

  1. Use a named volume
  2. Write inside the container
  3. Use --rm
  4. Use EXPOSE
Show answer

Answer: Use a named volume

Volumes live outside the container's writable layer.

Lesson: Security Basics

9. What does EXPOSE in a Dockerfile do?

  1. Publishes the port
  2. Documents the intended port only
  3. Opens the firewall
  4. Starts the app
Show answer

Answer: Documents the intended port only

Publishing needs -p or Compose ports.

Lesson: Security Basics

10. Which Compose command stops and removes the stack?

  1. docker compose up
  2. docker compose logs
  3. docker compose down
  4. docker compose pull
Show answer

Answer: docker compose down

Add -v to remove named volumes.

Lesson: Security Basics

11. Why use a multi-stage build?

  1. To skip tests
  2. To avoid tags
  3. To use root
  4. To ship a smaller final image
Show answer

Answer: To ship a smaller final image

Build tools stay in the build stage.

Lesson: Security Basics

12. Why avoid the latest tag in production?

  1. It makes rollbacks and audits hard
  2. It is slower
  3. It is private
  4. It disables logs
Show answer

Answer: It makes rollbacks and audits hard

Pin versions or commit hashes.

Lesson: Security Basics

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

  1. It builds faster
  2. It limits damage if the app is compromised
  3. It enables volumes
  4. It saves RAM
Show answer

Answer: It limits damage if the app is compromised

Least privilege reduces risk.

Lesson: Security Basics

← Back to the course