Could not connect to Redis at 127.0.0.1:6379: Connection refused in docker
Asked Answered
S

7

15

I am using a redis-server:latest image. I used "docker run -it --name="redis2" redis:1 bash" command and got inside the container. I saw that by default redis is listening to Port: 6379.

Running in stand alone mode

Port: 6379

PID: 39

http://redis.io

[39] 01 Mar 09:03:45.669 # Server started, Redis version 2.8.4 [39] 01 Mar 09:03:45.669 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 

To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. [39] 01 Mar 09:03:45.669 * The server is now ready to accept connections on port 6379

And then further there is no response. I tried "redis-cli ping". There was no response. Then I hit "ctrl+c" and type, "redis-cli ping" and get following response:

Could not connect to Redis at 127.0.0.1:6379: Connection refused

I tried to change the port to 6001 by executing following:

redis-server --port 6003

And I see following response:

Running in stand alone mode

Port: 6003

PID: 47

And again I tried "redis-cli ping" and it threw me the same error:

Could not connect to Redis at 127.0.0.1:6379: Connection refused

How do I fix this? Also I have updated the port(the new port 6003) in "/etc/redis/redis.conf" location.

Thank you

Scrutable answered 1/3, 2017 at 10:35 Comment(0)
F
23

Redis is listening on that port on the internal docker network, to access it from your local machine you need to map the container port to your local port using -p 6379:6379. That way if you have multiple redis containers you can map them each to different ports on your machine.

Flowage answered 1/3, 2017 at 11:8 Comment(4)
Hi Chris. Thanks a lot for the response. I tried following: "docker run -it --name="redis3" -p 6000:6379 redis:1 bash" and I enter into container. I typed "redis-cli" and it threw me error : Could not connect to Redis at 127.0.0.1:6379: Connection refused. I am sorry but I am little new to both docker and redis. Can you please let me know how can I make it run inside the container?Scrutable
I got it. Ran "sudo service redis-server start" and it fixed the issue.Scrutable
6000:6379 this means that you're mapping port 6379 from inside the container to port 6000 on the host. That's why were not able to connect at 6379 the first timePerfusion
I should do docker run -p 6379:6379 redis?Reiko
M
7

If you are getting this error:

Could not connect to Redis at 127.0.0.1:6379: Connection refused

You may need to daemonize the redis-server so that docker can connect to it:

/usr/local/bin/redis-server --daemonize yes

Then try running redis-cli ping again.

Marathon answered 4/6, 2021 at 18:45 Comment(0)
C
6

Well, it's been a while but for anybody else faced the same issue; when you put the "sh" command in "docker run" it replaces the default command which is starting Redis server. So you can either spin up the container in a separate terminal like "docker run redis" and in another terminal run "docker exec -it #container_id sh" which is a more common way to start up your container in a primary process and then attach a running shell to it or map the container port to your local port as Chris suggested.

Culch answered 8/6, 2020 at 6:32 Comment(1)
Thank you for this. It just saved me hours of hair pullingHearten
C
1

For future viewers:

  1. run sudo docker container ls to get all running docker containers

  2. sudo docker exec -it <redis_container_name> redis-cli -h <ec2_ip_address> -p 6379:6379 FLUSHALL

Hope this helped you in some way.

Clink answered 19/9, 2022 at 9:37 Comment(0)
C
1

I am using a laravel docker project. I had to change the REDIS_HOST variable in the .env.

Before:

REDIS_HOST=127.0.0.1

After:

REDIS_HOST=redis
Caskey answered 5/3 at 10:59 Comment(0)
Z
0

Ok, I had the same problem tonight.

I was using a Redis container created from my docker-compose.yml:

redis:
    container_name: 'myproject-redis'
    image: redis:latest
    ports:
      - "127.0.0.1:${HOST_MACHINE_REDIS_PORT}:6379"

My solution was to use the container "ID" -> here 'redis' instead of using the normal connection IP 127.0.0.1.

Why ? I assume that my Docker was creating a bridge directly to the created container (like it's often the case for MySQL i.e.).

Zampino answered 26/12, 2021 at 17:57 Comment(0)
B
0

I had the same problem using [email protected], when passing the host and port I got the same connection error message in nodejs. Using a lower version (3.1.2) worked for me. Additionally, I have read in the documentation that [email protected] accepts a connection url.

Balefire answered 2/11, 2022 at 1:10 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewNachison

© 2022 - 2024 — McMap. All rights reserved.