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 up
        docker compose ps
        docker compose exec postgres pg_isready
        docker compose exec redis redis-cli ping

        

Common Errors

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles