I've tried to follow the advice of some other excellent posts such as this one and others, but still running into problems.
I have two docker compose networks, one running kafka/zookeeper and another running a python application which includes a kafka producer, so needs to write to the container running kafka. I've created a network kafka_logging_network
which both containers are a part of. I've double checked the KAFKA_ADVERTISED_LISTENERS
variable many times but cant see what I'm missing.
The below shows the two docker-compose.yml
files, followed by the Dockerfile
and main.py
for the application. main.py
should run without error but in reality it cannot find any kafka brokers at the given KAFKA_HOST
and KAFKA_PORT
, raising kafka.errors.NoBrokersAvailable
. Where have I gone wrong?
kafka/docker-compose.yml (for kafka/zookeeper)
version: '3.7'
services:
zoo1:
image: confluentinc/cp-zookeeper:7.3.2
hostname: zoo1
container_name: zoo1
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_SERVER_ID: 1
ZOOKEEPER_SERVERS: zoo1:2888:3888
networks:
- logging_network
kafka1:
image: confluentinc/cp-enterprise-kafka:5.3.1
hostname: kafka1
container_name: kafka1
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:29092,PLAINTEXT_HOST://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka1:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
depends_on:
- zoo1
restart: always
networks:
- logging_network
networks:
logging_network:
driver: bridge
app_folder/docker-compose.yml (for python application)
version: '3.7'
services:
pyapp:
build:
context: .
dockerfile: Dockerfile
container_name: pyapp
networks:
- kafka_logging_network
environment:
KAFKA_HOST: kafka1
KAFKA_PORT: 29092
KAFKA_TOPIC: logs-topic
networks:
kafka_logging_network:
external: true
Dockerfile
FROM python:3.9.7
COPY . .
RUN pip install kafka_python==2.0.2
ENTRYPOINT python main.py
main.py
from kafka import KafkaProducer
import os
host = os.environ['KAFKA_HOST']
port = os.environ['KAFKA_PORT']
kp = KafkaProducer(bootstrap_servers=f'{host}:{port}')
print('success')