0

I have created a spring app and i want to connect it to redis server which is deployed on docker-compose i put the needed properties as follow : spring.redis.host=redis spring.redis.port=6379

But i keep getting a ConnexionException so how can i Know on which host redis is running and how to connect to it.

Here is my docker-compose file :

version: '2'
services:

redis:
   image: 'bitnami/redis:5.0'
   environment:
    # ALLOW_EMPTY_PASSWORD is recommended only for development.
    - ALLOW_EMPTY_PASSWORD=yes
    - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
   ports:
    - '6379:6379'
   volumes:
    - 'redis_data:/bitnami/redis/data'

volumes: redis_data: driver: local

enter image description here

  • Post the error log – Beez Feb 13 '20 at 21:28
  • Check if you can access the Redis instance from where you are running the Spring code. Example - https://stackoverflow.com/questions/9866541/how-to-check-whether-the-redis-server-is-running – Shankar Feb 13 '20 at 21:35

2 Answers2

2

From docker compose documentation

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name

If you want to access redis by container name ('redis' in this case), the Spring boot application has also be deployed as a docker compose service, but it doesn't appear in the docker-compose file that you've provided in the question, so please add it.

Alternatively If you're trying to run the spring boot application on host machine, use 'localhost' instead of 'redis' in order to access the redis container.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
0

Another approach you can use is "docker network" , Below are the steps to follow :

  1. Create a docker network for redis

    docker network create redis-docker

  2. Spin up redis container is redis-docker network.

    docker run -d --net redis-docker --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest

  3. Inspect the redis-docker container

    docker network inspect redis-docker

enter image description here

  1. Copy the "IPv4Address" IP and paster in application.yml

enter image description here

  1. Now build , start your application.
Dhruv sahu
  • 67
  • 1
  • 2