Connection Refused java.net using two docker containers

WhiteW :

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.

GreyFairer :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=194327&siteId=1