Docker – Elasticsearch – 无法build立新的连接: Connection refused',))

我正在使用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"] 

这是用于WebDockerfile https://github.com/mysticmode/LibreRead/blob/master/Dockerfile

这两个服务正在运行,我可以用我的:8000和:9200在浏览器中检查

但我无法连接到弹性search:9200从我的Python应用程序代码。

 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',)) 

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

尝试这个:

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

你在web容器中运行python代码吧? localhost引用同一个容器的回送接口。

使用在同一networking中为容器实现DNS用户定义的networking。 这就是为什么你可以通过名称调用elasticsearch容器。