Search code examples
spring-bootdockerelasticsearchdocker-compose

Elastic Search Connection Refused


I have this Dockerfile for my Spring Boot backend:

FROM eclipse-temurin:17-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ./target/elasticSpring-0.0.1-SNAPSHOT.jar elasticSpring.jar
ENTRYPOINT [ "java","-jar","/elasticSpring.jar" ]

And this is my application.properties

spring.data.elasticsearch.cluster-name=http://localhost:9200

And this is my docker-compose.yml file:

version: '1'
services:
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.12
    container_name: es
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      - bootstrap.memory_lock=true
      - xpack.security.enabled=false
      - discovery.type=single-node
    networks:
      - mynet

  springboot-app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      - es
    environment:
      - SPRING_DATA_ELASTICSEARCH_CLUSTER_NODES=es:9200
    networks:
      - mynet

networks:
  mynet:
    driver: bridge

As far as I know, docker-compose environment overrides application.properties so I don't think it's the problem. But I'm getting "Caused by: org.springframework.dao.DataAccessResourceFailureException: Connection refused" and connection errors.Please help.


Solution

  • After browsing all of the internet I finally find the solution, turns out default value for "spring.elasticsearch.uris" is localhost:9200 and my container was trying to connect to nonexistent host all along. I added "SPRING_ELASTICSEARCH_URIS=es:9200" to my backend environment in docker compose and now it's working perfectly fine.