I have a spring boot app that runs on localhost fine. However, I decided to add caching recently and I run my Redis server from docker. The problem I am facing now is that my Springboot app can not communicate with my Redis running on docker. I get the error as follows:
2021-08-29 23:51:22.435 ERROR 24319 --- [0.1-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to redis/<unresolved>:6379] with root cause
java.net.UnknownHostException: redis: nodename nor servname provided, or not known
at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:932) ~[na:na]
at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1517) ~[na:na]
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:851) ~[na:na]
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300) ~[na:na]
at java.base/java.net.InetAddress.getByName(InetAddress.java:1250) ~[na:na]
at io.netty.util.internal.SocketUtils$8.run(SocketUtils.java:156) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.internal.SocketUtils$8.run(SocketUtils.java:153) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at java.base/java.security.AccessController.doPrivileged(AccessController.java:554) ~[na:na]
at io.netty.util.internal.SocketUtils.addressByName(SocketUtils.java:153) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.resolver.DefaultNameResolver.doResolve(DefaultNameResolver.java:41) ~[netty-resolver-4.1.65.Final.jar:4.1.65.Final]
at io.netty.resolver.SimpleNameResolver.resolve(SimpleNameResolver.java:61) ~[netty-resolver-4.1.65.Final.jar:4.1.65.Final]
at io.netty.resolver.SimpleNameResolver.resolve(SimpleNameResolver.java:53) ~[netty-resolver-4.1.65.Final.jar:4.1.65.Final]
at io.netty.resolver.InetSocketAddressResolver.doResolve(InetSocketAddressResolver.java:55) ~[netty-resolver-4.1.65.Final.jar:4.1.65.Final]
at io.netty.resolver.InetSocketAddressResolver.doResolve(InetSocketAddressResolver.java:31) ~[netty-resolver-4.1.65.Final.jar:4.1.65.Final]
at io.netty.resolver.AbstractAddressResolver.resolve(AbstractAddressResolver.java:106) ~[netty-resolver-4.1.65.Final.jar:4.1.65.Final]
at io.netty.bootstrap.Bootstrap.doResolveAndConnect0(Bootstrap.java:206) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.bootstrap.Bootstrap.access$000(Bootstrap.java:46) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.bootstrap.Bootstrap$1.operationComplete(Bootstrap.java:180) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.bootstrap.Bootstrap$1.operationComplete(Bootstrap.java:166) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:552) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.channel.DefaultChannelPromise.trySuccess(DefaultChannelPromise.java:84) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.safeSetSuccess(AbstractChannel.java:1012) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:516) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.access$200(AbstractChannel.java:429) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:486) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500) ~[netty-transport-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.65.Final.jar:4.1.65.Final]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:an]
Below is what I have in my application.properties file
server.port=8080
server.address=localhost
management.endpoints.enabled-by-default= false
management.endpoint.health.enabled=true
springdoc.swagger-ui.disable-swagger-default-url=true
spring.cache.type=redis
spring.redis.host=redis
spring.redis.port=6379
spring.cache.redis.cache-null-values=false
spring.cache.redis.time-to-live=60000
spring.cache.redis.use-key-prefix=true
spring.cache.redis.key-prefix=digicore
Can anyone please explain what I might be doing wrong or if I am missing any file or configuration. Is it not possible to run springboot app locally and redis on docker?
2
Answers
Thanks to @Jonatan Ivanov, I made the following changes:
spring.redis.host
tolocalhost
instead ofredis
docker run --name DigicoreRedis -p 6379:6379 -d redis
It’s in the error message:
java.net.UnknownHostException: redis
redis
is not a known hostname outside of docker, but if you expose port6379
, you can make redis available onlocalhost
:spring.redis.host=localhost
Make sure you map the port
6379
on docker side (-p 6379:6379
).You can test it via:
telnet localhost 6379
.