Elasticsearch python connection error. Failed to establish a new connection

Here is my docker-compose file for elasticsearch:

container_name: avs-elasticsearch
image: "elasticsearch:6.8.3"
command: elasticsearch
  - ./.envs/.env.prod
  - 9200:9200
  - 9300:9300
    - "./esdata:/usr/share/elasticsearch/data"
    - ./elkstack/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
    - ./elkstack/elastic-certificates.p12:/usr/share/elasticsearch/config/elastic- 
    restart: unless-stopped
       - avs-net

Elasticsearch is working fine. I can browser: localhost:9200 and it works. Also i can curl indices.
But python client is not communicating.
Here is my error:

File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/__init__.py", line 805, in 
avs-celery       |     "GET", _make_path(index, "_search"), params=params, body=body
avs-celery       |   File "/usr/local/lib/python3.7/site- 
packages/elasticsearch/transport.py", line 350, in perform_request
avs-celery       |     timeout=timeout,
avs-celery       |   File "/usr/local/lib/python3.7/site- 
packages/elasticsearch/connection/http_urllib3.py", line 250, in perform_request
avs-celery       |     raise ConnectionError("N/A", str(e), e)
avs-celery       | elasticsearch.exceptions.ConnectionError: 
ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f4c4e73e9e8>: 
Failed to establish a new connection: [Errno 111] Connection refused) caused by: 
NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f4c4e73e9e8>: 
Failed to establish a new connection: [Errno 111] Connection refused)

Here is my connection config file:

# DB Configuration
db_hosts = [{'host': os.getenv('ES_HOST', ''), 'port': os.getenv('ES_PORT', '9200')}]
# ElasticSearch authetication details
http_auth = (os.getenv('ES_USER', 'elastic'), os.getenv('ES_PASSWORD', 

And here i am using it in the python script:

class TestClass:

    def __init__(self, uri=None, index='index_name'):
        self.es = DBConnect()
        self.index = index

I have a DBConnect class where i am using the db_hosts and http_auth like this:

class DBConnect(Elasticsearch):

    def __init__(self, **kwargs):
        super().__init__(**kwargs, hosts=db_hosts, http_auth=http_auth)

I don't understand why there is the connection problem. Anything i missed here?
Thank you!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.