Search code examples
javadockerapache-kafkadocker-composespring-kafka

Running Spring Boot application with Kafka in Docker-compose


I have application (kafka client). I have next properties:

spring.kafka.bootstrap-servers=127.0.0.1:29092
spring.kafka.consumer.group-id=mc3

And i have kafka in Docker-compose:

kafka:
  image: confluentinc/cp-kafka:latest
  depends_on:
    - zookeeper
  ports:
    - 29092:29092
  environment:
    KAFKA_BROKER_ID: 1
    KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
    KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
    KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
    KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

I need to move application in to compose like:

mc3-service:
  image: ksonv/mc3-service:0.0.1-SNAPSHOT
  ports:
    - "8103:8103"
  depends_on:
    - kafka
  environment:
    SPRING_KAFKA_BOOTSTRAP_SERVER: kafka:9092
    SPRING_KAFKA_CONSUMER_GROUP_ID: mc3

But i do not know how write spring.kafka.bootstrap-servers property in environment section on compose.

I am trying like SPRING_KAFKA_BOOTSTRAP_SERVER. But it does not work.

How determine spring.kafka.bootstrap-servers property in environment section on compose?


Solution

  • from one of the comments within this answer, it seems that in spring boot you can use SPRING_KAFKA_BOOTSTRAP_SERVERS (note that you missed the S at the end):

    environment:
    - SPRING_KAFKA_BOOTSTRAP_SERVERS=kafka:9092
    ...
    

    environment variables in docker-compose are usually start with - (YAML list) and value assigned with =.

    You can validate that it propagates by exec into the running container and run printenv to see that your environment variables exist:

    docker exec -it <container_name or container_id> bash