Not able to connect to Elasticsearch in Docker Not able to connect to Elasticsearch in Docker elasticsearch elasticsearch

Not able to connect to Elasticsearch in Docker


I fixed it by using the host as:

host:"host.docker.internal"

Code change,

es = Elasticsearch(hosts=[{"host": "host.docker.internal", "port": 9200}], connection_class=RequestsHttpConnection, max_retries=30,                       retry_on_timeout=True, request_timeout=30)


You can try to set the ELASTICSEARCH_NODES variable in your application environment section as and then consume the variable in your python code as http://ELASTICSEARCH_NODES:

version: '2.2'services:  elastic:    image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.7.0    container_name: elastic    environment:      - discovery.type=single-node      - bootstrap.memory_lock=true      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"    ulimits:      memlock:        soft: -1        hard: -1    volumes:      - data01:/usr/share/elasticsearch/data    ports:      - 9300:9300      - 9200:9200    networks:      - elastic  myimage:    image: myimage:myversion    depends_on:      - elastic    environment:      - ELASTICSEARCH_NODES=http://elastic:9200    ports:      - 5000:5000    expose:      - 5000    networks:      - elasticvolumes:  data01:    driver: localnetworks:  elastic:    driver: bridge