skip to Main Content

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


  1. If you set network mode of your container to host, it’s done.

    run your fast-api (pay attention to network switch):

    docker run --network host -p 8000:8000 fast-api
    

    run kafka:

    docker run -p 9092:9092 kafka
    

    run postgres:

    docker run -p 5432:5432 postgres
    

    but it’s better to use bridge-networks.

    Login or Signup to reply.
  2. cant run fastapi docker file with -p 8000:8000 -p 9092:9092 it says 9092 is already used.

    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 be kafka:9092 (if you follow the instructions in the linked post)

    1. Run docker network create
    2. Make sure you use docker run with --network on both containers
    3. Ensure KAFKA_ADVERTISTED_LISTENERS variable contains at least INTERNAL://kafka:9092
    4. Remove the -p flags for the Kafka container since you are only interacting with Kafka from another container.
    5. Connect Python to kafka:9092
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search