Docker-compose 和 Django-haystack

3

我正在尝试让docker-compose和django-haystack一起工作。我正在使用以下设置(Web和Elasticsearch在不同的容器中),当我尝试构建我的索引时,会出现错误。

我已经缩小了问题范围,看起来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

在我的web容器中...
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引起的,根据我的经验。 - Özer
2个回答

1
我认为正在发生的是脚本假设elasticsearch已经可用,但当所有容器同时启动时,elasticsearch可能仍在启动过程中,尚不可用。
在运行迁移之前,请等待固定秒数或重试连接几次,并在重试之间进行短暂的休眠。

谢谢,我会尝试一下。我假设添加依赖项将确保在最后进行链接,此时容器可能已经被创建。 - Rajesh Chamarthi
我更新了问题。我能够从Web到elasticsearch容器进行curl,但是haystack连接仍然失败。有什么想法吗? - Rajesh Chamarthi
今天我也遇到了这个问题... 有什么帮助吗? - jacoor

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接