My solution to this issue is slightly different. I configure Kafka to advertise on kafka host and, because it’s exposed on the host machine on localhost:9092, I add an entry in /etc/hosts for kafka to resolve to localhost. By doing this Kafka can be accessed from both other Docker containers and from localhost.
docker-compose.yml:
my-web-service:
build: ./my-web-service
ports:
- "8000:8000"
links:
- kafka
kafka:
image: "wurstmeister/kafka:0.10.2.0"
ports:
- "9092:9092"
hostname: kafka
links:
- zookeeper
environment:
- KAFKA_ADVERTISED_HOST_NAME=kafka
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_ADVERTISED_PORT=9092
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
Updated hosts file:
more /etc/hosts
127.0.0.1 localhost kafka