我正在建立一个非常简单的项目。两个python脚本; producer.py
和consumer.py
。生产者发送随机数,消费者打印它。它没有docker也可以工作。
[我为消费者和生产者构建了两个docker映像,并希望通过docker-compose“通信”它,当我运行docker-compose文件时,我看到以下内容:
emit_log_1 | Traceback (most recent call last):
emit_log_1 | File "emit_log.py", line 7, in <module>
emit_log_1 | connection = pika.BlockingConnection(
emit_log_1 | File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 359, in __init__
emit_log_1 | self._impl = self._create_connection(parameters, _impl_class)
emit_log_1 | File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 450, in _create_connection
emit_log_1 | raise self._reap_last_connection_workflow_error(error)
emit_log_1 | pika.exceptions.AMQPConnectionError
1_emit_log_1 exited with code 1
与接收完全相同:
Docker-compose.yml:
version: '3'
services:
rabbitmq:
image: rabbitmq:3-management
ports:
- "5672:5672"
- "15672:15672"
emit_log:
build:
context: .
dockerfile: Dockerfile.emit_log
ports:
- "8081:8081"
- "8001:8001"
links:
- rabbitmq
receive:
build:
context: .
dockerfile: Dockerfile.receive
ports:
- "8082:8082"
- "8002:8002"
links:
- rabbitmq
我怀疑您可能正在尝试从发射服务内部连接到“ localhost”,该服务可能指向容器localhost本身。默认情况下,Compose将网络容器放在一起。如果您的连接字符串以前在本地主机上,则需要更新它们以指向所需的容器。
例如,如果您以前是从生产者或消费者连接到localhost:5672
,并且它们与您的Rabbitmq实例在docker网络中组成,则需要使用rabbitmq:5672
连接>
查看有关Compose中网络的详细信息:https://docs.docker.com/compose/networking/