Have Docker wait for Kafka to startup before running tests

Viewed 2429

I have the following docker-compose.yml file

version: '3'
services:
  zookeeper:
    ports:
      - "2181:2181"
    hostname: zookeeper
    image: confluentinc/cp-zookeeper
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  broker:
    image: confluentinc/cp-enterprise-kafka
    hostname: broker
    ports:
      - "9092:9092"
    depends_on:
      - zookeeper
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://broker:9092'
      KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: broker:9092
      CONFLUENT_METRICS_REPORTER_ZOOKEEPER_CONNECT: zookeeper:2181
      CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
      CONFLUENT_METRICS_ENABLE: 'true'
      CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'

  test:
    image: java:8
    volumes:
        - .:/src
        - ${GRADLE_USER_HOME}:/gradle_mount
    environment:
      - GRADLE_USER_HOME=/gradle_mount
      - SPRING_KAFKA_BOOTSTRAP-SERVERS=broker:9092
    depends_on:
        - broker
        - zookeeper
    working_dir: /src
    privileged: true
    command:  "./gradlew --no-daemon clean test --info"

The tests pass locally but fail in Jenkins and I suspect it is because the tests are firing off before the Kafka service is running.

Is there any way to make the test container wait for the service on the Kafka container (broker) to start?

0 Answers
Related