Docker Compose for Local Spring Boot + PostgreSQL + Redis Dev
Fast recall
Quick revision
Core answer
Practical Docker Compose knowledge for local development workflows
Using depends_on without health checks — depends_on only waits for container start, not for the service to be ready to accept connections
Show health checks on postgres and redis, explain service DNS, discuss profiles for running only infrastructure vs full stack, and mention volume persistence
Interview-ready guide
Interview answer
Docker Compose lets you define a multi-container application as a single YAML file and manage the entire stack with one command. For local development, it replaces running three separate Docker commands and manually creating networks.
Service definition and networking
- Each service in
docker-compose.ymlmaps to one container; Compose creates a project-specific bridge network automatically — services resolve each other by name, so your Spring Boot app connects tojdbc:postgresql://postgres:5432/mydbby the service namepostgres, not by IP - The
depends_ondirective controls startup order but not readiness —depends_on: [postgres]ensures the Postgres container starts before Spring Boot, but Postgres is not necessarily accepting connections yet; add ahealthcheckto the postgres service and usecondition: service_healthyto wait for actual readiness - Without health check conditions, Spring Boot starts before Postgres is ready and fails with connection refused — the most common first-time Docker Compose debugging problem
Environment variables and secrets
- Define credentials in a
.envfile and reference them as${POSTGRES_PASSWORD}in the YAML — Compose reads.envautomatically and substitutes values; never hardcode passwords directly in the YAML - Add
.envto.gitignoreimmediately — it is the most common way credentials leak into public repositories; commit a.env.examplewith placeholder values instead - Production does not use Docker Compose; secrets in production go through the orchestrator's secret management (Kubernetes Secrets, AWS Secrets Manager) — Docker Compose is for local development and integration testing
Volume persistence
- Named volumes (
volumes: postgres_data: {}) survivedocker compose down; bind mounts do not — use named volumes for database data directories so a development database is not lost on every stack restart docker compose down -vremoves named volumes explicitly — useful when you need a clean database state, but easy to confuse with baredocker compose downwhich keeps the data
Docker Compose for Local Spring Boot + PostgreSQL + Redis Dev
Direct Answer
Use docker-compose with named services (postgres, redis, app), health checks on dependencies, and a shared bridge network. Spring Boot connects using service names (postgres:5432, redis:6379). Use depends_on with condition: service_healthy to ensure the database is ready before the app starts.
Common Mistakes & Pitfalls
Using depends_on without health checks — depends_on only waits for container start, not for the service to be ready to accept connections
Check your understanding
Answer these without looking back at the article.
- How do you initialize the PostgreSQL database schema automatically in Docker Compose?
- What is the difference between docker-compose down and docker-compose down -v?
- How would you add Kafka to this docker-compose setup?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.