Unable to connect kafka to zookeeper with different port using docker compose

Viewed 893

I am trying to connect my kafka setup with zookeeper contain exposed to a different port other than the default port, however when I change the port to 2181 the container is running fine but if I change the port in my yml file I am not able to run it, any guidance would be helpful.

version: '3'

services:
 zookeeper:
  image: wurstmeister/zookeeper
  container_name: zookeeper
  ports:
   - "2182:2181"
 
 kafka:
  image: wurstmeister/kafka
  container_name: kafka
  ports:
   - "9092:9092"
  environment:
   KAFKA_ADVERTISED_HOST_NAME: localhost
   KAFKA_ZOOKEEPER_CONNECT: zookeeper:2182
 
2 Answers

as @DavidMaze suggested. You try to connect 2182 which is exposed to outside. Just try the following.

version: '3'

services:
 zookeeper:
  image: wurstmeister/zookeeper
  container_name: zookeeper
  ports:
   - "2182:2181"
 
 kafka:
  image: wurstmeister/kafka
  container_name: kafka
  ports:
   - "9092:9092"
  environment:
   KAFKA_ADVERTISED_HOST_NAME: localhost
   KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181

Zookeeper has its own config for modifying the client port, which you seem to be confusing with the host port-forwarding.

However, the wurstmeister container has no environment variables to configure it, so you're stuck with 2181.

You probably shouldn't even have a port forward because there's little reason to connect to Zookeeper from the host

Also worth pointing out that that image hasn't been updated since 2019 and Zookeeper has had a few releases since then

Related