I am facing a weird issue during the composing of my RabbitMQ container.
When I build the container without my python script, which creates the test structure of my RabbitMQ, it works fine. I access the container, run the script manually, and everything gets created perfectly, with no errors.
If I run the script with the same command (“python3 manager.py”), but in a RUN entry at the Dockerfile, it’s like it is suddenly unable to find the hostname or something like that during the RabbitMQ connector creation. So, it aborts the creation of the container.
I have tried executing it as a background Linux process, and the container is created, but the RabbitMQ structure creation keeps failing.
version: "3.8" services: rabbitmq: container_name: rabbitmq image: rabbitmq build: src/server/ env_file: - src/server/server.env ports: - "15672:15672" - "5672:5672" hostname: rabbitmq networks: - rabbitmqnet networks: rabbitmqnet: name: rabbitmqnet driver: bridge
FROM rabbitmq:3-management WORKDIR /app EXPOSE 15672 COPY . /app RUN apt-get update -y RUN apt-get install -y python python3-pip RUN pip install -r requirements.txt RUN python3 manager.py
import pika import config connection = pika.BlockingConnection(pika.ConnectionParameters(config.server, config.port, "https://stackoverflow.com/", pika.PlainCredentials(config.user, config.password))) channel = connection.channel() def main(): createQueue("test-queue") createExchange("test-exchange") createBinding("test-exchange", "test-queue", "test") # This method creates a queue. def createQueue(qName): channel.queue_declare(queue=qName) # This method creates an exchange. def createExchange(eName): channel.exchange_declare( exchange=eName, exchange_type="direct" ) # This method creates a binding routing key between an exchange and a queue. This allows the publisher to send messages to the queue through the exchange. def createBinding(eName, qName, routingKey): channel.queue_bind( exchange=eName, queue=qName, routing_key=routingKey ) if __name__ == "__main__": main()
server="rabbitmq" port= 5672 user="user" password= 'password'
Leave an answer