0

running redis docker image in local as suggested in url link which is running fine as seen from log

docker pull redis
docker run --name some-redis -d redis

and then docker starts. I am able to see logs as below

docker logs --follow some-redis
1:C 08 Jul 2021 07:17:51.243 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 08 Jul 2021 07:17:51.243 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 08 Jul 2021 07:17:51.243 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 08 Jul 2021 07:17:51.244 * monotonic clock: POSIX clock_gettime
1:M 08 Jul 2021 07:17:51.247 * Running mode=standalone, port=6379.
1:M 08 Jul 2021 07:17:51.248 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 08 Jul 2021 07:17:51.248 # Server initialized
1:M 08 Jul 2021 07:17:51.250 * Ready to accept connections

I am using jedis to connect to this redis container here both this code and redis docker are in local machine. The code below is not on the container but local host machine. The redis already exposes 6379 port redis dockerfile link

public static void main(String [] args) {
    System.out.println("connecting");
    Jedis jedis;
    jedis = new Jedis("localhost",6379);
    System.out.println("connected and pinging now");
    //jedis.ping();
    jedis.set("events/city/rome", "32,15,223,828");  //line 274
    String cachedResponse = jedis.get("events/city/rome");
    System.out.println("ping done");
}

I am getting error connecting

    connecting
connected and pinging now
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)
    at redis.clients.jedis.Connection.connect(Connection.java:164)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
    at redis.clients.jedis.BinaryClient.set(BinaryClient.java:97)
    at redis.clients.jedis.Client.set(Client.java:32)
    at redis.clients.jedis.Jedis.set(Jedis.java:68)
    at com.org.app.module.TaskPerformer.main(TaskPerformer.java:274)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:606)
    at redis.clients.jedis.Connection.connect(Connection.java:158)
    ... 6 more

This error occurs at line 273 for jedis.ping() as well

veer7
  • 20,074
  • 9
  • 46
  • 74
  • Do you have both your java application and redis containerized (eventually by docker-compose)? If so they must be on the same network to reach each other. If you're trying to access containerized redis from your host machine you'll also need to expose the redis port in order to be able to reach it. In these cases, posting your docker/docker-compose setup is always useful for people to be able to help you out. – nitrin0 Jul 08 '21 at 09:06
  • @nitrin0: docker and java both are local... and 6379 already exposed in redis docker image https://github.com/docker-library/redis/blob/4422738bea4a9d38971a87fc820525864bb5ff62/6.2/Dockerfile – veer7 Jul 08 '21 at 09:12
  • 1
    Are you able to connect to your redis instance using redis-cli (or telnet/whatever)? If not look for firewall issues and, in redis.conf, for settings like `protected-mode` (should be set to "no") and `bind` (comment it if it isn't already). – nitrin0 Jul 08 '21 at 09:42
  • Can you edit the question to include details on how you're launching this setup? What's the `docker run` command(s) you're using; is the client application in a container, and is it on the same host as Redis? If the application is outside a container on the host, are you _publishing_ ports from the Redis container? – David Maze Jul 08 '21 at 09:48
  • @nitrin0 `redis-cli` says `not-connected` – veer7 Jul 08 '21 at 10:55
  • @DavidMaze - provided all the information. port are published in redis docker as in https://github.com/docker-library/redis/blob/4422738bea4a9d38971a87fc820525864bb5ff62/6.2/Dockerfile – veer7 Jul 08 '21 at 10:59
  • 2
    You need a `docker run -p` option to publish a container port to a host port (to make `localhost:6379` from the point of view of a non-container application). A Dockerfile `EXPOSE` setting doesn't do this on its own, and "expose" as a verb means very little in Docker. – David Maze Jul 08 '21 at 11:07

1 Answers1

5

You should publish the Redis port (TCP 6379) to the host system, either by explicitly mentioning the port or by using the host network.

Here is how to publish only TCP port 6379 (recommended solution):

docker run --rm -p 6379:6379 redis

And here is how you could that by using the network host (which appears to be overkill for your simple case and may expose to further security risks):

docker run --rm --network host redis
Efran Cobisi
  • 6,138
  • 22
  • 22
  • Use intellij, open docker container under the services tool window, choose your container then select port binding tab. Place the appropriate port bindings and it should work. – Kevin Kiwango Apr 10 '23 at 19:24