# Node.js doesn't establish connection to Elasticsearch from docker container

**URL:** https://discuss.elastic.co/t/node-js-doesnt-establish-connection-to-elasticsearch-from-docker-container/353298
**Category:** Elastic Search
**Tags:** docker, elastic-app-search
**Created:** [February 14, 2024, 4:26pm UTC](https://discuss.elastic.co/t/node-js-doesnt-establish-connection-to-elasticsearch-from-docker-container/353298 "2024-02-14T16:26:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Francis\_Lagares](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Francis\_Lagares](https://discuss.elastic.co/u/Francis_Lagares)
#### Post date: [February 14, 2024, 4:26pm UTC](https://discuss.elastic.co/t/node-js-doesnt-establish-connection-to-elasticsearch-from-docker-container/353298/1 "2024-02-14T16:26:21Z")

</div>

I'm trying to connect my Node.js application to Elasticsearch with docker containers but without success.  
I've tried many variants but none of them worked out for me.

Elasticsearch container is up and running and I can access to it in the browser with `localhost:9200` but it doesn't connect through the container name `elasticsearch:9200`.

I made a check with `curl elasticsearch:9200` and prints `curl: (6) Could not resolve host: elasticsearch`. Also tried with `curl http://host.docker.internal:9200` and the same error.

I can open Elastic Dashboard at `localhost:5601/app/observability/overview` but is not showing metrics panel obiously because of the Connection Error.

I've also checked console.log(`${config.ELASTIC_SEARCH_URL}`) and prints `http://elasticsearch:9200` and also tried with `node: ${config.ELASTIC_SEARCH_URL} || http://localhost:9200` as a fallback but neither of them works.

I tried to add notification service to elastic network but it immediately fails and also removed elastic network and tried to use the Docker network that adds by default but still the same error so I don't know how to get around this.

Any help will be appreciated endlessly !

`curl localhost:9200`

```
{
  "name": "3e9d7110bb8a",
  "cluster_name": "docker-cluster",
  "cluster_uuid": "sAZvoS3YRSiewKxFqC1RhA",
  "version": {
    "number": "8.11.4",
    "build_flavor": "default",
    "build_type": "docker",
    "build_hash": "da06c53fd49b7e676ccf8a32d6655c5155c16d81",
    "build_date": "2024-01-08T10:05:08.438562403Z",
    "build_snapshot": false,
    "lucene_version": "9.8.0",
    "minimum_wire_compatibility_version": "7.17.0",
    "minimum_index_compatibility_version": "7.0.0"
  },
  "tagline": "You Know, for Search"
}

```

Elasticsearch docker-compose.yaml file:

```
version: '3.9'

services:
  elasticsearch:
    container_name: elasticsearch
    image: docker.elastic.co/elasticsearch/elasticsearch:8.11.4
    restart: always
    environment:
      ES_JAVA_OPTS: -Xmx2g -Xms2g
      discovery.type: 'single-node'
      xpack.security.enabled: 'false'
    ports:
      - 9200:9200
      - 9300:9300
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    deploy:
      resources:
        limits:
          cpus: '2.0'
        reservations:
          cpus: '1.0'
    networks:
      - elastic

```

Node.js notification service:

```
version: '3.9'

services:
  notification:
    container_name: notification
    build:
      context: .
      dockerfile: Dockerfile.dev
    restart: always
    ports:
      - 4001:4001
    environment:
      - NODE_ENV=development
      - ELASTIC_SEARCH_URL=http://elasticsearch:9200
    depends_on:
      - elasticsearch

```

Root docker-compose.yaml

```
version: '3.9'

services:    
  elasticsearch:
    extends:
      service: elasticsearch
      file: infra/docker/docker-compose-elasticsearch.yaml

  kibana:
    extends:
      service: kibana
      file: infra/docker/docker-compose-kibana.yaml

  notification:
    extends:
      service: notification
      file: api/notification/docker-compose.yaml
    depends_on:
      - elasticsearch

networks:
  elastic:
    name: elastic

```

The client code:

```
import { Client } from '@elastic/elasticsearch';
import { ClusterHealthResponse } from '@elastic/elasticsearch/lib/api/types';
import { config } from '@notification/config';
import { Logger } from 'winston';

const logger: Logger = winstonLogger(
  `${config.ELASTIC_SEARCH_URL}`,
  'notificationElasticSearchServer',
  'debug',
);

const elasticSearchClient = new Client({
  node: `${config.ELASTIC_SEARCH_URL}`,
});

export const checkConnection = async (): Promise<void> => {
  let isConnected = false;

  while (!isConnected) {
    try {
      const health: ClusterHealthResponse =
        await elasticSearchClient.cluster.health({});

      logger.info(
        `NotificationService ElasticSearch health status - ${health.status}`,
      );
      isConnected = true;
    } catch (error) {
      logger.error('Connection to ElasticSearch failed. Retrying...');
      logger.error(
        'error',
        'NotificationService checkConnection() method:',
        error,
      );
    }
  }
};

```

When I run docker-compose up it throws:

```
notification | ERROR:
notification | message: "Connection to ElasticSearch failed. Retrying..."
notification | service: "notificationElasticSearchServer"
notification | ERROR:
notification | message: "error"
notification | service: "notificationElasticSearchServer"

```

---

<div class="post-metadata">

### Author: ![Alex\_Salgado-Elastic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_salgado-elastic/32/103081_2.png) [@Alex\_Salgado-Elastic](https://discuss.elastic.co/u/Alex_Salgado-Elastic)
#### Post date: [February 14, 2024, 4:48pm UTC](https://discuss.elastic.co/t/node-js-doesnt-establish-connection-to-elasticsearch-from-docker-container/353298/2 "2024-02-14T16:48:40Z")

</div>

Make sure to define the "elastic" network in the same docker-compose.yaml file.

```auto
networks:
  elastic:
    driver: bridge

```

---

<div class="post-metadata">

### Author: ![Francis\_Lagares](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Francis\_Lagares](https://discuss.elastic.co/u/Francis_Lagares)
#### Post date: [February 14, 2024, 7:45pm UTC](https://discuss.elastic.co/t/node-js-doesnt-establish-connection-to-elasticsearch-from-docker-container/353298/3 "2024-02-14T19:45:32Z")

</div>

Still get the same error.

---

<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: [March 13, 2024, 7:46pm UTC](https://discuss.elastic.co/t/node-js-doesnt-establish-connection-to-elasticsearch-from-docker-container/353298/4 "2024-03-13T19:46:25Z")

</div>

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