Pascal Stockert
Pascal Stockert

Reputation: 366

Elasticsearch refusing connection through Python client

While testing about with ES I'm struggling to get it to work with it's Python Client. I've followed the quick-start guide and set up local Docker containers running both ES and Kibana. To test the installation I used Postman to send some requests over to http://localhost:9200/ which responded with proper data.

The Python Client however fails to connect - installed it via Pip, imported it and set it up like so:

from elasticsearch import Elasticsearch

es = Elasticsearch(
    ['localhost'],
    scheme="http",
    port=9200,
)

if not es.ping():
    raise BaseException("Connection failed")

The Error being thrown:

Failed to establish a new connection: [Errno 111] Connection refused

Here's an excerpt from my docker-compose:

es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.13.3
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic

Note that my python-container does not share the network with ES - does that play a notable role here?

Upvotes: 1

Views: 1973

Answers (1)

Pascal Stockert
Pascal Stockert

Reputation: 366

Turns out it was related to the networks.

I've fixed this issue by including ES and Kibana in the same network as my Python application and changing the initialization to:

from elasticsearch import Elasticsearch

es = Elasticsearch(
    ['es01'],
    scheme="http",
    port=9200,
)
if not es.ping():
    print("Connection failed")
else:
    print("Connection successful")

es01 references the name of the docker container running Elasticsearch.

Upvotes: 2

Related Questions