Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker (Compose) client connects to Kafka too early

I am trying to run Kafka with Docker and Docker Compose. This is the docker-compose.yml:

version: "2"

services:
  zookeeper:
    image: "wurstmeister/zookeeper"
    ports:
      - "2181:2181"

  kafka:
    build:
      context: "./services/kafka"
      dockerfile: "Dockerfile"
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: "0.0.0.0"
      KAFKA_CREATE_TOPICS: "test:1:1"
      KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"

  users:
    build:
      context: "./services/users"
      dockerfile: "Dockerfile"
    ports:
      - "4001:4001"
    environment:
      NODE_ENV: "develop"
      ZOOKEEPER_HOST: "zookeeper"
      ZOOKEEPER_PORT: "2181"
    volumes:
      - "./services/users:/service"

The users service only tries to connect (using kafka-node in Node.js) and listens on a topic and publishes one message to it every time it is ran.

The problem is that I keep getting Connection Refused errors. I am using Dockerize to wait for the kafka port to be available in the Dockerfile with the line CMD dockerize -wait tcp://kafka:9092 node /service/index.js.

It waits for the port to be available before starting the users container and this system works, but it is not at the right time. It seems that Kafka is opening the 9092 port before it has elected a leader.

When I run Kafka first and let it start completely and then run my app, it runs smoothly.

How do I wait for the correct moment before starting my service?

like image 280
Industrial Avatar asked Feb 21 '17 03:02

Industrial


People also ask

Is Docker necessary for Kafka?

You will need two Docker images to get Kafka running: wurstmeister/zookeeper.


1 Answers

Try the docker-compose version 2.1 or 3, as it includes an healthcheck directive.
See "Docker Compose wait for container X before starting Y" as an example.

You can:

depends_on:
  kafka:
    condition: service_healthy

And in kafka add:

healthcheck:
    test: ["CMD", ...]
    interval: 30s
    timeout: 10s
    retries: 5

with a curl command for instance which would test if kafka has elected a leader.

like image 105
VonC Avatar answered Sep 19 '22 03:09

VonC