0

Im trying to run a spring boot application with redis locally using docker. The app cannot seems to connect with the redis container.

My Dockerfile

FROM java:8
VOLUME /tmp
ADD target/test-*.jar test.jar
RUN bash -c 'touch /test.jar'
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/test.jar"]

docker-compose.yml

version: '2'
services:
  app:
    build: .
    ports:
      - "8080:8080"
    links:
      - "db:redis"
  db:
    image: "redis:alpine"
    hostname: redis
    ports:
      - "6379:6379"

application properties

spring.redis.host=redis

When the application try to connect to the database it throws

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool] with root cause
app_1  | 
app_1  | java.net.ConnectException: Connection refused (Connection refused)

Code that i use to connect to db

@Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

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

any help would be really appreciated. Thanks

Dilantha
  • 1,422
  • 2
  • 27
  • 41
  • Does this answer your question? [When to use --hostname in docker?](https://stackoverflow.com/questions/43031100/when-to-use-hostname-in-docker) – Turing85 Sep 03 '20 at 20:32
  • What you probably want is an [alias](https://docs.docker.com/compose/compose-file/#aliases) for `db` or just call the container by its service name `db`. – Turing85 Sep 03 '20 at 20:35
  • spring.redis.port is set? how do you connect to the redis in your code? – ferdy Sep 03 '20 at 20:38
  • @Turing85 Do you mean using links: - "db" instead of links: - "db:redis" ? – Dilantha Sep 03 '20 at 21:04
  • Yes. [Or forego `links` alltogether since it is deprecated](https://docs.docker.com/compose/compose-file/#links) and use [`depends_on`](https://docs.docker.com/compose/compose-file/#depends_on) or [user-defined networks](https://docs.docker.com/compose/networking/). – Turing85 Sep 03 '20 at 21:06
  • @ferdyi tried adding the port as well.. but didnt work. i modified my question with the code im using – Dilantha Sep 03 '20 at 21:07
  • I tried it with a python client. Your code works for me. Therefore I think it has to do with the java networking actually not being able to resolve "redis" to a real host. I'd have been thinking, the "links" creating hard connections in your containers /etc/hosts file, but actually i am not quite sure about this. – ferdy Sep 03 '20 at 21:13
  • I've tried directly using jredis in groovy and it worked. I also used spring along with java and jredis as you did and it worked. Did you run a clean `docker-compose build` in between? Do you have a VPN connection while you work with containers? Which OS you're working with? How exactly does your code look like? I need to reproduce your error to understand it better. – ferdy Sep 03 '20 at 22:33
  • @ferdy Thanks for the help. after i removed the beans where it creates JedisConnectionFactory and RedisTemplate it worked ! – Dilantha Sep 04 '20 at 13:43

1 Answers1

0

Change your docker-compose.yml file link in app service to - links: - "db". When making a link between two services, you dont need to add the hostname. The hostname will be used to refer in your code. Which you have done correctly in your applications.properties file.

  • Thanks for your reply. I tried that.. but still giving the same exception – Dilantha Sep 03 '20 at 21:23
  • "*The hostname will be used to refer in your code.*" - No. The hostname sets the hostname in the container it is specified on (i.e. pinging the hostname from within the container will result in pinging localhost) – Turing85 Sep 03 '20 at 21:24