prostý člověk
prostý člověk

Reputation: 939

Kafka Connection Problem with Docker Compose

I have below docker compose yml,

version: '3.5'
services:

  zookeeper:
    container_name: kafka-zookeeper
    image: wurstmeister/zookeeper:3.4.6
    ports:
      - "2181:2181"
    networks:
      - my-network

  kafka:
    container_name: kafka-server
    image: wurstmeister/kafka:2.12-2.5.0
    ports:
      - "9092:9092"
    links:
      - zookeeper
    environment:
      KAFKA_LISTENERS: PLAINTEXT://:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - my-network
networks:
  my-network:
    driver: bridge

I referred the below reference,

https://github.com/wurstmeister/kafka-docker/wiki/Connectivity

My spring boot application.yml file as below,

kafka:
  bootstrap:
    url: 127.0.0.1:9092

The error is,

[taKafkaProducer] org.apache.kafka.clients.NetworkClient   : [Producer clientId=SensorDataKafkaProducer] Connection to node -1 (/127.0.0.1:9092) could not be established. Broker may not be available.

I can connect Kafka via client tool and see listener details 127.0.0.1:9092

bd919e5a4df2        wurstmeister/kafka:2.12-2.5.0       "start-kafka.sh"         About a minute ago   Up About a minute   0.0.0.0:9092->9092/tcp                                      kafka-server

9b94b7dac237        wurstmeister/zookeeper:3.4.6        "/bin/sh -c '/usr/sb…"   About a minute ago   Up About a minute   22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp          kafka-zookeeper

any issue with config?

Upvotes: 0

Views: 1251

Answers (1)

Mikalai Lushchytski
Mikalai Lushchytski

Reputation: 1651

With a user-defined bridge network, containers can resolve each other by name or alias. Thus, in your case the application.yml should point at the kafka container name:

kafka:
  bootstrap:
    url: kafka-server:9092

Also, for it works to configure the kafka advertised listener by container name as well:

KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-server:9092

Upvotes: 0

Related Questions