Docker中的Rabbitmq IP地址和Redis IP地址

时间:2017-10-13 14:54:47

标签: docker redis rabbitmq

在Docker之外,redis和rabbitmq的ip地址被设置为' localhost'在我的django应用程序在ubuntu 16.04环境中。但是,我正在尝试将我的Web服务迁移到docker中。我使用Docker的官方存储库redis和rabbitmq。

在我的Django settings.py中,我必须说明redis和rabbitmq的ip地址,以便在我的应用程序中连接它们。我不知道如何在Docker中确定他们的IP地址。在docker-compose.yml中,配置如下:

version: '2'

services:

  # RabbitMQ
  rabbit:
    hostname: my-rabbit
    image: rabbitmq:3
    ports:
      - "5672:5672"  # we forward this port because it's useful for 
debugging
      - "15672:15672"  # here, we can access rabbitmq management plugin

  #Redis
  redis:
    image: redis:latest

  #Data Processing WS
  web:
   build: .
   command: python manage.py runserver 0.0.0.0:9060 --noreload
    volumes:
      - .:/srv
   ports:
      - "9060:9060"
    links: ["rabbit", "redis"]

当我尝试将他们的IP设置为' localhost'时,会发生错误

settings.CONNECTION = pika.BlockingConnection(pika.ConnectionParameters(host=settings.RABBIT_MQ_
IP_ADDRESS, heartbeat_interval=0))
web_1     |   File "/usr/local/lib/python2.7/dist-
packages/pika/adapters/blocking_connection.py", line 339, in __init__
web_1     |     self._process_io_for_connection_setup()
web_1     |   File "/usr/local/lib/python2.7/dist-
packages/pika/adapters/blocking_connection.py", line 374, in 
_process_io_for_connection_setup
web_1     |     self._open_error_result.is_ready)
web_1     |   File "/usr/local/lib/python2.7/dist-
packages/pika/adapters/blocking_connection.py", line 395, in _flush_output
web_1     |     raise exceptions.ConnectionClosed()
web_1     | pika.exceptions.ConnectionClosed

1 个答案:

答案 0 :(得分:0)

docker-compose.yml my-rabbit作为主机名,但在links部分,您有rabbit。这些需要匹配。

尝试将RabbitMQ主机名更改为rabbit

此外,对于web服务,您应指定此网络应用depends on rabbitredis。这样可以确保在rabbit之前启动redisweb。根据您的错误,当网络启动时,RabbitMQ可能还没有完全启动。

depends_on: 
  - rabbit
  - redis