javaspring-bootdockerredis

Spring Boot Application in Docker Unable to Connect to Redis Container (Connection Refused)


I’m encountering a RedisConnectionFailureException error when trying to connect to Redis from my Spring Boot application running in Docker. Here’s the error message:

ERROR 1 --- [ElasticSearch_books] [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis] with root cause
java.net.ConnectException: Connection refused

Setup and Observations Environment:

  1. Spring Boot version: 3.3.5
  2. Redis: running as a Docker container Application: also containerized in Docker

What works:

When running the Spring Boot application locally (outside of Docker) and Redis in Docker, the application connects to Redis without any issues.

What doesn’t work:

When both the Spring Boot application and Redis are running in Docker containers, I receive a Connection refused error.

Docker and Application Configurations application-docker.properties:

spring.cache.type=redis
spring.redis.host=redis
spring.redis.port=6379

Docker Compose Configuration:

Both app and redis services are on the same Docker network (book-network).

Redis health checks and depends_on condition are added to ensure Redis is fully ready before the application starts.

Additional Details I’ve verified the following:

Network: Both app and redis are on the same network in Docker Compose (book-network), and they can ping each other.

Environment Variable: SPRING_PROFILES_ACTIVE=docker is set for the app container to ensure it uses application-docker.properties.

docker-compose.yml

networks:
  book-network:
    driver: bridge

services:
  app:
    image: elasticsearchbook:latest  # Reference to your application image
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"  # Maps the app's port to the host
    depends_on:
      elasticsearch:
        condition: service_healthy
      redis:
        condition: service_healthy
      prometheus:
        condition: service_healthy
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - SPRING_DATA_ELASTICSEARCH_HOST=elasticsearch
      - SPRING_DATA_ELASTICSEARCH_PORT=9200
      - SPRING_REDIS_HOST=redis
      - SPRING_REDIS_PORT=6379
    networks:
      - book-network
    restart: always
  redis:
    image: redis:alpine
    ports:
      - "6379:6379"  # Redis default port
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 10
    networks:
      - book-network
    restart: always

Connectivity Test: I can successfully ping Redis from the app container and even use redis-cli to connect from inside the app container. Despite these checks, the Spring Boot application still fails to connect to Redis when both are running in Docker.

Question

What might be causing the connection issue between my Spring Boot application and Redis when both are containerized? Is there something specific to Spring Boot 3.3.5 or Docker networking that I may have overlooked?


Solution

  • The spring.redis.host and spring.redis.port properties are from Spring Boot 2.x. They have been renamed in Spring Boot 3 to spring.data.redis.host and spring.data.redis.port.

    This was done to have 1 namespace for the spring.data.* related configuration properties. Before this there was a mixture between spring.data.* and things like spring.redis.*. Hence the unification.

    This does pose a challenge if you are following some older tutorial for Spring Boot 2 and use Spring Boot 3.