# \[Elasticsearch Docker\] \[Errno 111\] Connection refused when connecting with python script

**URL:** <https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678>\
**Category:** Elasticsearch\
**Tags:** docker\
**Created:** [August 4, 2020, 9:28am UTC](https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678 "2020-08-04T09:28:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stimmot](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stimmot](https://discuss.elastic.co/u/Stimmot)\
**Post date:** [August 4, 2020, 9:28am UTC](https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678/1 "2020-08-04T09:28:15Z")

</div>

Hey,

I got an issue with a dockerized elasticsearch cluster that I would like to connect with a simple python indexing script.

After executing docker-compose up it brings up the cluster, kibana as well, but when it tries to connect to ES via the python script it get the following error:

```auto
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f1c04e0c0d0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f1c04e0c0d0>: Failed to establish a new connection: [Errno 111] Connection refused)

```

I can reach ES with the browser, I can also index regularly if I execute the script in pycharm. So it has to do with docker, but I can't find the source of the problem.  
This is my dockerfile:

```auto
    version: '3.4'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
    container_name: es01
    environment:
      #- discovery.type=single-node needed?
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic

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

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

  kib01:
    image: docker.elastic.co/kibana/kibana:7.8.1
    container_name: kib01
    depends_on:
      - es01
      - es02
      - es03
    ports:
      - 5601:5601
    environment:
      ELASTICSEARCH_URL: http://es01:9200
      ELASTICSEARCH_HOSTS: http://es01:9200
    networks:
      - elastic

  web:
    build: .
    ports:
      - 8000:8000
    depends_on:
      - es01
      - es02
      - es03
    networks:
      - elastic

volumes:
  data01:
    driver: local
  data02:
    driver: local
  data03:
    driver: local

networks:
  elastic:
    driver: bridge

```

And the python script:

```auto
from elasticsearch import Elasticsearch, helpers
import sys, json, os
import requests

es = Elasticsearch(hosts=[{"host": 'localhost'}])

def load_json(directory):
    " Use a generator, no need to load all in memory"
    for filename in os.listdir(directory):
        if filename.endswith('.json'):
            filename = "JSON/" + filename
            with open(filename,'r') as open_file:
                yield json.load(open_file)

if __name__ == ' __main__':
    helpers.bulk(es, load_json("JSON"), index='urteile')

```

Anybody knows what could be the problem here?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 4, 2020, 9:57am UTC](https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678/2 "2020-08-04T09:57:36Z")

</div>

Can you just try

```auto
es = Elasticsearch()

```

and see if that works? Also is running curl/wget against localhost:9200 returning the expected results?

---

<div class="post-metadata">

**Author:** ![Stimmot](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stimmot](https://discuss.elastic.co/u/Stimmot)\
**Post date:** [August 4, 2020, 10:28am UTC](https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678/3 "2020-08-04T10:28:39Z")

</div>

Tried it, didn't work unfortunately - still the same error. Curl localhost:9200 works as it should.

---

<div class="post-metadata">

**Author:** ![Stimmot](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stimmot](https://discuss.elastic.co/u/Stimmot)\
**Post date:** [August 4, 2020, 10:35am UTC](https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678/4 "2020-08-04T10:35:36Z")

</div>

A curl command within the docker file also gives me a Connection refused by the way. This is the error I get:

```auto
mapping_1 | * TCP_NODELAY set
mapping_1 | * connect to 127.0.0.1 port 9200 failed: Connection refused
mapping_1 | * Trying ::1...
mapping_1 | * TCP_NODELAY set
mapping_1 | * Immediate connect fail for ::1: Address not available
mapping_1 | * Trying ::1...
mapping_1 | * TCP_NODELAY set
mapping_1 | * Immediate connect fail for ::1: Address not available
mapping_1 | * Failed to connect to localhost port 9200: Connection refused
mapping_1 | * Closing connection 0
mapping_1 | curl: (7) Failed to connect to localhost port 9200: Connection refused

```

---

<div class="post-metadata">

**Author:** ![Stimmot](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stimmot](https://discuss.elastic.co/u/Stimmot)\
**Post date:** [August 10, 2020, 7:27am UTC](https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678/5 "2020-08-10T07:27:27Z")

</div>

Any new ideas on this? Couldn't resolve the issue so far...

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 12, 2020, 8:13am UTC](https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678/6 "2020-08-12T08:13:51Z")

</div>

If you are running your python script also within docker, then using `localhost` won't work, but you need to specify the elasticsearch host.

---

<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:** [September 9, 2020, 8:13am UTC](https://discuss.elastic.co/t/elasticsearch-docker-errno-111-connection-refused-when-connecting-with-python-script/243678/7 "2020-09-09T08:13:58Z")

</div>

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