Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - Elasticsearch - Failed to establish a new connection: [Errno 111] Connection refused',))

I'm using docker-compose to run python flask app and elasticsearch services in a Linode server.

Here is my 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

Here is the 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"]

Here is the Dockerfile for web https://github.com/mysticmode/LibreRead/blob/master/Dockerfile

Both the services are running, which I can check in the browser with my :8000 and :9200

But I couldn't connect to elasticsearch at :9200 from my python application code.

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

It is showing this error

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

Could someone guide me? Thanks!

like image 827
rnk Avatar asked Dec 30 '16 06:12

rnk


1 Answers

Try this:

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

You are running the python code in web container right? localhost refers to the same container's loopback interface.

Using userdefined networks that implement DNS for containers in the same network. That's why you can call elasticsearch container by its name.

like image 140
Farhad Farahi Avatar answered Nov 16 '22 10:11

Farhad Farahi