skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search