13

I am trying to access Elasticsearch database inside a container from a Java application which is also inside a container.

Both of them are in the following docker-compose.yml:

version: "3.7"
services:
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.1
    ports:
      - "9200:9200"
      - "9300:9300"
    networks:
      - elastic

  java-app:
    image: neileen/dockerhub_app:java-latest
    ports:
      - "8080:8080"
    depends_on:
      - es
    networks:
      - elastic

networks:
  elastic:
    driver: bridge

As you can see, I use a bridge network to make the containers visible and accessible to one another.

In my Java Application I use RestHighLevelClient:

RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("es", 9200, "http")));

I also tried using "localhost" and "0.0.0.0" as the hostname instead of "es" with no result.

The exception that I keep getting is:

java-app_1     | The cluster is unhealthy: Connection refused
java-app_1     | java.net.ConnectException: Connection refused
java-app_1     |        at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:804)
java-app_1     |        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:225)
java-app_1     |        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:212)

I am aware that this is a problem related to the port 9200 not being visible inside the java-app container but I do not know where the problem is as I already provided a custom network and used the container name as the hostname.

Note

ES is accessible through "http://localhost:9200"

Thank you in advance.

2 Answers 2

14

Elasticsearch does some bootstrap checks on startup. If you want to start it as a single node in Docker, you need to disable these, or it will not open the TCP/IP port.

This can be done by specifying an environment parameter: discovery.type=single-node.

services:
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.1
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      - discovery.type=single-node
Sign up to request clarification or add additional context in comments.

1 Comment

Save a big day, I tried to upgrade from 6.x to 7.x and I didn't understand the error...
2

On new version you need to disable the xpack.security.enabled=false

services:
   elasticsearch:
   image: docker.elastic.co/elasticsearch/elasticsearch:8.4.3
   container_name: elasticsearch
   environment:
     - cluster.name=es
     - discovery.type=single-node
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
     - xpack.security.enabled=false
  ports:
    - 9200:9200
    - 9300:9300
  volumes:
     - ./esdata-8:/var/lib/elasticsearch/data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.