Opscraft
Docker Compose · Lesson 10 of 10 · 3 min read

Interview questions and model answers

"How do services find each other in Compose?"

Compose creates a project network and Docker's embedded DNS resolves each service name to its containers' IPs. Services talk on container ports using names like db:5432. localhost inside a container refers to that container, so it's the wrong address for another service. Published ports are only for access from the host.

"depends_on is set but my app still fails because the database isn't ready. Why, and how do you fix it?"

Plain depends_on only controls start order, not readiness. I'd add a real healthcheck to the database (pg_isready) and use condition: service_healthy, plus service_completed_successfully for a migration job. And I'd make the app retry connections with backoff, because readiness can change after startup and in Kubernetes there's no depends_on at all.

"How do you handle dev, CI and prod differences with one Compose setup?"

A base compose.yaml with shared definitions, compose.override.yaml for dev conveniences that loads automatically, and explicit -f overlays for CI and prod. Interpolated variables with defaults (${TAG:-dev}) and required checks (${TAG:?}) for things like image tags. Profiles for optional tooling. And docker compose config to verify the merged result.

"What's the difference between .env, env_file and environment?"

.env feeds variable interpolation in the compose file itself and isn't passed to containers automatically. env_file loads variables from a file into a container. environment sets variables inline and wins over env_file. Shell variables override .env values for interpolation.

"Would you use Compose in production?"

For a small single-host service, yes, with pinned images, restart policies, health checks, limits, log rotation, backups and CI-driven deploys. But it has no multi-node HA, rolling updates or autoscaling, so for anything business-critical I'd use Kubernetes or ECS — and I'd translate the compose file into Deployments, Services, ConfigMaps, Secrets and PVCs, replacing depends_on with probes and retries.

"--scale worker=3 fails. Why?"

Usually because the service publishes a fixed host port, and only one container can bind it, or because it has a fixed container_name. I'd remove the host port mapping for scaled internal services or put a reverse proxy in front that load-balances across the replicas via the service name.

"How do you use Compose in a CI pipeline?"

Spin up real dependencies with a unique project name per build, up -d --wait to wait for health, run tests with docker compose run --rm tests, collect logs as artifacts, and always run down -v --remove-orphans in a cleanup step so agents don't accumulate containers and volumes.