I'm using binami/kafka docker image, and running a Golang app that suppose to connect to it. But the accessible ports change when I run the Go app on my host vs running it inside a container in docker network.
This is the docker-compose kafka code:
kafka:
image: docker.io/bitnami/kafka:3.3
ports:
- "29092:29092"
- "9092:9092"
volumes:
- "kafka_data:/bitnami"
environment:
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_LISTENERS=EXTERNAL_SAME_HOST://:29092,INTERNAL://:9092
- KAFKA_ADVERTISED_LISTENERS=INTERNAL://kafka:9092,EXTERNAL_SAME_HOST://localhost:29092
- KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:PLAINTEXT,EXTERNAL_SAME_HOST:PLAINTEXT
- KAFKA_INTER_BROKER_LISTENER_NAME=INTERNAL
- KAFKA_BROKER_ID=1
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
depends_on:
- zookeeper
This is the Go code (no need to know Go)
kafkaURL := "localhost:29092" //works when running in my mac
kafkaURL := "kafka:9092" //works when running inside the container
topic := "payments"
writer := newKafkaWriter(kafkaURL, topic)
...
Now why this works with different ports?