Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Docker connection refused

I just built the redis docker instance

$ docker pull redis 

After which I ran it like this.

$ docker run --name=redis --detach=true --publish=6379:6379 redis 

I get the following

$ docker ps  key        redis   "/sbin/entrypoint.sh"    22 minutes ago      Up 22 minutes       0.0.0.0:6379->6379/tcp   redis 

To me the above means that it is now running listening on port 6379 on localhost or 127.0.0.1 or 0.0.0.0.

But to my great surprise, when I try to connect is responds with connection refused.

Please can someone throw some light.

like image 780
Ikenna Avatar asked Nov 12 '15 16:11

Ikenna


People also ask

How do I connect Redis to Docker?

To connect to a Redis instance from another Docker container, add --link [Redis container name or ID]:redis to that container's docker run command. To connect to a Redis instance from another Docker container with a command-line interface, link the container and specify the host and port with -h redis -p 6379.

Could not connect to Redis at Connection Refused?

Firewall restriction is another common reason that can trigger the “could not connect to Redis connection refused”. By default Redis server listen to the TCP port 6379. If another application is using the port or if the firewall restrictions blocks the port, it can trigger the connection refused error.

Can Redis run on Docker?

If you wish to connect to a Docker container running Redis from a remote server, you can use Docker's port forwarding to access the container with the host server's IP address or domain name. To use Docker's port forwarding for Redis, add the flag -p [host port]:6379 to the docker run command.


2 Answers

You need to provide more information about your environment (OS, Docker installation, etc), but basically, if you start your Redis container like this:

docker run --name=redis-devel --publish=6379:6379 --hostname=redis --restart=on-failure --detach redis:latest 

It should expose the port no matter what. The only reason you might not be able to connect to it, is if you've messed up your bridge interface, if you're on Linux, or you're using a docker machine with its own network interface and IP address and you're not connecting to that IP address. If you're using Docker for Mac, then that only supports routing to the localhost address, since bridging on Mac hosts doesn't work yet.

Anyway, on MacOS with Docker for Mac (not the old Docker Toolbox), the following should be enough to get your started:

➜  ~ docker run --name=redis-devel --publish=6379:6379 --hostname=redis --restart=on-failure --detach redis:latest 6bfc6250cc505f82b56a405c44791f193ec5b53469f1625b289ef8a5d7d3b61e ➜  ~ docker ps CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES 6bfc6250cc50        redis:latest        "docker-entrypoint.s…"   10 minutes ago      Up 10 minutes       0.0.0.0:6379->6379/tcp   redis-devel ➜  ~ redis-cli ping PONG ➜  ~  
like image 193
Paul-Sebastian Manole Avatar answered Sep 20 '22 21:09

Paul-Sebastian Manole


Find the discovered port of the redis and use that to connect

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-server 
like image 37
Rajesh Guptan Avatar answered Sep 17 '22 21:09

Rajesh Guptan