简体   繁体   English

容器化的 python 应用程序无法连接到在 Docker 容器上运行的 RabbitMQ 服务器

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

I'm trying to connect my Python application to RabbitMQ server.我正在尝试将我的 Python 应用程序连接到 RabbitMQ 服务器。 Both Python app and RabbitMQ server are running in Docker containers. Python 应用程序和 RabbitMQ 服务器都在 Docker 容器中运行。 Problem that I'm having is that I'm trying to connect using my Python app to Server and I'm receiving error:我遇到的问题是我正在尝试使用我的 Python 应用程序连接到服务器,但收到错误消息:

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 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用于 Python 应用程序的 Dockerfile

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: Python App 代码片段:

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.我也尝试在不同的图像上运行 Python 应用程序,然后检查nslookup rabbitmq并且我收到了 rabbitmq 服务器的正确 IP 地址。 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.另一件事是我的 .NET Core 应用程序与rabbitmq主机连接没有任何问题。

I am running Docker for Windows.我正在为 Windows 运行 Docker。

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>运行容器时执行: 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如果这不起作用,您可以尝试将host='rabbitmq'替换为host='<ip-address>'您可以通过docker exec -it <ip-address> /bin/bash找到您的 ip-address 并输入hostname -I在你的容器里

Lastly,最后,

RabbitMQ will not work outside of localhost with the default guest user. RabbitMQ 将无法在本地主机之外使用默认访客用户工作。 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!但是,首先只需添加端口并尝试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过 docker 容器连接到 RabbitMQ - Can't connect to RabbitMQ via docker container 无法将 celery 与 docker 上的 rabbitmq 服务器连接 - Can't connect celery with rabbitmq server on docker 如何通过运行带有URL的docker容器来连接docker容器中的python应用 - how to connect python app in docker container with running docker container with url 无法使用 Dockerfile 连接到 docker 容器(python 服务器) - Can not connect to docker container(python server) with Dockerfile 无法连接到在 Docker 容器中运行的 ActiveMQ 控制台 - Can't connect to ActiveMQ Console running in Docker container 无法连接到Windows Server 2016上的docker container python flask应用程序 - unable to connect to docker container python flask app on a windows server 2016 无法将客户端连接到 docker python 服务器 - Can't connect client to docker python server Mariadb docker容器无法使用Python连接到主机上的MySQL服务器(111连接被拒绝) - Mariadb docker container Can't connect to MySQL server on host (111 Connection refused) with Python 如何连接rabbitMQ docker容器? - How to connect to the rabbitMQ docker container? 为什么我无法连接到docker容器中的flask服务器? - Why can't I connect to the flask server in docker container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM