【问题标题】:docker-compose and django-haystackdocker-compose 和 django-haystack
【发布时间】:2015-12-23 19:10:23
【问题描述】:

我正在尝试让 docker-compose 和 django-haystack 一起工作。我正在使用以下设置(不同容器中的 Web 和弹性搜索)并在尝试构建索引时看到错误。

我已经稍微缩小了问题范围,看起来 elasticsearch 容器正在按预期工作。但是,haystack 后端无法建立连接。

所有容器:

root@movie-new:/home/django/movie# docker-compose ps
        Name                       Command               State                       Ports
---------------------------------------------------------------------------------------------------------------
movie_data_1            /docker-entrypoint.sh true       Up      5432/tcp
movie_db_1              /docker-entrypoint.sh postgres   Up      0.0.0.0:5432->5432/tcp
movie_elasticsearch_1   /docker-entrypoint.sh elas ...   Up      0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp
movie_nginx_1           /usr/sbin/nginx                  Up      0.0.0.0:80->80/tcp
movie_web_1             bash -c python manage.py m ...   Up      8000/tcp
movie_web_run_1         /bin/bash                        Up      8000/tcp
movie_web_run_3         /bin/bash                        Up      8000/tcp

在我的网络容器内..

root@movie-new:/home/django/movie# docker-compose run --rm web /bin/bash
root@0351ddc88229:/usr/src/app# curl -XGET http://elasticsearch:9200/
{
  "name" : "Dream Weaver",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.1.1",
    "build_hash" : "40e2c53a6b6c2972b3d13846e450e66f4375bd71",
    "build_timestamp" : "2015-12-15T13:05:55Z",
    "build_snapshot" : false,
    "lucene_version" : "5.3.1"
  },
  "tagline" : "You Know, for Search"
}

settings.py

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://elasticsearch:9200/',
        'INDEX_NAME': 'haystack',
        'TIMEOUT' : 120
    },
}

root@4b397e3ad5dc:/usr/src/app# python manage.py rebuild_index

WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
Are you sure you wish to continue? [y/N] y
Removing all documents from your index because you said so.
Failed to clear Elasticsearch index: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7fc67c8192d0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7fc67c8192d0>: Failed to establish a new connection: [Errno 111] Connection refused)
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py", line 234, in clear
    self.conn.indices.delete(index=self.index_name, ignore=404)
  File "/usr/local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/elasticsearch/client/indices.py", line 198, in delete
    params=params)
  File "/usr/local/lib/python2.7/site-packages/elasticsearch/transport.py", line 307, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/usr/local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 89, in perform_request
    raise ConnectionError('N/A', str(e), e)
ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7fc67c8192d0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7fc67c8192d0>: Failed to establish a new connection: [Errno 111] Connection refused)

【问题讨论】:

  • 在没有 docker-compose.yml 的情况下很难调试,可能是 docker-entrypoint.sh 等(与 Docker 相关)。根据我的经验,这些连接问题通常来自 Docker。

标签: python django django-haystack docker-compose


【解决方案1】:

我认为发生的情况是脚本假设 elasticsearch 已经可用,但是当所有容器同时启动时,elasticsearch 可能仍在启动但还不可用。

在运行迁移之前,要么休眠固定秒数,要么重试连接几次,并在重试之间短暂休眠。

【讨论】:

  • 谢谢,我会试试的。我假设添加依赖项将确保链接发生在最后,此时可能已经创建了容器。
  • 我更新了问题。我可以从 web 卷曲到 elasticsearch 容器,但是 haystack 连接仍然失败。有什么想法吗?
  • 我今天也碰壁了...有什么帮助吗?
【解决方案2】:

您设置连接的位置

connections.create_connection()

connections.create_connection(
    alias='default', 
    hosts=['http://elasticsearch:9200'], 
    timeout=60)

这里是文档 https://elasticsearch-dsl.readthedocs.io/en/latest/configuration.html#single-connection-with-an-alias

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 2021-07-23
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多