I am very new to docker and Kafka, and have a simple kafka python publisher shown below
The following are in my dockerfile:
FROM python:3.10
WORKDIR /app
COPY . /app
RUN pip install --user pip==23.0.1 && pip install pipenv && pipenv install --system
ENV ENVIRONMENT=production
CMD ["python3", "src/consumer.py"]
as well as my yaml file for compose:
version: '3'
services:
zookeeper:
image: confluent/zookeeper
container_name: zookeeper
ports:
- "2181:2181"
kafka:
image: confluent/kafka
container_name: kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
publisher:
container_name: publisher
build:
context: ./publisher
dockerfile: Dockerfile
environment:
KAFKA_BOOTSTRAP_SERVERS: kafka:9092
KAFKA_TOPIC: metrics
KAFKA_BROKER: kafka:9092
depends_on:
- kafka
consumer:
container_name: consumer
build:
context: ./consumer
dockerfile: Dockerfile
environment:
KAFKA_BOOTSTRAP_SERVERS: kafka:9092
KAFKA_TOPIC: test_topic
KAFKA_BROKER: kafka:9092
depends_on:
- kafka
in consumer.py
I have:.
print('-- consumer script -- ')
import json
from kafka import KafkaProducer, KafkaConsumer
test_topic = "test_topic"
consumer = KafkaConsumer(auto_offset_reset='earliest', bootstrap_servers=['kafka:9092'],
api_version=(0, 10), consumer_timeout_ms=10000)
consumer.subscribe([test_topic])
for message in consumer: # the line that seems to influence docker attachement
print('-----loop works: ', message)
In one terminal I frist run:
docker-compose -f docker-compose.yml up zookeeper
and similarly for Kafka:
docker-compose -f docker-compose.yml up kafka
and then:
docker-compose -f docker-compose.yml up consumer
But the terminal seems stuck with the following lines:
Starting consumer ... done
Attaching to consumer
When I remove the line for message in consumer
, the code follows without issue