建立新连接失败,Docker-Compose Django + Haystack + Mysql + ElasticSearch

Failed to establish a new connection, Docker-Compose Django + Haystack + Mysql + ElasticSearch

当我在 url 上输入时 http://0.0.0.0:9200/ 正在工作。

当我想要保存或检索数据时出现以下错误:

ConnectionError(<urllib3.connection.HTTPConnection object at 
0x7f234afdacc0>: Failed to establish a new connection: [Errno 111] 
Connection refused) caused by: 
NewConnectionError(<urllib3.connection.HTTPConnection object at 
0x7f234afdacc0>: Failed to establish a new connection: [Errno 111] 
Connection refused)

我的docker-compose.yml:

version: "2"
services:
  redis:
    image: redis:latest

  rabbit:
    image: rabbitmq:latest
    ports:
      - "5672:5672"
      - "15672:15672"

  mysql:
    image: mysql:5.7.22
    environment:
      MYSQL_DATABASE: db
      MYSQL_ROOT_PASSWORD: 'db'
    ports:
      - 3306

  phpmyadmin:
    image: nazarpc/phpmyadmin
    environment:
      MYSQL_USERNAME: db
    ports:
      - "0.0.0.0:8081:80"
    links:
      - mysql:mysql

  celery_worker:
    build:
     context: .
    command: bash -c "sleep 3 && celery -A wk worker -l debug"
    volumes:
      - /log:/log
      - /tmp:/tmp
      - ./wk:/wk
    env_file:
      - ./envs/development.env
    environment:
      - C_FORCE_ROOT=true
      - BROKER_URL=amqp://guest:guest@rabbit//
    working_dir: /wk
    links:
      - mysql:mysql
      - rabbit:rabbit
      - redis:redis

  celery_worker_refactor:
     build:
     context: .
     command: bash -c "sleep 10 && celery -A wk worker -l error -Ofair -Q refactor"
    volumes:
       - /log:/log
       - /tmp:/tmp
       - ./wk:/wk
    env_file:
      - ./envs/development.env
    environment:
      - C_FORCE_ROOT=true
      - BROKER_URL=amqp://guest:guest@rabbit//
    working_dir: /wk
    links:
      - mysql:mysql
      - rabbit:rabbit
      - redis:redis

  celery_beat:
    build:
     context: .
    command: bash -c "rm -f /tmp/celerybeat.pid && sleep 3 && celery -A wk beat -l debug -s /log/celerybeat --pidfile=/tmp/celerybeat.pid"
    volumes:
      - /log:/log
      - /tmp:/tmp
      - ./wk:/wk
    env_file:
      - ./envs/development.env
    environment:
      - C_FORCE_ROOT=true
      - BROKER_URL=amqp://guest:guest@rabbit//
    working_dir: /wk
    links:
      - mysql:mysql
      - rabbit:rabbit
      - redis:redis

  ssh_server:
    build:
      context: .
      dockerfile: Dockerfile-ssh
    command: /usr/sbin/sshd -D
    environment:
      DEBUG: 'True'
    env_file:
      - ./envs/development.env
    environment:
      - BROKER_URL=amqp://guest:guest@rabbit//
    volumes:
      - ./wk:/wk
    ports:
      - "2222:22"
    links:
      - mysql:mysql
      - redis:redis
      - rabbit:rabbit

  elasticsearch:
    image: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - xpack.security.enabled=false
    volumes:
      - esdata:/usr/share/elasticsearch/data
   ports:
      - "9200:9200"
      - "9300:9300"

  kibana:
    image: kibana
    ports:
      - 5601:5601

  web:
    build:
     context: .
    command: bash -c "sleep 3 && python manage.py runserver 0.0.0.0:8000"
    privileged: true
    env_file:
      - ./envs/development.env
    environment:
      - BROKER_URL=amqp://guest:guest@rabbit//
    volumes:
      - /log:/log
      - /tmp:/tmp
      - ./wk:/wk
    depends_on:
      -  elasticsearch
    links:
      - mysql:mysql
      - redis:redis
      - rabbit:rabbit
      - elasticsearch:elasticsearch
    ports:
       - "0.0.0.0:8000:8000"

volumes:
  esdata:
    driver: local

我尝试在网络和弹性搜索之间创建电子网络,结果相同。 当我调用 http://0.0.0.0:9200/ 时,我从服务器得到一个 JSON.

的响应

我的 Haystack 配置在 settings.py

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE':'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': '0.0.0.0:9200/',
        'INDEX_NAME': 'haystack'
    }
}

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

一些内容所以我可以post这个问题,不仅仅是代码。

你应该使用 elasticsearch 而不是 0.0.0.0:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE':'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
>>>     'URL': 'elasticsearch:9200',
        'INDEX_NAME': 'haystack'
    }
}