GAK
GAK

Reputation: 1088

Docker compose spring boot redis connection issue

I am running a simple rest app with redis running in a docker container/docker-compose. I believe, redis must be accessible to spring boot using http://redis:6379. But, it throws the error:

018-07-22 21:53:33.972 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool] with root cause

java.net.ConnectException: Connection refused (Connection refused)

My code is here.

Upvotes: 2

Views: 2724

Answers (2)

Avinash Khadsan
Avinash Khadsan

Reputation: 497

I Was Facing this Issue for two days and final I got the answer above.
i was missing please see below my docker-cmpose.yaml
  **links:
        - "redis"**

----------------------------------------
version: "3.8"

services:
  mongo_db:
    image: mongo:5.0.2
    restart: unless-stopped
    env_file: ./.env
    environment:
      - MONGO_INITDB_ROOT_USERNAME=$MONGODB_USER
      - MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD
    ports:
      - $MONGODB_LOCAL_PORT:$MONGODB_DOCKER_PORT
    volumes:
      - db:/data/db
  redis:
    image: redis
    ports: 
      - "6379:6379"
  app:
    links:
        - "redis"
    depends_on:
      - mongo_db
      #- cache
    build: ./order-service
    restart: on-failure
    env_file: ./.env
    ports:
      - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
    environment:
      SPRING_APPLICATION_JSON: '{
          "spring.data.mongodb.uri" : "mongodb://$MONGODB_USER:$MONGODB_PASSWORD@mongo_db:$MONGODB_DOCKER_PORT/$MONGODB_DATABASE?authSource=admin"
        }'
    volumes:
      - .m2:/root/.m2
    stdin_open: true
    tty: true

volumes:
  db:

Upvotes: 0

Dilan Tharaka
Dilan Tharaka

Reputation: 567

Since you are using alias in links, you have to use the hostname identical to the alias to access the container. Therefore you can do one of following,

use http://localhost:6379 instead of http://redis:6379 in your spring boot application

or,

change

links:
 - "redis:localhost"

to

links:
 - "redis"

Upvotes: 4

Related Questions