我正在尝试将Python应用程序连接到RabbitMQ服务器。 Python应用程序和RabbitMQ服务器都在Docker容器中运行。我遇到的问题是我正在尝试使用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
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
适用于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"]
Python应用代码片段:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq'))
我还检查了所有容器是否都在同一网络中,以及它们是否在同一网络中
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"
}
}
]
我还尝试了在其他图像上运行Python应用程序,然后检查了nslookup rabbitmq
,并且我收到了Rabbitmq服务器的正确IP地址。在这一点上,我认为我已经尝试了一切,但我不知道还要检查什么。知道我该如何解决吗?另外,我的.NET Core应用程序可以与rabbitmq
主机连接,没有任何问题。
我正在运行Windows的Docker。
答案 0 :(得分:0)
我相信您会错过一件事,如果那不起作用,您应该尝试另一件事。
首先,
您是否暴露了必要的端口?运行容器时,请执行以下操作:
docker run -p 15672:15672 -p 5672:5672 <your-rabbitmq-container>
我非常肯定这是端口问题。
第二,
如果这不起作用,是否可以尝试将host='rabbitmq'
替换为host='<ip-address>'
,您可以通过docker exec -it <ip-address> /bin/bash
找到您的IP地址,然后在容器中输入hostname -I
>
最后,
RabbitMQ在默认来宾用户的本地主机之外无法使用。您将需要一个用户,以便可以在localhost之外使用它。但是,首先只需添加端口并尝试!