🐳 Docker Beginner

What is docker-compose.yml structure?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Docker topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

The docker-compose.yml (or compose.yml) is a YAML file defining multi-container applications. Main top-level keys: version: Compose file format version (still valid but deprecated in Compose V2 which auto-detects). services: defines each container/service. Each service can have: image (use existing image), build (build from Dockerfile — can be a path or object with context/dockerfile), ports (host:container port mapping), environment (env vars as list or map), env_file (.env file), volumes (volume mounts), depends_on (start order), networks (network membership), restart (restart policy), command (override CMD), entrypoint, healthcheck, deploy (resource limits in Swarm). volumes: declares named volumes used by services. networks: defines custom networks (Compose auto-creates one per project). configs and secrets: Docker Swarm features for configuration and secrets management. Example health check: healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000/health"] interval: 30s timeout: 10s retries: 3 start_period: 40s. Compose files support YAML anchors for reusing configuration blocks.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Docker project, I used this when...' immediately makes your answer more credible and memorable.