elasticsearch-py: New ConnectionError [Errno 111] Connection Refused using Docker

I have elasticsearch running in one docker container and elasticsearch-dsl installed in another docker on the same network.

I am trying to establish a connection between the two but am getting an error. Elasticsearch is up and running.

I entered the docker with elasticsearch-dsl installed.

from elasticsearch import Elasticsearch connections.create_connections(hosts=['localhost:9200']) client = Elasticsearch() client.ping()

I get an error here, copied below. Is there a configuration step I’m missing?

Thanks

HEAD http://localhost:9200/ [status:N/A request:0.000s] Traceback (most recent call last): File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 141, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File “/usr/local/lib/python3.5/site-packages/urllib3/util/connection.py”, line 83, in create_connection raise err File “/usr/local/lib/python3.5/site-packages/urllib3/util/connection.py”, line 73, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/usr/local/lib/python3.5/site-packages/elasticsearch/connection/http_urllib3.py”, line 149, in perform_request response = self.pool.urlopen(method, url, body, retries=False, headers=request_headers, **kw) File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 639, in urlopen _stacktrace=sys.exc_info()[2]) File “/usr/local/lib/python3.5/site-packages/urllib3/util/retry.py”, line 333, in increment raise six.reraise(type(error), error, _stacktrace) File “/usr/local/lib/python3.5/site-packages/urllib3/packages/six.py”, line 686, in reraise raise value File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 601, in urlopen chunked=chunked) File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 357, in _make_request conn.request(method, url, **httplib_request_kw) File “/usr/local/lib/python3.5/http/client.py”, line 1107, in request self._send_request(method, url, body, headers) File “/usr/local/lib/python3.5/http/client.py”, line 1152, in _send_request self.endheaders(body) File “/usr/local/lib/python3.5/http/client.py”, line 1103, in endheaders self._send_output(message_body) File “/usr/local/lib/python3.5/http/client.py”, line 934, in _send_output self.send(msg) File “/usr/local/lib/python3.5/http/client.py”, line 877, in send self.connect() File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 166, in connect conn = self._new_conn() File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 150, in _new_conn self, “Failed to establish a new connection: %s” % e) urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f08652ead68>: Failed to establish a new connection: [Errno 111] Connection refused HEAD http://localhost:9200/ [status:N/A request:0.000s] Traceback (most recent call last): File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 141, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File “/usr/local/lib/python3.5/site-packages/urllib3/util/connection.py”, line 83, in create_connection raise err File “/usr/local/lib/python3.5/site-packages/urllib3/util/connection.py”, line 73, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/usr/local/lib/python3.5/site-packages/elasticsearch/connection/http_urllib3.py”, line 149, in perform_request response = self.pool.urlopen(method, url, body, retries=False, headers=request_headers, **kw) File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 639, in urlopen _stacktrace=sys.exc_info()[2]) File “/usr/local/lib/python3.5/site-packages/urllib3/util/retry.py”, line 333, in increment raise six.reraise(type(error), error, _stacktrace) File “/usr/local/lib/python3.5/site-packages/urllib3/packages/six.py”, line 686, in reraise raise value File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 601, in urlopen chunked=chunked) File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 357, in _make_request conn.request(method, url, **httplib_request_kw) File “/usr/local/lib/python3.5/http/client.py”, line 1107, in request self._send_request(method, url, body, headers) File “/usr/local/lib/python3.5/http/client.py”, line 1152, in _send_request self.endheaders(body) File “/usr/local/lib/python3.5/http/client.py”, line 1103, in endheaders self._send_output(message_body) File “/usr/local/lib/python3.5/http/client.py”, line 934, in _send_output self.send(msg) File “/usr/local/lib/python3.5/http/client.py”, line 877, in send self.connect() File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 166, in connect conn = self._new_conn() File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 150, in _new_conn self, “Failed to establish a new connection: %s” % e) urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f086504f0f0>: Failed to establish a new connection: [Errno 111] Connection refused HEAD http://localhost:9200/ [status:N/A request:0.000s] Traceback (most recent call last): File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 141, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File “/usr/local/lib/python3.5/site-packages/urllib3/util/connection.py”, line 83, in create_connection raise err File “/usr/local/lib/python3.5/site-packages/urllib3/util/connection.py”, line 73, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/usr/local/lib/python3.5/site-packages/elasticsearch/connection/http_urllib3.py”, line 149, in perform_request response = self.pool.urlopen(method, url, body, retries=False, headers=request_headers, **kw) File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 639, in urlopen _stacktrace=sys.exc_info()[2]) File “/usr/local/lib/python3.5/site-packages/urllib3/util/retry.py”, line 333, in increment raise six.reraise(type(error), error, _stacktrace) File “/usr/local/lib/python3.5/site-packages/urllib3/packages/six.py”, line 686, in reraise raise value File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 601, in urlopen chunked=chunked) File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 357, in _make_request conn.request(method, url, **httplib_request_kw) File “/usr/local/lib/python3.5/http/client.py”, line 1107, in request self._send_request(method, url, body, headers) File “/usr/local/lib/python3.5/http/client.py”, line 1152, in _send_request self.endheaders(body) File “/usr/local/lib/python3.5/http/client.py”, line 1103, in endheaders self._send_output(message_body) File “/usr/local/lib/python3.5/http/client.py”, line 934, in _send_output self.send(msg) File “/usr/local/lib/python3.5/http/client.py”, line 877, in send self.connect() File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 166, in connect conn = self._new_conn() File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 150, in _new_conn self, “Failed to establish a new connection: %s” % e) urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f086504f1d0>: Failed to establish a new connection: [Errno 111] Connection refused HEAD http://localhost:9200/ [status:N/A request:0.000s] Traceback (most recent call last): File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 141, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File “/usr/local/lib/python3.5/site-packages/urllib3/util/connection.py”, line 83, in create_connection raise err File “/usr/local/lib/python3.5/site-packages/urllib3/util/connection.py”, line 73, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/usr/local/lib/python3.5/site-packages/elasticsearch/connection/http_urllib3.py”, line 149, in perform_request response = self.pool.urlopen(method, url, body, retries=False, headers=request_headers, **kw) File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 639, in urlopen _stacktrace=sys.exc_info()[2]) File “/usr/local/lib/python3.5/site-packages/urllib3/util/retry.py”, line 333, in increment raise six.reraise(type(error), error, _stacktrace) File “/usr/local/lib/python3.5/site-packages/urllib3/packages/six.py”, line 686, in reraise raise value File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 601, in urlopen chunked=chunked) File “/usr/local/lib/python3.5/site-packages/urllib3/connectionpool.py”, line 357, in _make_request conn.request(method, url, **httplib_request_kw) File “/usr/local/lib/python3.5/http/client.py”, line 1107, in request self._send_request(method, url, body, headers) File “/usr/local/lib/python3.5/http/client.py”, line 1152, in _send_request self.endheaders(body) File “/usr/local/lib/python3.5/http/client.py”, line 1103, in endheaders self._send_output(message_body) File “/usr/local/lib/python3.5/http/client.py”, line 934, in _send_output self.send(msg) File “/usr/local/lib/python3.5/http/client.py”, line 877, in send self.connect() File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 166, in connect conn = self._new_conn() File “/usr/local/lib/python3.5/site-packages/urllib3/connection.py”, line 150, in _new_conn self, “Failed to establish a new connection: %s” % e) urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f086504f2b0>: Failed to establish a new connection: [Errno 111] Connection refused False

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

You have 2 containers on the same network.

Are you using Docker-compose?

Localhost is probably the wrong hostname to use.

When Docker networks containers you can communicate between containers by using the name of the containers.

So say you named your elasticsearch container elasticsearch you would then want to establish a connection to ES using the name elasticsearch instead of localhost.

By using localhost the container running -dsl-py will attempt to connect to WS running within the same container. But it will fail cause ES is available in a different container.

The reason I asked about compose is because it makes it easy to name your services and the name of each service also doubles as a DNS name to reach said service.

@davidefiocco @mihaitodor

ahh yes that would make sense.

Another option in your indexer script is to put the sleep in a while loop:

import time
connected = False

while not connected:
    try:
        es.info()
        connected = True
    except ConnectionError:
        print("Elasticsearch not available yet, trying again in 2s...)
        time.sleep(2)

edit: or even better: https://github.com/elastic/elasticsearch-py/blob/master/elasticsearch/helpers/test.py#L22-L28

@ngocAnh1999 Have a look at this question on Stackoverflow and see if you can replicate the setup in the question. Then see this answer below on how to make sure elasticsearch is up and running before trying to connect to it. Hope that works for you.

@fxdgear: I ran into a similar problem as @laubnyc, and asked a question on https://stackoverflow.com/questions/48711455/create-dockerized-elasticsearch-index-using-a-python-script-running-in-docker . If I do your exact curl edit to my docker-compose yml I get the response fine from the elasticsearch service, but still have the ConnectionRefusedError: [Errno 111] Connection refused with my python script.

@fxdgear @mihaitodor Thanks both! Mihai nailed it, here’s code that connects fine to elasticsearch without the [Errno 111]: https://github.com/davidefiocco/dockerized-elasticsearch-indexer

Also, there is a synchronisation bug in there, because the indexer.py script doesn’t make sure that elasticsearch really is up before it tries to query it.