Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check/connect to Redis running within docker from Java(Spring Boot) or Node.js

Tags:

I quick started with Redis on Windows PC with

docker run -p 6379:6379 redis

(Redis does not have Windows distribution, fork for Windows is not the latest version )

1:C 10 Sep 08:17:03.635 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

...
1:M 10 Sep 08:17:03.644 * The server is now ready to accept connections on port 6379

Then however I can't connect from Spring Boot app. With application.properties like

spring.redis.host=localhost
spring.redis.port=6379

got error

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:164) ~[jedis-2.8.2.jar:na]
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80) ~[jedis-2.8.2.jar:na]
    at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1677) ~[jedis-2.8.2.jar:na]
    at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:87) ~[jedis-2.8.2.jar:na]
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868) ~[commons-pool2-2.4.2.jar:2.4.2]
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435) ~[commons-pool2-2.4.2.jar:2.4.2]
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) ~[commons-pool2-2.4.2.jar:2.4.2]
    at redis.clients.util.Pool.getResource(Pool.java:49) ~[jedis-2.8.2.jar:na]
    ... 23 common frames omitted
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_45]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_45]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_45]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_45]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_45]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_45]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_45]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_45]
    at redis.clients.jedis.Connection.connect(Connection.java:158) ~[jedis-2.8.2.jar:na]
    ... 30 common frames omitted

When trying to use Node.js with node_redis example, I got

Error Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
like image 982
Paul Verest Avatar asked Sep 10 '16 09:09

Paul Verest


2 Answers

As you mentioned (in the comments), redis bundled their image with protected-mode set to yes (see here).

How to go around protected-mode

  • 1) Disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
  • 2) Alternatively you can disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server.
  • 3) If you started the server manually (perhaps for testing), restart it with the '--protected-mode no' option.
  • 4) Setup a bind address or an authentication password.

source: redis-github

Build your own image

  • You could create your own image by pulling redis's and ADDing your own redis.conf to the image ?
  • Or update the start command in the Dockerfile to disable protected-mode: CMD [ "redis-server", "--protected-mode", "no" ]

You can also take a look at this Dockerfile which contains the modification suggested above (last line): https://github.com/docker-library/redis/blob/23b10607ef1810379d16664bcdb43723aa007266/3.2/Dockerfile

This Dockerfile is provided in a Redis issue on github, it replaces the startup command with CMD [ "redis-server", "--protected-mode", "no" ]. You could just download this Dockerfile and type:

$ docker build -t redis-unprotected:latest .
$ docker run -p 6379:6379 redis-unprotected
like image 91
alexbt Avatar answered Sep 24 '22 16:09

alexbt


Ran into a similar problem today. Using the redis container's IP address for the JedisConnectionFactory solved the issue for me.

Docker command:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' some-redis-instance
like image 33
srinivasreddyramaram Avatar answered Sep 26 '22 16:09

srinivasreddyramaram