0

I'm new to docker and I'm trying to run redis-server and my springboot app both on a container. The Redis server is running fine on docker but when i try to connect my spring boot app to the redis server it get this error

jakarta.servlet.ServletException: Request processing failed: org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis


at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1022)


at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914)


at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:547)


at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)


at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:614)


at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)


at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)


at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)


at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)


at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)


at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)


at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)


at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)


at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:110)


at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)


at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)


at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:108)


at org.springframework.security.web.FilterChainProxy.lambda$doFilterInternal$3(FilterChainProxy.java:231)


at org.springframework.security.web.ObservationFilterChainDecorator$FilterObservation$SimpleFilterObservation.lambda$wrap$1(ObservationFilterChainDecorator.java:479)


at org.springframework.security.web.ObservationFilterChainDecorator$AroundFilterObservation$SimpleAroundFilterObservation.lambda$wrap$1(ObservationFilterChainDecorator.java:340)


at org.springframework.security.web.ObservationFilterChainDecorator.lambda$wrapSecured$0(ObservationFilterChainDecorator.java:82)


at org.springframework.security.web.ObservationFilterChainDecorator$VirtualFilterChain.doFilter(ObservationFilterChainDecorator.java:128)

My properties file is like this

spring.data.redis.host= ${REDIS_HOST:127.0.0.1}
spring.data.redis.port= 6379

My docker-compose file is like this

  redis-server:
    image: redis:latest
    restart: always
    container_name: redis-server
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - royalnet

  iam-service:
    build:
      context: ./iam-service
    container_name: iam-service
    ports:
      - "8082:8082"
    environment:
      IAM_INTERNAL: http://iam-service:8082
      STORAGE_INTERNAL: http://storage-service:8083
      REDIS_HOST: redis-server
      POSTGRES_URL: jdbc:postgresql://my_postgres:5432/iam_db
      postgres:
        condition: service_healthy
      keycloak:
        condition: service_started
      redis-server:
        condition: service_started
    networks:
      - royalnet

I have try change the redis.conf to

bind 0.0.0.0
protected-mode no
port 6379

and try using jedis but still the same error

@EnableCaching
@Configuration
public class RedisConfig {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private Integer port;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPort(port);
        return new JedisConnectionFactory(config);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        return redisTemplate;
    }
}
New contributor
RoyalPie is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • 1
    Your code references spring.redis.host, but your configuration uses spring.data.redis.host.
    – David Maze
    Commented Apr 25 at 14:24
  • thanks you for your answer and i have try both spring.redis.host and spring.data.redis.host but still the same error. Here its more of that error can you know the reason why? Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:6379 Caused by: java.net.ConnectException: Connection refused at java.base/sun.nio.ch.Net.pollConnect(Native Method) at java.base/sun.nio.ch.Net.pollConnectNow(Unknown Source) at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source) at
    – RoyalPie
    Commented Apr 26 at 2:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.