Raji
Raji

Reputation: 887

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

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

Upvotes: 15

Views: 56407

Answers (7)

Thom Kok
Thom Kok

Reputation: 15

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

Upvotes: 1

Angel Quiroz Soriano
Angel Quiroz Soriano

Reputation: 19

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.

Upvotes: 0

wc203
wc203

Reputation: 1217

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.

Upvotes: 1

PoulsQ
PoulsQ

Reputation: 2016

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.).

Upvotes: 0

user185160
user185160

Reputation: 966

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.

Upvotes: 7

Ashcan
Ashcan

Reputation: 71

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.

Upvotes: 6

Chris Tanner
Chris Tanner

Reputation: 1650

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.

Upvotes: 23

Related Questions