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
- What is the difference between an image and a container, and how do image layers work?
- What is a container, and how does it differ from a virtual machine?
- What are multi-stage builds, and why use them?
- CMD or ENTRYPOINT: what is the difference, and what are exec and shell forms?
- How does Docker build caching work, and how do you order a Dockerfile to use it well?
- What are Dockerfile best practices for production images?
- ARG or ENV: what is the difference, and how do you pass secrets into a build safely?
- How do you secure Docker containers and images?