0

Here I have a network of Docker containers:

Docker-compose.yml:

version: "2"
services:
 zookeeper:
    image: zookeeper
    container_name: zookeeper
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
 broker:
    image: confluentinc/cp-kafka:latest
    container_name: broker
    ports:
      - '9092:9092'
    depends_on:
      - zookeeper
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 100
      KAFKA_HEAP_OPTS: "-Xmx512M -Xms256M"
 kafkacat:
    build: kafkacat
    container_name: kafkacat
    depends_on:
      - broker
    entrypoint:
      - /bin/bash
      - -c
      - /scripts/get_data.sh

And the following directory structure

├── README.md
├── docker-compose.yml
├── kafka
│   ├── kafkacat
├── kafkacat
│   ├── Dockerfile
│   ├── get_data.sh
│   ├── print_data.sh
│   └── wait_for_it.sh

And kafkacat/Dockerfile:

FROM edenhill/kafkacat:1.6.0
COPY *.sh scripts/
WORKDIR scripts
RUN chmod +x .
RUN apk add --no-cache bash
RUN apk add jq;
RUN apk add curl;

When spinning with sudo docker-compose up kafkacat, the kafkacat container returns a Connection refused error:

kafkacat           | %3|1667332626.747|FAIL|rdkafka#producer-1| [thrd:broker:29092/bootstrap]: broker:29092/bootstrap: Connect to ipv4#172.18.0.3:29092 failed: Connection refused (after 1ms in state CONNECT)
kafkacat           | % ERROR: Local: Broker transport failure: broker:29092/bootstrap: Connect to ipv4#172.18.0.3:29092 failed: Connection refused (after 1ms in state CONNECT)
kafkacat           | % ERROR: Local: All broker connections are down: 1/1 brokers are down : terminating

This error does not occur with docker-compose up kafkacat as non-superuser.

When I deleted the empty kafka directory and its contents, i.e.:

├── README.md
├── docker-compose.yml
├── kafkacat
│   ├── Dockerfile
│   ├── get_data.sh
│   ├── print_data.sh
│   └── wait_for_it.sh

The error ceased to occur with sudo docker-compose up kafkacat. I think it's something to do with the mechanics of the Docker build, but I really can't figure it out. Does anyone have a good explanation for why this could occur?

Jack Rowntree
  • 193
  • 1
  • 5

1 Answers1

1

depends_on doesn't wait for the broker container to start.

Kafka takes some time to start, therefore, you should use that wait_for_it script before you try to do any actions against the broker

e.g.

    depends_on:
      - broker
    entrypoint: ['bash', '-c']
    command:
      - /scripts/wait_for_it.sh
      - broker:29092
      - --
      - /scripts/get_data.sh

Also, ideally, you'd pass a shell argument / environment variable for the broker to your script here, but this should be enough for now

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • aside: `chmod` the scripts **before** you COPY. Don't add an unnecessary Docker layer (and you can `apk add` multiple packages at once) – OneCricketeer Nov 01 '22 at 20:51