Back to Docker: Images, Containers, Multi-Stage
Q5Question 5 of 12

What is Docker Compose and how do you run multi-container applications

Answer: Docker Compose is a tool that defines and runs multi-container applications using a YAML file, letting you start an entire banking stack (application, database, cache, message broker) with a single `docker compose up` command.

Key Concepts
  • Docker Compose defines multi-container applications in a single YAML file and manages their lifecycle with one command.
  • Each service in a Compose file is a container with its own image, ports, volumes, environment variables, and network configuration.
  • Compose creates a default network so all services can reach each other by service name.
  • Volumes persist database data across container restarts, preventing data loss when PostgreSQL or Redis containers are recreated.
  • Health checks and depends_on with conditions ensure services start in the correct order.
  • Compose profiles let you define optional services like monitoring or debug tools that only run when activated.
  • Docker Compose is for local development and testing, not for production orchestration where Kubernetes is preferred.

Definition

Docker Compose is a tool that defines and runs multi-container applications using a YAML file, letting you start an entire banking stack (application, database, cache, message broker) with a single `docker compose up` command.
  • docker-compose.yml is a YAML file that defines services (containers), networks, and volumes for a multi-container application.
  • Service is a container definition with image, ports, environment variables, volumes, health checks, and dependencies.
  • Network is an isolated communication channel where services can reach each other by name.
  • Volume is persistent storage that survives container restarts and recreations.
  • depends_on specifies startup order between services, optionally waiting for a health check to pass.

Think of setting up a new National Bank of India branch. You need a teller counter (the Spring Boot app), a vault (PostgreSQL), a fast-access drawer (Redis cache), and an internal mail system (Kafka). Without Compose, you would set up each one individually, wire the cables between them, and hope they start in the right order. With Compose, you have a single blueprint that describes all four components and their connections. You hand the blueprint to the construction crew (Docker) and say "build it." One command, everything comes up connected and ready.

Understanding the Concepts

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. You describe your entire application stack in a docker-compose.yml file: which containers to run, how they connect, what data to persist, and in what order to start them.

Without Compose, you would run multiple docker run commands with long argument lists. With Compose, one file defines everything and one command (docker compose up) starts everything.

YAML
# docker-compose.yml for National Bank of India backend
version: '3.9'

services:
  transfer-service:
    build: .
    ports:
      - "8080:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=local
      - DB_URL=jdbc:postgresql://bank-db:5432/bankdb
      - REDIS_HOST=bank-redis
      - KAFKA_BROKERS=kafka:9092
    depends_on:
      bank-db:
        condition: service_healthy
      bank-redis:
        condition: service_started
      kafka:
        condition: service_started

  bank-db:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: bankdb
      POSTGRES_USER: bankadmin
      POSTGRES_PASSWORD: localdev123
    ports:
      - "5432:5432"
    volumes:
      - bank-pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U bankadmin -d bankdb"]
      interval: 5s
      timeout: 3s
      retries: 5

  bank-redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - bank-redis-data:/data

  kafka:
    image: confluentinc/cp-kafka:7.6.0
    environment:
      KAFKA_NODE_ID: 1
      KAFKA_PROCESS_ROLES: broker,controller
      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka:9093
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      CLUSTER_ID: banking-local-cluster-001
    ports:
      - "9092:9092"

volumes:
  bank-pgdata:
  bank-redis-data:
BASH
# Start the entire stack
docker compose up -d

# View running services
docker compose ps

# View logs from all services
docker compose logs -f

# Stop everything
docker compose down

# Stop and remove volumes (deletes all data)
docker compose down -v
TEXT
[+] Running 4/4
  Container bank-db           Healthy
  Container bank-redis        Started
  Container kafka             Started
  Container transfer-service  Started
JAVA
package com.bank.docker;

public class ComposeStackDemo {
    public static void main(String[] args) {
        System.out.println("National Bank of India local stack:");
        System.out.println("  transfer-service -> localhost:8080");
        System.out.println("  bank-db (PostgreSQL) -> localhost:5432");
        System.out.println("  bank-redis (Redis) -> localhost:6379");
        System.out.println("  kafka -> localhost:9092");
        System.out.println();
        System.out.println("Internal DNS (container-to-container):");
        System.out.println("  transfer-service connects to bank-db:5432");
        System.out.println("  transfer-service connects to bank-redis:6379");
        System.out.println("  transfer-service connects to kafka:9092");
    }
}
TEXT
National Bank of India local stack:
  transfer-service -> localhost:8080
  bank-db (PostgreSQL) -> localhost:5432
  bank-redis (Redis) -> localhost:6379
  kafka -> localhost:9092

Internal DNS (container-to-container):
  transfer-service connects to bank-db:5432
  transfer-service connects to bank-redis:6379
  transfer-service connects to kafka:9092

How do services communicate in Docker Compose?

Docker Compose creates a default network for all services defined in the file. Each service is reachable by its service name as the hostname. The transfer-service connects to PostgreSQL using bank-db:5432, not localhost:5432.

JAVA
package com.bank.config;

public class DataSourceConfig {

    // In docker-compose, the hostname is the service name
    private static final String DB_URL =
        "jdbc:postgresql://bank-db:5432/bankdb";
    private static final String REDIS_HOST = "bank-redis";
    private static final int REDIS_PORT = 6379;
    private static final String KAFKA_BROKERS = "kafka:9092";

    public static void main(String[] args) {
        System.out.println("Database URL: " + DB_URL);
        System.out.println("Redis: " + REDIS_HOST + ":" + REDIS_PORT);
        System.out.println("Kafka: " + KAFKA_BROKERS);
        System.out.println();
        System.out.println("Docker Compose creates a bridge network.");
        System.out.println("Each service name resolves to that container's IP.");
        System.out.println("No hardcoded IPs needed.");
    }
}
TEXT
Database URL: jdbc:postgresql://bank-db:5432/bankdb
Redis: bank-redis:6379
Kafka: kafka:9092

Docker Compose creates a bridge network.
Each service name resolves to that container's IP.
No hardcoded IPs needed.

How do you persist data with volumes?

Without volumes, container data is lost when the container is removed. Volumes store data on the host filesystem, surviving container restarts and recreations.

In banking, PostgreSQL data must survive container restarts. If you run docker compose down and then docker compose up, the database should still have all accounts and transactions.

YAML
# Volume definition in docker-compose.yml
volumes:
  bank-pgdata:      # Named volume for PostgreSQL
  bank-redis-data:  # Named volume for Redis persistence

services:
  bank-db:
    image: postgres:16-alpine
    volumes:
      - bank-pgdata:/var/lib/postgresql/data
      # Also mount init scripts
      - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
JAVA
package com.bank.docker;

public class VolumeDemo {
    public static void main(String[] args) {
        System.out.println("Docker volume lifecycle:");
        System.out.println();
        System.out.println("1. docker compose up    -> volume created");
        System.out.println("2. App writes data      -> stored in volume");
        System.out.println("3. docker compose down  -> container removed, volume kept");
        System.out.println("4. docker compose up    -> new container, same volume, data intact");
        System.out.println();
        System.out.println("5. docker compose down -v -> volume DELETED, data LOST");
        System.out.println();
        System.out.println("Volume types:");
        System.out.println("  Named volume: bank-pgdata (managed by Docker)");
        System.out.println("  Bind mount: ./db/init.sql:/docker-entrypoint-initdb.d/init.sql");
    }
}
TEXT
Docker volume lifecycle:

1. docker compose up    -> volume created
2. App writes data      -> stored in volume
3. docker compose down  -> container removed, volume kept
4. docker compose up    -> new container, same volume, data intact

5. docker compose down -v -> volume DELETED, data LOST

Volume types:
  Named volume: bank-pgdata (managed by Docker)
  Bind mount: ./db/init.sql:/docker-entrypoint-initdb.d/init.sql

How do health checks and depends_on work?

depends_on controls startup order. With condition: service_healthy, Docker Compose waits for the dependency's health check to pass before starting the dependent service.

Without health checks, the application container might start before PostgreSQL is ready to accept connections, causing connection refused errors on startup.

YAML
services:
  bank-db:
    image: postgres:16-alpine
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U bankadmin -d bankdb"]
      interval: 5s
      timeout: 3s
      retries: 5
      start_period: 10s

  transfer-service:
    depends_on:
      bank-db:
        condition: service_healthy  # Wait for DB to be ready
      bank-redis:
        condition: service_started  # Just wait for it to start
JAVA
package com.bank.docker;

public class HealthCheckDemo {
    public static void main(String[] args) {
        System.out.println("Startup sequence with health checks:");
        System.out.println();
        System.out.println("1. bank-db starts (PostgreSQL)");
        System.out.println("   Health check: pg_isready every 5s");
        System.out.println("   Status: starting... starting... healthy");
        System.out.println();
        System.out.println("2. bank-redis starts (no health check needed)");
        System.out.println("   Status: started");
        System.out.println();
        System.out.println("3. kafka starts (no health check needed)");
        System.out.println("   Status: started");
        System.out.println();
        System.out.println("4. transfer-service starts AFTER bank-db is healthy");
        System.out.println("   Connects to bank-db:5432 successfully");
    }
}
TEXT
Startup sequence with health checks:

1. bank-db starts (PostgreSQL)
   Health check: pg_isready every 5s
   Status: starting... starting... healthy

2. bank-redis starts (no health check needed)
   Status: started

3. kafka starts (no health check needed)
   Status: started

4. transfer-service starts AFTER bank-db is healthy
   Connects to bank-db:5432 successfully

What are common mistakes with Docker Compose?

Mistake 1: Not using health checks with depends_on

Wrong: depends_on without a condition only waits for the container to start, not for the service inside to be ready.

YAML
# WRONG: transfer-service starts as soon as bank-db container starts
# but PostgreSQL inside is still initializing
services:
  transfer-service:
    depends_on:
      - bank-db  # Only waits for container start, not readiness

Correct: use health checks with condition: service_healthy.

YAML
# CORRECT: wait for PostgreSQL to accept connections
services:
  bank-db:
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U bankadmin -d bankdb"]
      interval: 5s
      retries: 5
  transfer-service:
    depends_on:
      bank-db:
        condition: service_healthy

Mistake 2: Using docker compose down -v in development without thinking

Wrong: docker compose down -v deletes all volumes, including your database data.

BASH
# WRONG: this deletes all your test data
docker compose down -v

Correct: use docker compose down without -v to keep volumes.

BASH
# CORRECT: stop containers, keep data
docker compose down

# Only use -v when you intentionally want a clean slate
docker compose down -v  # Deletes all volumes and data

How is Docker Compose different from Kubernetes?

AspectDocker ComposeKubernetes
PurposeLocal development, testingProduction orchestration
ScopeSingle hostCluster of nodes
ScalingManual (docker compose up --scale)Auto-scaling (HPA)
Self-healingNo (must restart manually)Yes (restarts failed pods)
NetworkingSimple bridge networkService discovery, ingress, load balancing
Rolling updatesNoYes (zero-downtime deployments)
SecretsEnvironment variables or filesEncrypted secrets with RBAC
Config formatdocker-compose.ymlYAML manifests (Deployment, Service, etc.)
JAVA
package com.bank.docker;

public class ComposeVsK8sDemo {
    public static void main(String[] args) {
        System.out.println("Docker Compose: development and testing");
        System.out.println("  docker compose up -d");
        System.out.println("  Runs on your laptop");
        System.out.println("  Quick to start, simple YAML");
        System.out.println();
        System.out.println("Kubernetes: production deployment");
        System.out.println("  kubectl apply -f deployment.yaml");
        System.out.println("  Runs on a cluster of nodes");
        System.out.println("  Auto-scaling, self-healing, rolling updates");
        System.out.println();
        System.out.println("At National Bank of India:");
        System.out.println("  Developers: docker compose for local stack");
        System.out.println("  Production: Kubernetes on AWS EKS");
    }
}
TEXT
Docker Compose: development and testing
  docker compose up -d
  Runs on your laptop
  Quick to start, simple YAML

Kubernetes: production deployment
  kubectl apply -f deployment.yaml
  Runs on a cluster of nodes
  Auto-scaling, self-healing, rolling updates

At National Bank of India:
  Developers: docker compose for local stack
  Production: Kubernetes on AWS EKS

What about environment-specific configuration?

Compose supports .env files for environment-specific values and multiple Compose files for overrides.

BASH
# .env file for local development
POSTGRES_PASSWORD=localdev123
SPRING_PROFILES_ACTIVE=local
KAFKA_PARTITIONS=3
YAML
# docker-compose.override.yml (auto-loaded with docker-compose.yml)
services:
  transfer-service:
    environment:
      - DEBUG=true
      - SPRING_PROFILES_ACTIVE=local
    volumes:
      # Mount source code for hot-reload during development
      - ./src:/app/src
JAVA
package com.bank.docker;

public class EnvConfigDemo {
    public static void main(String[] args) {
        System.out.println("Environment configuration in Compose:");
        System.out.println();
        System.out.println("1. .env file: default variables");
        System.out.println("   POSTGRES_PASSWORD=localdev123");
        System.out.println();
        System.out.println("2. docker-compose.yml: base config");
        System.out.println("   All services, networks, volumes");
        System.out.println();
        System.out.println("3. docker-compose.override.yml: dev overrides");
        System.out.println("   Debug mode, source mounts, extra logging");
        System.out.println();
        System.out.println("4. docker compose -f base.yml -f test.yml up");
        System.out.println("   Merge multiple files for test environment");
    }
}
TEXT
Environment configuration in Compose:

1. .env file: default variables
   POSTGRES_PASSWORD=localdev123

2. docker-compose.yml: base config
   All services, networks, volumes

3. docker-compose.override.yml: dev overrides
   Debug mode, source mounts, extra logging

4. docker compose -f base.yml -f test.yml up
   Merge multiple files for test environment

What should a senior engineer know?

- Compose profiles. Define optional services that only start when a profile is activated. Monitoring tools (Prometheus, Grafana) can be in a "monitoring" profile, and debug tools (pgAdmin, Kafka UI) in a "debug" profile. Activate with docker compose --profile monitoring up.

- Compose Watch (v2.22+). Compose Watch automatically rebuilds and restarts containers when source files change. This replaces manual docker compose build && up cycles during development.

- Resource limits. Set deploy.resources.limits for CPU and memory to prevent one container from starving others. This mirrors what Kubernetes does with resource requests and limits.

- Init containers pattern. Use a service with depends_on to run database migrations before the application starts. The migration service runs, applies schema changes, exits, and then the application starts with the correct schema.

- Testcontainers. For integration tests, use the Testcontainers library instead of a shared Compose file. Testcontainers spins up containers on demand in your test code and tears them down after the test. This gives each test run a clean environment.

What are common interview follow-up questions?

  • How do you handle secrets in Docker Compose for production-like environments?
  • What is the difference between docker compose up and docker compose run?
  • How do you run database migrations as part of the Compose startup?
  • How do you scale a specific service in Docker Compose?
  • When should you move from Docker Compose to Kubernetes?

Visual Overview

DIAGRAM
  Docker Compose: Banking Local Stack
  =====================================

  docker-compose.yml
  ┌────────────────────────────────────────────────┐
  │                                                │
  │  ┌────────────────┐    ┌────────────────┐      │
  │  │ transfer-      │    │ bank-db        │      │
  │  │ service        │───>│ (PostgreSQL)   │      │
  │  │ :8080          │    │ :5432          │      │
  │  │                │    │ volume: pgdata │      │
  │  │                │    └────────────────┘      │
  │  │                │                            │
  │  │                │    ┌────────────────┐      │
  │  │                │───>│ bank-redis     │      │
  │  │                │    │ :6379          │      │
  │  │                │    │ volume: redis  │      │
  │  │                │    └────────────────┘      │
  │  │                │                            │
  │  │                │    ┌────────────────┐      │
  │  │                │───>│ kafka          │      │
  │  │                │    │ :9092          │      │
  │  └────────────────┘    └────────────────┘      │
  │                                                │
  │  Network: bank-network (auto-created)          │
  │  All services reachable by service name        │
  └────────────────────────────────────────────────┘

  Startup order:
  1. bank-db (wait for healthcheck: pg_isready)
  2. bank-redis, kafka (start immediately)
  3. transfer-service (after bank-db is healthy)

Key Takeaway

Docker Compose turns a complex multi-container setup into a single YAML file and a single command. For the National Bank of India backend, that means starting PostgreSQL, Redis, Kafka, and the Spring Boot application with docker compose up -d. Every developer on the team gets the same environment, configured the same way, with the same data.

The most important feature is the default network. Docker Compose creates a bridge network where every service is reachable by its service name. The Spring Boot application connects to bank-db:5432, not a hardcoded IP. When you add a new service, it automatically joins the network.

Volumes protect your data. Without a volume, stopping and removing the PostgreSQL container deletes all accounts and transactions. With a named volume, the data survives container restarts. The only command that deletes volumes is docker compose down -v, and you should use it only when you intentionally want a clean slate.

Health checks and depends_on with conditions solve the startup ordering problem. PostgreSQL takes a few seconds to initialize. Without a health check, the application starts before the database is ready and fails with a connection refused error. With condition: service_healthy, Docker Compose waits for pg_isready to succeed before starting the application.

One sentence to remember: Docker Compose defines your entire development stack in one YAML file and starts it with one command, with services connected by name, data persisted in volumes, and startup ordered by health checks.

---

Share
Use ← → arrow keys to navigate