Connection Refused connecting from docker to Elasticsearch docker container
Asked Answered
G

2

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.

Garcon answered 11/3, 2020 at 11:49 Comment(0)
T
13

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
Trakas answered 11/3, 2020 at 12:10 Comment(1)
Save a big day, I tried to upgrade from 6.x to 7.x and I didn't understand the error...Chickie
D
1

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
Dangelo answered 1/9, 2023 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.