Home > Enterprise >  Java cannot access Elasticsearch running in Docker on Windows 11
Java cannot access Elasticsearch running in Docker on Windows 11

Time:01-05

EDIT: it works if I disable transport client sniffing.

Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", false)
                .build();

Any ideas how to fix it?


First of all, everything works fine in Ubuntu 20.04, but when I try to run the same project using Docker Desktop on Windows 10, 11, and macOS, it fails.

When the Spring service starts it tries to find the Elasticsearch node and it fails, the exception is the following:

2022-12-27 16:20:40.459  INFO 23752 --- [           main] o.e.p.PluginsService                     : no modules loaded
2022-12-27 16:20:40.459  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
2022-12-27 16:20:40.460  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2022-12-27 16:20:40.460  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2022-12-27 16:20:40.460  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
2022-12-27 16:20:40.460  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2022-12-27 16:21:02.679  INFO 23752 --- [           main] c.u.OnStartup                            : -----> APPLICATION INITIALIZATION <-----
2022-12-27 16:21:02.679  INFO 23752 --- [           main] c.u.OnStartup                            : -----> SETTING TIMEZONE
2022-12-27 16:21:02.679  INFO 23752 --- [           main] c.u.OnStartup                            : -----> CREATE INDEXES AND MAPPINGS NOW
2022-12-27 16:21:02.683 ERROR 23752 --- [           main] c.u.OnStartup                            : Elasticsearch unavailable, skipping creating of indexes

org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{CA1SJmWMQfe1sRIDTY_Lpw}{localhost}{127.0.0.1:9300}]
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:371) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1247) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46) ~[elasticsearch-6.2.4.jar:6.2.4]
    at com.mymy.service.services.impl.IndexAndTypeManagementServiceImpl.indexExists(IndexAndTypeManagementServiceImpl.java:65) ~[classes/:?]
    at com.mymy.service.services.impl.IndexAndTypeManagementServiceImpl.createIndex(IndexAndTypeManagementServiceImpl.java:95) ~[classes/:?]
    at com.mymy.OnStartup.createIndexes(OnStartup.java:87) ~[classes/:?]
    at com.mymy.OnStartup.init(OnStartup.java:55) ~[classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_352]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_352]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_352]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_352]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) [spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) [spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) [spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) [spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at

The docker-compose file looks like:

version: '3.5'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
    container_name: elasticsearch
    ports: 
      - 9200:9200
      - 9300:9300
    environment:
      - discovery.type=single-node
      - cluster.name=mycluster
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - xpack.security.enabled=false
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - elastic_data:/usr/share/elasticsearch/data

  kibana:
    image: docker.elastic.co/kibana/kibana:6.2.4
    container_name: kibana
    environment:
      SERVER_NAME: localhost
      ELASTICSEARCH_URL: http://elasticsearch:9200/
    ports:
      - 5601:5601
    volumes: 
      - kibana_data:/var/log
    depends_on: 
      - elasticsearch
volumes:
  elastic_data:
  kibana_data:

Java version is 1.8. Any suggestion on how to fix it?

Elasticsearch is accessible via Elasticsearch tools in the browser.

CodePudding user response:

The answer is to add the following environment variables to docker-compose.yaml of the elasticsearch service:

environment:
  - discovery.type=single-node
  - network.host=_local_,_site_
  - network.publish_host=_local_

or to start the elasticsearch with the command:

command: elasticsearch -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_

Solution thanks to https://github.com/olivere/elastic-with-docker/

  • Related