简体   繁体   中英

Containerized python app can't connect to RabbitMQ server running on Docker container

I'm trying to connect my Python application to RabbitMQ server. Both Python app and RabbitMQ server are running in Docker containers. Problem that I'm having is that I'm trying to connect using my Python app to Server and I'm receiving error:

Traceback (most recent call last):
  File "cfg_rpc_server.py", line 9, in <module>
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq'))
  File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 359, in __init__
    self._impl = self._create_connection(parameters, _impl_class)
  File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 450, in _create_connection
    raise self._reap_last_connection_workflow_error(error)
  File "/usr/local/lib/python3.8/site-packages/pika/adapters/utils/selector_ioloop_adapter.py", line 562, in _resolve
    result = socket.getaddrinfo(self._host, self._port, self._family,
  File "/usr/local/lib/python3.8/socket.py", line 914, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

docker-compose.yml

version: '3.4'

services:
  seq:
    image: datalust/seq:latest

  rabbitmq:
    image: rabbitmq:3.8.1-management-alpine

  mongodb:
    image: mongo
    volumes:
      - mongodb_volume:/data/db

  apigateway:
    image: ${DOCKER_REGISTRY-}apigateway
    build:
      context: .
      dockerfile: src/ApiGateway/Dockerfile

  detection.api:
    image: ${DOCKER_REGISTRY-}detectionapi
    build:
      context: .
      dockerfile: src/Services/Detection/Detection.API/Dockerfile
    depends_on:
      - rabbitmq
      - mongodb
    volumes:
      - ./data/output:/output

  training.api:
    image: ${DOCKER_REGISTRY-}trainingapi
    build:
      context: .
      dockerfile: src/Services/Training/Training.API/Dockerfile
    depends_on:
      - rabbitmq
      - mongodb

  webmvc:
    image: ${DOCKER_REGISTRY-}webmvc
    build:
      context: .
      dockerfile: src/Web/WebMVC/Dockerfile      

  mgr: # My python app
    image: ${DOCKER_REGISTRY-}mgr
    build:
        context: .
        dockerfile: src/BuildingBlocks/ConfigManager/Dockerfile
    depends_on:
      - rabbitmq
      - mongodb
volumes:
  mongodb_volume:
    external: false

Dockerfile for Python app

FROM python:slim-buster

RUN mkdir /app
WORKDIR /app
COPY ["src/BuildingBlocks/ConfigManager", "."]

RUN pip install -r requirements.txt

CMD ["python", "cfg_rpc_server.py"]

Fragment of Python App code:

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq'))

I've also checked if all containers are in the same network and in did they are

PS C:\Windows\system32> docker network inspect c6818581e269
[
    {
        "Name": "dockercompose6314373887499575973_default",
        "Id": "c6818581e269d602cd6f774747845552341fb282d6be780049e716b5ad0c7be3",
        "Created": "2019-12-06T15:37:10.8675273Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.26.0.0/16",
                    "Gateway": "172.26.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "10fd0dd1cc98770ddd69c5d9ce07d51a6714a9f9826a27fb8d52309f5a2055e3": {
                "Name": "Detection.API",
                "EndpointID": "d21f3433119f6671a214ba48513160ba5af9441829ca29da1edbcf79bd137098",
                "MacAddress": "02:42:ac:1a:00:09",
                "IPv4Address": "172.26.0.9/16",
                "IPv6Address": ""
            },
            "325c6dda2bd14b4d08c6dbbf37672823529accac1d5152aadf336f96688f8923": {
                "Name": "Training.API",
                "EndpointID": "51124c1531d93cc718c1c4cfd92b2ea6ff1ac2f5cc0512db3993e90532199874",
                "MacAddress": "02:42:ac:1a:00:08",
                "IPv4Address": "172.26.0.8/16",
                "IPv6Address": ""
            },
            "3941454d159d0665851b332c89494eda137f95f8ae48b3731a3ebe6189defb85": {
                "Name": "dockercompose6314373887499575973_mgr_1",
                "EndpointID": "9d92c5aae7ce6e794d2e158fd3709bc1df726c937f4d681f8aaf0ef6be307291",
                "MacAddress": "02:42:ac:1a:00:03",
                "IPv4Address": "172.26.0.3/16",
                "IPv6Address": ""
            },
            "5d0971e9091fa431c0a9820d6ff492c3abbc3a1e58009e9a518f4e6c0a244bd0": {
                "Name": "ApiGateway",
                "EndpointID": "a81fe63e9d607ae91a35c06f9a9f400c795a1d287b3bbfa9ccfa41cd0e068240",
                "MacAddress": "02:42:ac:1a:00:04",
                "IPv4Address": "172.26.0.4/16",
                "IPv6Address": ""
            },
            "929290c65a1c2cdcd212383049b37632d7cb86bde5f7870a7f925ff30dac633c": {
                "Name": "dockercompose6314373887499575973_seq_1",
                "EndpointID": "9753b94aeac93dad134bec6689a6c4daed2c6199f5b702f4bee8c90554838ee5",
                "MacAddress": "02:42:ac:1a:00:06",
                "IPv4Address": "172.26.0.6/16",
                "IPv6Address": ""
            },
            "92a18333f5ca808872aadf55a1a4a917c9094b8dc4798df483f3003d4e8a3476": {
                "Name": "dockercompose6314373887499575973_rabbitmq_1",
                "EndpointID": "29340f69550353c3d53f71829541112ad71a67d4c795121297b573b9d7a42ccb",
                "MacAddress": "02:42:ac:1a:00:05",
                "IPv4Address": "172.26.0.5/16",
                "IPv6Address": ""
            },
            "c96dacaa40a4aa1898dce05402054d8a36d9f144837cb83cc1345dbbdb46ae33": {
                "Name": "WebMVC",
                "EndpointID": "a976bb304e22f55f16b74588f3ef972b7d7b00a3fa28b3784e6e57cbd7e96599",
                "MacAddress": "02:42:ac:1a:00:02",
                "IPv4Address": "172.26.0.2/16",
                "IPv6Address": ""
            },
            "d690db70785a7c7cb975ce9a8162ff8e41dd7aadc6e58f506a94df5d194b269d": {
                "Name": "dockercompose6314373887499575973_mongodb_1",
                "EndpointID": "5279d09e077d1c1f0becb611ff31f16d84d388cfe4cbc2357371b895ceddd83c",
                "MacAddress": "02:42:ac:1a:00:07",
                "IPv4Address": "172.26.0.7/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "dockercompose6314373887499575973",
            "com.docker.compose.version": "1.24.1"
        }
    }
]

Also I have tried running Python app on different image and then checked nslookup rabbitmq and I've received correct IP address of rabbitmq server. At this point I think I've tried everything and I don't know what else to check. Any idea how I can fix this ? Also other thing is that my .NET Core app connects with rabbitmq host without any problem.

I am running Docker for Windows.

I believe there is one thing you have missed, and if that does not work there is another thing you should try.

Firstly,

Have you exposed the necessary ports? When running your container do: docker run -p 15672:15672 -p 5672:5672 <your-rabbitmq-container>

I'm quite positive this is a port issue.

Secondly,

If this doesn't work, can you try replacing host='rabbitmq' with host='<ip-address>' you can find your ip-address by docker exec -it <ip-address> /bin/bash and entering hostname -I inside your container

Lastly,

RabbitMQ will not work outside of localhost with the default guest user. You will need to make a user, so that you could use it outside of localhost. But, firstly just add in the ports and try!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM