BBackend CourseLeroy Labs
0/0
Search the course

docker-compose for local dev with healthchecks, watch, profiles & tooling

One command to run the whole stack. depends_on with healthchecks, compose watch for hot rebuild, debug ports, optional tools via profiles, init scripts, and spring-boot-docker-compose.

● beginnerHostel-Web-AppReport-Manager-2SperoTek (in dev)

The concept

services:
  db:
    image: postgis/postgis:18-3.6-alpine
    environment: { POSTGRES_DB: hostel, POSTGRES_USER: hostel, POSTGRES_PASSWORD: ${DB_PASSWORD} }
    ports: ["5433:5432"]                         # host 5433 avoids clashing with a local Postgres
    volumes:
      - pgdata:/var/lib/postgresql/data          # data survives "docker compose down"
      - ./db-init:/docker-entrypoint-initdb.d    # runs once on first init (CREATE EXTENSION postgis)
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U hostel"]
      interval: 5s
      retries: 10

  app:
    build: { context: ., dockerfile: Dockerfile.dev }
    env_file: .env
    ports: ["8080:8080", "5005:5005"]            # 5005 = remote debugger
    depends_on:
      db: { condition: service_healthy }         # wait until Postgres is READY, not just started
    develop:
      watch:
        - { action: rebuild, path: pom.xml }
        - { action: sync+restart, path: ./src, target: /app/src }

  pgadmin:
    image: dpage/pgadmin4
    profiles: [tools]                            # only with: docker compose --profile tools up
    ports: ["5050:80"]

volumes: { pgdata: {} }

docker compose up --watch gives you the whole stack with hot reload.

In your projects

Hostel-Web-Appdocker-compose.yaml · db-init/00-init-extensions.sql

PostGIS with a healthcheck, the app from Dockerfile.dev with develop.watch, debug port 5005, an actuator healthcheck, and pgAdmin under profiles: [tools]. The spring-boot-docker-compose dependency can also start compose services automatically when you run the app from your IDE.

Report-Manager-2docker-compose.yaml · grafana/dashboards/rabbitmq-overview.json

A full observability stack locally: Postgres, RabbitMQ (management + Prometheus plugin), Prometheus, Grafana with a provisioned RabbitMQ dashboard, and the app with compose watch. You can see queue depths and JVM metrics on your laptop before production.

SperoTek (in dev)docker-compose.yaml

postgres:18 (port 5433), Mailpit (SMTP catcher, UI on :8025), the app (8081 → 8080, debug 5006), the web frontend (Vite on 5173) and cloudflared. The whole product runs with one command, and the tunnel can expose it for demos or mobile testing.

Alternatives

Option Notes
docker-compose (yours) Standard for local multi-service development
Testcontainers at dev time Spring Boot TestApplication starts containers from code
Dev containers / GitHub Codespaces The whole dev environment in a container
Tilt / Skaffold Local Kubernetes development loops
Installing Postgres locally Version drift; no PostGIS by default
  • depends_on without a healthcheck only waits for the container to start, not for the DB to be ready.
  • Named volumes vs bind mounts. Use volumes for DB data and bind mounts for source code.
  • Networks. Services reach each other by service name (jdbc:postgresql://db:5432/...), not localhost.
  • Compose in production is fine for a single host (IR3D’s docker-compose.prod.yml on EC2), but it doesn’t do multi-host scheduling.

Check yourself

1. From the app container, the DB host is…
2. condition: service_healthy ensures…