I have two containers one is kafka container open port 9092:9092 and another is Fastapi container. If I don’t dockerize Fastapi, I can make rest api request to fastapi to kafka. It sends message to kafka via fastapi. But when I dockerize fastapi can’t connect fastapi container to kafka container.
I cant run fastapi docker file with -p 8000:8000 -p 9092:9092 it says 9092 is already used.
How can I make request to fastapi container then fastapi connects to kafka container.
fastapi dockerfile
FROM python:3.8.10
ADD . .
COPY requirements.txt .
RUN pip3 install -r requirements.txt
CMD ["python3", "main.py"]
My error is
kafka.errors.NoBrokersAvailable: NoBrokersAvailable
.
I get kafka container IP address and I am making to kafka container IP address example
producer = KafkaProducer(bootstrap_servers=containerip, value_serializer=lambda x: json.dumps(x).encode('utf-8'),api_version=(2)), lines=True, orient='records')
2
Answers
If you set network mode of your container to host, it’s done.
run your fast-api (pay attention to
network
switch):run kafka:
run postgres:
but it’s better to use bridge-networks.
Remove it then. Unclear why you need port 9092 on your API, anyway ; it’s not the Kafka service.
Without seeing your complete Kafka client code, it’s hard to say what your other problems are, so please consult Connect to Kafka running in Docker
For example, what is
containerip
? This should bekafka:9092
(if you follow the instructions in the linked post)docker network create
docker run
with--network
on both containersKAFKA_ADVERTISTED_LISTENERS
variable contains at leastINTERNAL://kafka:9092
-p
flags for the Kafka container since you are only interacting with Kafka from another container.kafka:9092