skip to Main Content

So I currently have a service for an eccomerce project which uses redis and jedis to connect. It is working when ran using source code but when it is dockerized it pops up the following error:

redis.clients.jedis.exceptions.JedisConnectionException: Failed to connect to any host resolved for DNS name.

When I ran redis docker container and my service’s source code it works no problem

Docker Compose:

version: '3'

services:
  

  order-api:
    build:
      context: ./orderapi
      dockerfile: Dockerfile
    ports:
      - "8002:8002"
    depends_on:
      - redis
    environment:
      - MYSQL_HOST=host.docker.internal
      - MYSQL_PORT=3306
      - MYSQL_DATABASE=ecommerce_fyp_order
      - MYSQL_USER=root
      - MYSQL_PASSWORD=12345
      - REDIS_HOST=redis #options: host.docker.internal, redis, localhost
      - REDIS_PORT=6379
    networks:
      - net

  redis:
    image: redis:latest #IPaddress is 172.18.0.2
    ports:
      - "6379:6379"
    command: ["redis-server", "--bind", "redis", "--port", "6379"]
    networks:
      - net

networks:
  net:
    driver: bridge

I am coding this in spring boot and the following is my application.properties of my service:

server.port=8002
spring.application.name=orderapi

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/ecommerce_fyp_order
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql: true

#Redis
spring.session.redis.namespace=session
spring.data.redis.host={REDIS_HOST:localhost}
spring.data.redis.port=6379

Redis Configuration:

@Configuration
public class RedisConfig {
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }

}

I have tried changing the REDIS_HOST to redis, host.docker.internal, redis, localhost but all do not work.

Would appreciate any help to resolve this.

Update: I tried to connect to the redis container using docker command

docker exec -it e-commercenew-redis-1 redis-cli

and I get the following error

Could not connect to Redis at 127.0.0.1:6379: Connection refused

It work when I run redis container and run the service using the spring boot source code but it doesn’t work when run using docker compose so not too sure why the connection is being refused

Any suggestions on how to solve this on docker thank you

More Information on Docker Enviroment Json:

"Env": [
            "REDIS_PORT=6379",
            "MYSQL_HOST=host.docker.internal",
            "MYSQL_PORT=3306",
            "MYSQL_DATABASE=ecommerce_fyp_order",
            "MYSQL_USER=root",
            "MYSQL_PASSWORD=12345",
            "REDIS_HOST=redis",
            "PATH=/usr/java/openjdk-17/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "JAVA_HOME=/usr/java/openjdk-17",
            "LANG=C.UTF-8",
            "JAVA_VERSION=17.0.2",
            "MAVEN_HOME=/usr/share/maven",
            "MAVEN_CONFIG=/root/.m2",
            "SPRING_DATA_REDIS_HOST=localhost",
            "SPRING_DATA_REDIS_PORT=6379"
        ]
    

2

Answers


  1. read your docker file , it indicate that you build a internal network and request the service via its name. but , you define the redis host in spring application yaml file with hard code ?? you have not realize the config spring.data.redis.host=localhost is strange?

    for reslove this issue , you should define the

    spring.data.redis.host={REDIS_HOST:localhost}, it would read the variables from deploy environment whici is you specified the redis host in docker compose file in current scenario, if there is empty ,it would take defalut valud localhost .

    - REDIS_HOST=redis #options: host.docker.internal, redis, localhost

    Login or Signup to reply.
  2. Try spring.data.redis.host={REDIS_HOST}.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search