title: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException:
body: <p>I'm new to docker and I'm trying to run redis-server and my springboot app both on a container.</p>
  <p>I was able to hit redis(present in a docker container) when I start the springboot app locally just fine, but when i put this springboot app also in the docker container then I'm unable to connect to redis and getting</p>
  <pre><code> Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 0.0.0.0:6397] with root cause
  urlshortner |
  urlshortner | java.net.ConnectException: Connection refused
  urlshortner |   at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:na]
  urlshortner |   at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:779) ~[na:na]
  urlshortner |   at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) ~[netty-transport-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:702) ~[netty-transport-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) ~[netty-transport-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) ~[netty-transport-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.45.Final.jar!/:4.1.45.Final]
  urlshortner |   at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
  </code></pre>
  <p>I have tried following:</p>
  <ol>
  <li>used docker-compose to get them onto same network</li>
  </ol>
  <p><em>my docker-compose</em></p>
  <pre><code>version: '3'
  services:
    app:
      container_name: urlshortner
      image: docker-urlshortner:v1
      build: .
      links:
        - redis
      ports:
        - &quot;10095:10095&quot;
      volumes:
        - ~/docker/redis:/urlshortner/logs
    redis:
      container_name: myredis
      image: redis:v1
      build: ./redis
      hostname: localhost
      ports:
        - &quot;6379:6379&quot;
  </code></pre>
  <p><em>dockerfile to start springboot app</em></p>
  <pre><code>FROM adoptopenjdk/openjdk11
  VOLUME /urlshortner
  ARG JAR_FILE=target/Urlshortning-0.0.1-SNAPSHOT.jar
  ADD ${JAR_FILE} urlshortning.jar
  EXPOSE 10095
  ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;/urlshortning.jar&quot;]
  </code></pre>
  <p><em>dockerfile for running redis</em></p>
  <pre><code>FROM redis
  COPY redis.conf /redis/redis.conf
  CMD [ &quot;redis-server&quot;, &quot;/redis/redis.conf&quot; ]
  </code></pre>
  <ol start="2">
  <li>commented out bind 127.0.0.1 and added bind 0.0.0.0 in redis.conf</li>
  </ol>
  <p><strong>but still getting same error</strong></p>
  <p><em>my redis config in java app</em></p>
  <pre><code>@Configuration
  public class RedisConfig {
  
      private final String url;
      private final int port;
      private final String password;
  
      @Autowired
      private ObjectMapper objectMapper;
  
      public RedisConfig(@Value(&quot;${spring.redis.host}&quot;) String url, @Value(&quot;${spring.redis.port}&quot;) int port,
              @Value(&quot;${spring.redis.password}&quot;) String password) {
          this.url = url;
          this.port = port;
          this.password = password;
      }
  
      /**
       * Redis configuration
       *
       * @return redisStandaloneConfiguration
       */
      @Bean
      public RedisStandaloneConfiguration redisStandaloneConfiguration() {
          RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(url, port);
          redisStandaloneConfiguration.setPassword(password);
          return redisStandaloneConfiguration;
      }
  
      /**
       * Client Options Reject requests when redis is in disconnected state and Redis
       * will retry to connect automatically when redis server is down
       *
       * @return client options
       */
      @Bean
      public ClientOptions clientOptions() {
          return ClientOptions.builder().disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
                  .autoReconnect(true).build();
      }
  
      /**
       * Create a LettuceConnection with redis configurations and client options
       *
       * @param redisStandaloneConfiguration redisStandaloneConfiguration
       * @return RedisConnectionFactory
       */
      @Bean
      public RedisConnectionFactory connectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration) {
  
          LettuceClientConfiguration configuration = LettuceClientConfiguration.builder().clientOptions(clientOptions())
                  .build();
  
          return new LettuceConnectionFactory(redisStandaloneConfiguration, configuration);
      }
  
      // Setting up the redis template object.
      @SuppressWarnings({ &quot;rawtypes&quot;, &quot;unchecked&quot; })
      @Bean
      @ConditionalOnMissingBean(name = &quot;redisTemplate&quot;)
      @Primary
      public RedisTemplate&lt;String, Url&gt; redisTemplate(RedisConnectionFactory redisConnectionFactory) {
          Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Url.class);
          jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  
          RedisTemplate&lt;String, Url&gt; redisTemplate = new RedisTemplate&lt;&gt;();
          redisTemplate.setConnectionFactory(redisConnectionFactory);
          redisTemplate.setKeySerializer(new StringRedisSerializer());
          redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
          return redisTemplate;
      }
  }
  </code></pre>
  <p><em>and my application.properties</em></p>
  <pre><code>server.port=10095
  redis.ttl=86400
  
  spring.redis.host=localhost
  spring.redis.port=6379
  spring.redis.password=redisdb
  
  </code></pre>
  <p><strong>i have also tried changing spring.redis.host from localhost to 0.0.0.0 and 127.0.0.1</strong></p>
  <p>so when i hit it from postman I get the error</p>
  <p><a href="https://i.stack.imgur.com/QrivJ.png" rel="nofollow noreferrer">both containers running redis and springboot app</a></p>
  <p>How do i resolve this problem, any help is appreciated, thanks</p>