Docker interview question · Most asked

What is Docker Compose used for, and how do you handle service start-up order?

Summary

Compose defines multi-container applications in a YAML file (compose.yaml): services, images or builds, ports, volumes, networks, environment and dependencies, and starts them together with docker compose up. It is the standard tool for local development environments and simple single-host deployments.

Longer Answer

depends_on controls start order, but by default only waits for containers to start, not for the service inside to be ready. Use depends_on with condition: service_healthy plus a healthcheck on the dependency (for example pg_isready for PostgreSQL), and still make applications retry connections, because dependencies can restart later too.

Other useful features: profiles for optional services, env_file, override files for per-developer settings, watch for syncing code changes, and include to combine projects.

Code Example

services:
  db:
    image: postgres:17
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      retries: 10
  api:
    build: .
    depends_on:
      db:
        condition: service_healthy

Key Points

  • Multi-container apps in YAML.
  • Great for local development.
  • depends_on + service_healthy + healthcheck.
  • Apps must still retry connections.

More most-asked Docker questions

← All Docker questions