# Elasticsearch python connection error. Failed to establish a new connection

**URL:** <https://discuss.elastic.co/t/elasticsearch-python-connection-error-failed-to-establish-a-new-connection/229640>\
**Category:** Elasticsearch\
**Tags:** docker\
**Created:** [April 24, 2020, 11:54am UTC](https://discuss.elastic.co/t/elasticsearch-python-connection-error-failed-to-establish-a-new-connection/229640 "2020-04-24T11:54:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shekhar\_Koirala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shekhar_koirala/32/67002_2.png) [@Shekhar\_Koirala](https://discuss.elastic.co/u/Shekhar_Koirala)\
**Post date:** [April 24, 2020, 11:54am UTC](https://discuss.elastic.co/t/elasticsearch-python-connection-error-failed-to-establish-a-new-connection/229640/1 "2020-04-24T11:54:43Z")

</div>

Here is my docker-compose file for elasticsearch:

```auto
elasticsearch:
container_name: avs-elasticsearch
image: "elasticsearch:6.8.3"
command: elasticsearch
env_file:
  - ./.envs/.env.prod
ports:
  - 9200:9200
  - 9300:9300
volumes:
    - "./esdata:/usr/share/elasticsearch/data"
    - ./elkstack/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
    - ./elkstack/elastic-certificates.p12:/usr/share/elasticsearch/config/elastic- 
certificates.p12
    restart: unless-stopped
    networks:
       - 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:

```auto
File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/ __init__.py", line 805, in 
search
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:

```auto
# DB Configuration
db_hosts = [{'host': os.getenv('ES_HOST', '0.0.0.0'), '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:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 22, 2020, 11:54am UTC](https://discuss.elastic.co/t/elasticsearch-python-connection-error-failed-to-establish-a-new-connection/229640/2 "2020-05-22T11:54:43Z")

</div>

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