Search code examples
pythondockerdocker-composepikacloudamqp

Printing not happening in python pika microservice consumer inside a docker-compose service


I am trying to receive messages from my pika producer. I am following along with this tutorial: https://www.youtube.com/watch?v=0iB5IPoTDts.

I can see that when I manually run docker-compose exec backend bash and then run python consumer.py, I can receive messages and they are being logged to stdout through the print() function. However, when I add the following service to my docker-compose.yml, the container is not logging to stdout:

rabbitmq_queue:
    build:
        context: .
        dockerfile: Dockerfile
    command: 'python consumer.py && echo hello'
    volumes:
        - .:/app
    depends_on:
        - db

There is no error. When I change the command to echo hello, the container prints hello to stdout nicely. Why is my service not logging to stdout? Also, it does seem to be running properly - when I try to throw an exception, my service errors out. When It errors out, it also starts printing messages. It does not print error messages otherwise.

Is there a way to fix this issue?

consumer.py:

import pika

params = pika.URLParameters(
    "hidden thing here"
)

connection = pika.BlockingConnection(params)

channel = connection.channel()

channel.queue_declare(queue="main")


def callback(channel, method, properties, body):
    print("[CONSUMER] Received message in main")
    print(body)
    # raise Exception()

channel.basic_consume(queue="main", on_message_callback=callback, auto_ack=True)

print("[CONSUMER] Started consuming")

channel.start_consuming()

channel.close()

Solution

  • Same problem. Working container with no output. I added PYTHONUNBUFFERED=1 for enable log.

    # Dockerfile
    ENV PYTHONUNBUFFERED=1