Can't connect to ElasticSearch from python API in Docker-Compose setup

Here is how I create the ES container in Docker-Compose:

db:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.15.1
    restart: always
    ports:
      - 9200:9200
      - 9300:9300
    ulimits:
      memlock:
        soft: -1
        hard: -1
    environment:
      discovery.type: single-node
      network.host: db

Here is how I'm attempting to connect from the Python API:

from elasticsearch import Elasticsearch
es = Elasticsearch(
    {'host': 'db', 'port': 9200, 'use_ssl': False},
)

localhost:9200 works fine when the container is running, but the connection does not work. Whenever I dont set the network.host ENV the error I get is

Errno -2: Name or Service not known

And when I do set the ENV the error is

Errno -5: No address associated with this host name

Any help or advice would be very, very appreciated. Thank you!

Welcome to our community! :smiley:

Try setting network.host to 0.0.0.0.

Thanks for the warm welcome warkolm! I did try that previously and unfortunately it gave me the same error as it does with the hostname set to db.

Where did you set that and what was the error?

I set it in the same place I set "network.host: db" and the error was
"No address associated with this host name"

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