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"