I implemented simple, standalone webapp with REST API based on Spring Boot framework. My app is using Redis to cache some data. Now I would like to dockerize webapp and Redis cache to provide simple run procedure without forcing Redis installation in working environment.
I tried many approaches but I still struggle with RedisConnectionFailureException: Cannot get Jedis connection
and root cause ConnectException: Connection refused
.
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<repository>${project.artifactId}</repository>
</configuration>
</plugin>
</plugins>
</build>
docker-compose.yml:
version: '3'
services:
web:
build: .
ports:
- "8080:8080"
links:
- redis
redis:
image: redis
ports:
- "6379:6379"
Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/xxx.jar abc.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS - Djava.security.egd=file:/dev/./urandom -jar /abc.jar" ]
application.properties:
spring.redis.host=redis
spring.redis.port=6379
I autowire StringRedisTemplate
via constructor, get ValueOperations
from template and simple use it (No extra Redis configuration). I run app with command sudo docker-compose up
and get exception listed above with every Sprin Boot app call to Redis. Redis cache is reachable from host machine. Without Docker and when Redis service is installed everything is working fine.
UPDATE: I am using application.properties that I pasted before and application.yml where I store some custom configuration related to logic of my app (not framework configuration). I read that it shouldn't be a problem because Spring Boot load both files and merges them. I suspect that Redis host is not set correctly in Jedis connection but I have no idea how to check it.
Try to create a docker network and attach the containers to this network:
docker network create spring
version: '3'
services:
web:
build: .
ports:
- "8080:8080"
networks:
- spring
redis:
image: redis
command: [ "redis-server", "--protected-mode", "no" ]
ports:
- "6379:6379"
networks:
- spring
networks:
spring:
external: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With