Skip to content

Docker Setup

Configure Docker Compose for Kafka, Zookeeper, and MySQL with health checks for proper startup order.

Infrastructure Services

  • MySQL 8.0 - Port 3306, database: workshop, user: workshop, password: workshop
  • Apache Kafka 7.5.0 - Port 9092
  • Zookeeper 7.5.0 - Port 2181

docker-compose.yml

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:7.5.0
    container_name: workshop-zookeeper
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - "2181:2181"
    networks:
      - kafka-workshop
    healthcheck:
      test: ["CMD", "nc", "-z", "localhost", "2181"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  kafka:
    image: confluentinc/cp-kafka:7.5.0
    container_name: workshop-kafka
    depends_on:
      zookeeper:
        condition: service_healthy
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
    networks:
      - kafka-workshop
    healthcheck:
      test: ["CMD", "kafka-broker-api-versions", "--bootstrap-server", "localhost:9092"]
      interval: 10s
      timeout: 10s
      retries: 10
      start_period: 30s

  mysql:
    image: mysql:8.0
    platform: linux/arm64
    container_name: workshop-mysql
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: workshop
      MYSQL_USER: workshop
      MYSQL_PASSWORD: workshop
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
      - ./mysql:/docker-entrypoint-initdb.d
    networks:
      - kafka-workshop
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-prootpassword"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

networks:
  kafka-workshop:
    driver: bridge

volumes:
  mysql-data:

Platform Notes

Apple Silicon (M1/M2/M3)

MySQL is configured with platform: linux/arm64 for native ARM support.

Intel/AMD (x86_64)

Change MySQL platform to linux/amd64 if needed.

Kafka and Zookeeper images are multi-platform and work on both architectures.

Commands

Start All Services

docker-compose up -d

Check Service Health

docker-compose ps

View Logs

# All services
docker-compose logs -f

# Specific service
docker-compose logs -f kafka
docker-compose logs -f mysql

Stop Services

# Stop containers
docker-compose down

# Stop and remove volumes
docker-compose down -v

Startup Order

  1. Zookeeper starts first (Kafka coordination)
  2. Kafka waits for Zookeeper health check
  3. MySQL starts independently
  4. Backend application (after infrastructure is up)
  5. Frontend application (after backend is up)

Health Checks

  • Zookeeper: nc -z localhost 2181 (10s interval, 5 retries, 10s start period)
  • Kafka: kafka-broker-api-versions --bootstrap-server localhost:9092 (10s interval, 10 retries, 30s start period)
  • MySQL: mysqladmin ping -h localhost -u root -prootpassword (10s interval, 5 retries, 30s start period)

Health checks ensure proper startup order and prevent connection failures. The start_period gives services time to initialize before health checks begin.

Network Configuration

All services are connected via a custom bridge network called kafka-workshop. This allows containers to communicate with each other using their service names:

  • Kafka connects to Zookeeper using zookeeper:2181
  • Services are isolated from other Docker networks
  • External access via exposed ports (9092, 3306, 2181)

Volume Mounts

  • mysql-data: Named volume for persistent database storage
  • ./mysql: Directory mount for MySQL initialization scripts (optional)

Data persists across container restarts. Use docker-compose down -v to remove volumes.

Key Takeaways

  • Docker Compose manages all infrastructure services with version 7.5.0 for Confluent images
  • Health checks with start periods ensure proper startup sequence
  • Custom bridge network isolates workshop services
  • Multi-architecture support (ARM64/AMD64) with platform-specific configuration
  • Persistent volumes for MySQL data across container restarts
  • Easy local development environment setup with single command