使用pika,如何连接到docker中的rabbitmq 运行,开始于docker-compose with external network?
Using pika, how to connect to rabbitmq running in docker, started with docker-compose with external network?
我有以下 docker-compose 文件:
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
运行时,docker ps
显示
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
我想创建到那个 rabbitmq 的连接。 user:pass 是 guest:guest
.
我无法做到这一点,在所有情况下都非常缺乏信息 AMQPConnectionError
:
下面的代码在另一个不相关的容器中运行。
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@rabbitmq/"))
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@localhost/"))
也试过
$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' rabbitmq
172.27.0.2
和
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@172.27.0.2/")) #
也试过
credentials = pika.credentials.PlainCredentials(
username="guest",
password="guest"
)
parameters = pika.ConnectionParameters(
host=ip_address, # tried all above options
port=5672,
credentials=credentials,
heartbeat=10,
)
请注意,容器 car_parts_segmentor
能够看到容器 rabbitmq
。两者都由 docker-compose.
启动
我的假设是这与 uv_atp_network
两个容器都存在有关,我正在尝试从网络外部访问该网络内部的 docker。
真的是这个问题吗?
如果是这样,如何实现?
对于未来——如何从鼠兔那里得到更多信息错误?
如我所料,问题是名称 rabbitmq
仅存在于网络 uv_atp_network
。
尝试连接到该网络的代码在它自己的容器内运行,该容器不存在于网络中。
解决connect the current container网络:
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)
此后题中上述代码确实有效,因为rabbitmq
可以解决
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@rabbitmq/"))
我有以下 docker-compose 文件:
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
运行时,docker ps
显示
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
我想创建到那个 rabbitmq 的连接。 user:pass 是 guest:guest
.
我无法做到这一点,在所有情况下都非常缺乏信息 AMQPConnectionError
:
下面的代码在另一个不相关的容器中运行。
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@rabbitmq/"))
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@localhost/"))
也试过
$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' rabbitmq
172.27.0.2
和
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@172.27.0.2/")) #
也试过
credentials = pika.credentials.PlainCredentials(
username="guest",
password="guest"
)
parameters = pika.ConnectionParameters(
host=ip_address, # tried all above options
port=5672,
credentials=credentials,
heartbeat=10,
)
请注意,容器 car_parts_segmentor
能够看到容器 rabbitmq
。两者都由 docker-compose.
我的假设是这与 uv_atp_network
两个容器都存在有关,我正在尝试从网络外部访问该网络内部的 docker。
真的是这个问题吗?
如果是这样,如何实现?
对于未来——如何从鼠兔那里得到更多信息错误?
如我所料,问题是名称 rabbitmq
仅存在于网络 uv_atp_network
。
尝试连接到该网络的代码在它自己的容器内运行,该容器不存在于网络中。
解决connect the current container网络:
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)
此后题中上述代码确实有效,因为rabbitmq
可以解决
connection = pika.BlockingConnection(pika.URLParameters("amqp://guest:guest@rabbitmq/"))