Search code examples
pythondockerdocker-composerabbitmqpika

Using pika, how to connect to rabbitmq running in docker, started with docker-compose with external network?


I have the following docker-compose file:

version: '2.3'
networks:
    default: { external: true, name: $NETWORK_NAME } # NETWORK_NAME in .env file is `uv_atp_network`.

services:
    car_parts_segmentor:
#        container_name: uv-car-parts-segmentation
        image: "uv-car-parts-segmentation:latest"
        ports:
            - "8080:8080"
        volumes:
            - ../../../../uv-car-parts-segmentation/configs:/uveye/configs
            - /isilon/:/isilon/
#            - local_data_folder:local_data_folder
        command: "--run_service rabbit"
        runtime: nvidia
        depends_on:
          rabbitmq_local:
            condition: service_started
        links:
          - rabbitmq_local
        restart: always


    rabbitmq_local:
        image: 'rabbitmq:3.6-management-alpine'
        container_name: "rabbitmq"
        ports:
          - ${RABBIT_PORT:?unspecified_rabbit_port}:5672
          - ${RABBIT_MANAGEMENT_PORT:?unspecified_rabbit_management_port}:15672

When this runs, docker ps shows

21400efd6493   uv-car-parts-segmentation:latest                             "python /uveye/app/m…"   5 seconds ago   Up 1 second    0.0.0.0:8080->8080/tcp, :::8080->8080/tcp                                                                                            joint_car_parts_segmentor_1
bf4ab8581f1f   rabbitmq:3.6-management-alpine                               "docker-entrypoint.s…"   5 seconds ago   Up 4 seconds   4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp   rabbitmq

I want to create a connection to that rabbitmq. The user:pass is guest:guest.

I was unable to do it, with the very uninformative AMQPConnectionError in all cases:

Below code runs in another, unrelated container.

connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@rabbitmq/"))
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@localhost/"))

Also tried with

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' rabbitmq
172.27.0.2

and

connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:[email protected]/")) #

Also tried with


    credentials = pika.credentials.PlainCredentials(
        username="guest",
        password="guest"
    )
    parameters = pika.ConnectionParameters(
        host=ip_address, # tried all above options
        port=5672,
        credentials=credentials,
        heartbeat=10,
    )


Note that the container car_parts_segmentor is able to see the container rabbitmq. Both are started by docker-compose.


My assumption is this has to do with the uv_atp_network both containers live in, and I am trying to access a docker inside that network, from outside the network.

Is this really the problem?

If so, how can this be achieved?

For the future - how to get more informative errors from pika?


Solution

  • As I suspected, the problem was the name rabbitmq existed only in the network uv_atp_network.

    The code attempting to connect to that network runs inside a container of its own, which was not present in the network.

    Solution connect the current container to the network:

    import socket
    
    
    client = docker.from_env()
    network_name = "uv_atp_network"
    atp_container = client.containers.get(socket.gethostname())
    client.networks.get(network_name).connect(container=atp_container.id)
    

    After this, the above code in the question does work, because rabbitmq can be resolved.

    connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@rabbitmq/"))