1

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:guest@172.27.0.2/")) #

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?

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • I would expect the Compose service name `rabbitmq_local` to work as a host name, provided both are on the same Compose network (in your example, both are on `default`); it shouldn't matter that the network is externally-defined. Deleting the obsolete `links:` option shouldn't make a difference, but won't hurt. What is the complete AMQPConnectionError? – David Maze Feb 08 '22 at 11:59
  • @DavidMaze How to get the complete error? Knowing this could allow me to solve my own problems :( All I get is `AMQPConnectionError`, and `socket.gaierror: [Errno -2] Name or service not known`. Don't know what is not resolving properly. There is no more details I can see about the `AMQPConnectionError`, but I guess there is a way to see them I am not aware of? – Gulzar Feb 08 '22 at 12:20
  • @DavidMaze Same happens with `rabbitmq_local`. Also, why does using this name make sense? We see from command line the name `rabbitmq` is resolved to an IP – Gulzar Feb 08 '22 at 12:23
  • Also, `docker.from_env().containers.get("rabbitmq").attrs["NetworkSettings"]["Networks"]["uv_atp_network"]["IPAddress"]` does resolve, to the same IP address as the command line. With "rabbitmq_local", getting `404 Client Error for .... No such container ...` – Gulzar Feb 08 '22 at 12:30
  • @DavidMaze FYI the problem was indeed the network. Thanks :) Still would like to know how to get better information from pika errors – Gulzar Feb 08 '22 at 15:03
  • It's informative that you're getting "Name or service not known" ("the `rabbitmq_local` name doesn't make sense to me") as opposed to, say, "connection refused" ("that host name is correct but the port isn't right or the service isn't actually running"). The error doesn't quite make sense to me since the two containers you show are both on the `default` network and you have the `depends_on:` line; `rabbitmq_local` really should work as a host name. – David Maze Feb 08 '22 at 15:17
  • @DavidMaze The code above also runs within a 3rd, external docker container – Gulzar Feb 08 '22 at 15:19

1 Answers1

1

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/"))
Gulzar
  • 23,452
  • 27
  • 113
  • 201