Search code examples
pythondockerelasticsearchjupyter-notebook

Connection error Jupyter and Elasticsearch (Docker)


I am trying to make a connection from Jupyter Notebook to Elasticsearch both in Docker containers but connected to the same network (bridge).

Here is my code:

elastic_client = Elasticsearch(hosts=["http://localhost:9200/"], http_auth=('generator', 'generator'))
elastic_index='data_generator_nvdi_geoloc'

df_out = pandas.read_csv(local_destination_path)

I get this error:

ConnectionError: Connection error caused by: ConnectionError(Connection error caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f7ada675710>: Failed to establish a new connection: [Errno 111] Connection refused))

I think it may be a problem of the containers, but both are connected to the same network and I don't know how to solve it.


Solution

  • Issue in your case is using localhost:9200 as connection string. Because ES is not "localhost" inside your Jupiter container. Eeach container gets its own localhost reference. You need to adjust the connection string to your Docker's DNS record so service/container name you set up inside your docker-compose.yml file. Based on your comment you should use elasticsearch:9200 for your connection string.