Docker - Elasticsearch - 无法建立新连接:[Errno 111] Connection refused',))

时间:2016-12-30 06:31:47

标签: python elasticsearch docker docker-compose

我正在使用docker-compose在Linode服务器上运行python flask app和elasticsearch服务。

这是我的docker-compose.yml

version: '2'

services:
  elasticsearch:
    build: config/elastic/
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      ES_JAVA_OPTS: "-Xms1g -Xmx1g"
    networks:
      - docker_lr
  web:
    build: .
    ports:
      - "8000:8000"
    networks:
      - docker_lr
    depends_on:
      - elasticsearch

networks:
  docker_lr:
    driver: bridge

这是elasticsearch Dockerfile

FROM elasticsearch:5

ENV ES_JAVA_OPTS="-Des.path.conf=/etc/elasticsearch"

CMD ["-E", "network.host=0.0.0.0", "-E", "discovery.zen.minimum_master_nodes=1"]

以下是适用于网络的Dockerfile https://github.com/mysticmode/LibreRead/blob/master/Dockerfile

这两项服务都在运行,我可以使用my:8000和:9200

在浏览器中查看

但是我无法从我的python应用程序代码连接到:9200的elasticsearch。

r = requests.get('http://localhost:9200/lr_index/book_info/_search', data=payload)

显示此错误

ConnectionError: HTTPConnectionPool(host='localhost', port=9200): Max retries exceeded with url: /lr_index/book_info/_search (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f5b13872250>: Failed to establish a new connection: [Errno 111] Connection refused',))

有人可以指导我吗?谢谢!

1 个答案:

答案 0 :(得分:6)

试试这个:

r = requests.get('http://elasticsearch:9200/lr_index/book_info/_search', data=payload)

您正在python容器中运行web代码吗? localhost指的是同一容器的loopback接口。

使用userdefined networks为同一网络中的容器实施DNS。这就是为什么你可以通过名字调用elasticsearch容器。