0

I spun up kafka cluster with this file

version: "3"

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:7.2.0
    hostname: zookeeper
    container_name: zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  broker:
    image: confluentinc/cp-kafka:7.2.0
    hostname: broker
    container_name: broker
    depends_on:
      - zookeeper
    ports:
      - "29092:29092"
      - "9092:9092"
      - "9101:9101"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
      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: 0
      KAFKA_JMX_PORT: 9101
      KAFKA_JMX_HOSTNAME: localhost

  schema-registry:
    image: confluentinc/cp-schema-registry:7.2.0
    hostname: schema-registry
    container_name: schema-registry
    depends_on:
      - broker
    ports:
      - "8081:8081"
    environment:
      SCHEMA_REGISTRY_HOST_NAME: schema-registry
      SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'broker:29092'
      SCHEMA_REGISTRY_LISTENERS: http://0.0.0.0:8081

and I am running spring boot application which runs fine on localhost when l give bootstrap server as localhost:9092 but when I try to dockerize it, it fails to connect to the broker.

spring boot application.yaml is

spring:
  main:
    allow-bean-definition-overriding: true
  kafka:
    bootstrap-servers: broker:29092
  cloud:
    stream:
      binder:
        consumer-properties:
          key.deserializer: io.confluent.kafka.serializers.KafkaAvroDeserializer
          value.deserializer: io.confluent.kafka.serializers.KafkaAvroDeserializer
          schema.registry.url: http://host.docker.internal:8081
          properties:
            specific.avro.reader: true
      schemaRegistryClient:
        endpoint: http://host.docker.internal:8081
      bindings:
        event-in-0:
          destination: event-details
          contentType: application/*+avro
          group: group1
      function:
        definition: event

I build the docker image and run as

docker run -it --name event-consumer -p 9091:9091 event-consumer:1.0

but it is not able to connect to the broker.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user1298426
  • 3,467
  • 15
  • 50
  • 96
  • 1
    that's because they are running on a different docker networks. You can see it with `docker network ls`. Can't you add the app in the docker-compose.yml as well? – ItayB Jul 10 '22 at 18:41
  • Add your Spring app to the same Compose file or add `--network` option to docker run command – OneCricketeer Jul 10 '22 at 20:23

0 Answers0