Getting pika.exceptions.StreamLostError: Transport indicated EOF while running python script docker image which using pika
Asked Answered
L

3

9

I am using Python which is using RabbitMQ for input and output. I am able to run my script locally without any errors but when I try to Dockerize that script and run it it's giving me the following error:

Traceback (most recent call last):
File "./Kusto_connection_with_rabbitmq_2.py", line 1674, in <module>
 main()
File "./Kusto_connection_with_rabbitmq_2.py", line 1668, in main
 channel.start_consuming()
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 1865, 
  in start_consuming
self._process_data_events(time_limit=None)
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 2026, 
  in _process_data_events    self.connection.process_data_events(time_limit=time_limit)
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 824, 
  in process_data_events
self._flush_output(common_terminator)
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 523, 
in _flush_output
 raise self._closed_result.value.error
 pika.exceptions.StreamLostError: Transport indicated EOF

Below is the my Python code which is connecting to RabbitMQ:

credentials = pika.PlainCredentials(username, password)
parameters = pika.ConnectionParameters(host=Api_url,virtual_host=rmqvhost,credentials=credentials,heartbeat=0)
print (username,password)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

channel.queue_declare(queue='test',durable=True)


channel.basic_qos(prefetch_size=0,prefetch_count=1) # this is for acknowdeging packet one by one 
channel.basic_consume(queue='test', on_message_callback=callback,auto_ack=False)


print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

My Dockerfile:

FROM python:3.8
WORKDIR /First_try
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY Kusto_connection_with_rabbitmq_2.py .
CMD ["python","./Kusto_connection_with_rabbitmq_2.py"]

I run my Docker container with

docker run <image_name>
Loma answered 21/12, 2021 at 11:3 Comment(4)
Are you sure that rabbitmq is accessible from your docker container? Can you specify full code listings of your tries to dockerize this script?Bronze
FROM python:3.8 WORKDIR /First_try COPY requirements.txt . RUN pip install -r requirements.txt COPY Kusto_connection_with_rabbitmq_2.py . CMD ["python","./Kusto_connection_with_rabbitmq_2.py"] my docker fileLoma
Thank you. And could you also specify how do you run your docker?Bronze
docker run image_nameLoma
A
4

Maybe your connection is being interrupted, and pika is declaring your client dead. Try setting the heartbeat in your parameters to 30 or so.

Analyst answered 1/4, 2022 at 20:11 Comment(1)
worked for me.. for my refAmendatory
C
4

In my case I was connecting to the wrong port. 5672 is default for AMQP connections and 15672 is default for HTTP API. pika.ConnectionParameters wants host and port for AMQP. When I pass it 15672 I get StreamLostError: Transport indicated EOF. Pass it 5672 (or leaving port blank) and it connects without issue. In your browser, you'll want to use http(s)://<your_rabbitmq_host>:15672

Communard answered 5/10, 2023 at 20:5 Comment(0)
B
2

I suppose your problem is you don't have access to rabbitmq from your docker container.

Here is simple example of usage rabbitmq from python using docker-compose, you should try to implement your solution relying on this example.

Bronze answered 21/12, 2021 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.