Running a Full-Stack Backend Environment with Containers
Running a full-stack backend locally often requires multiple services: a PostgreSQL database, a Kafka message broker, a Redis cache, and the application itself. Docker Compose simplifies this setup by defining all services in a single YAML file.
Prerequisites & Compose Setup
Docker and Docker Compose must be installed. The docker-compose.yml file defines four services: postgres, kafka, redis, and app. Each service uses the latest official image and configures ports, environment variables, and volume mounts for persistence.
Service Configuration
The PostgreSQL service sets POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD via environment variables. Kafka is configured with KAFKA_ADVERTISED_LISTENERS and KAFKA_ZOOKEEPER_CONNECT for the KRaft mode or traditional ZooKeeper setup. Redis exposes its default port 6379.
Networking
All services share a Docker network named backend-net, enabling communication by service name. The application service depends on postgres, kafka, and redis using depends_on, ensuring they start before the app.
Environment Variables
The application reads configuration from application.yml using Spring's ${} placeholders. Example: spring.datasource.url=jdbc:postgresql://postgres:5432/mydb. Docker Compose exports all environment variables defined in the file, making it easy to swap configurations between environments.
Health Checks & Verification
Each service defines a health check. PostgreSQL uses pg_isready, Kafka checks broker connectivity, and Redis uses redis-cli ping. The application endpoint /actuator/health aggregates these checks. Terminal commands for verification:
docker compose updocker compose psdocker compose exec postgres pg_isreadydocker compose exec redis redis-cli pingCommon Errors
port already in use- resolve by removing conflicting containers or adjusting port mappings.Kafka broker not available- verify Zookeeper connectivity and network settings.Redis connection refused- ensure the Redis service is running and not paused.