0

This is my docker-compose.yml file.

version: '2'

networks:
  kafkanet:
    driver: bridge

services:
  zookeeper:
    image: 'bitnami/zookeeper:latest'
    ports:
      - '2181:2181'
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
    networks:
      - kafkanet
  kafka:
    image: 'bitnami/kafka:latest'
    networks:
      - kafkanet
    ports:
      - '9093:9093'
    environment:
      - KAFKA_BROKER_ID=1
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT
      - KAFKA_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9093
      - KAFKA_CFG_ADVERTISED_LISTENERS=CLIENT://kafka:9092,EXTERNAL://localhost:9093
      - KAFKA_CFG_INTER_BROKER_LISTENER_NAME=CLIENT
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
    depends_on:
      - zookeeper

  spring_boot:
    build: .
    networks:
      - kafkanet
    depends_on:
      - kafka
      - zookeeper

Now, after I run docker-compose up -d, kafka works fine. If I set spring.kafka.bootstrap-servers = 0.0.0.0:9093 and run the spring app locally in my IDE, it works fine, too. But in docker, the spring_boot container can't connect with kafka.

I searched similar questions and some people said spring.kafka.bootstrap-servers = kafka:9092 should be used, but there will be errors when build the docker image with this setting.

Thanks for any help.

Dockerfile

FROM openjdk:11
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
  • 1
    What errors would you get building the image? I wouldn't expect the build itself to need to contact Kafka at all. – David Maze May 24 '22 at 10:24
  • I added my dockerfile above. I run `mvn clean install` first and then build with the dockerfile. If I set the value to "kafka:9092", `mvn clean install` will report errors. – John Smith May 24 '22 at 12:18
  • Specify in your Docker compose file the ENV variable where spring finds the Kafka server. Since you are in the docker network you can not use localhost and the server is called kafka. SPRING_KAFKA_BOOTSTRAP_SERVERS=kafka:9093 – mszalbach May 24 '22 at 12:24
  • @mszalbach port 9092 is correct. 9093 will return back localhost – OneCricketeer May 24 '22 at 14:34
  • 1
    @john Are you running tests that require Kafka connectivity? That's the only reason maven should fail, and that's not a docker problem, that's an issue with your tests. Try again with `mvn clean install -DskipTests` and `kafka:9092`, then work on fixing the tests – OneCricketeer May 24 '22 at 14:36
  • Thanks! I solved my problem by changing`KAFKA_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9093` to `KAFKA_CFG_LISTENERS=CLIENT://0.0.0.0:9092,EXTERNAL://0.0.0.0:9093`. – John Smith May 25 '22 at 00:12

0 Answers0