Redis Docker connection refused
Asked Answered
B

7

47

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.

Barbellate answered 12/11, 2015 at 16:5 Comment(2)
If you're running boot2docker (or in any other VM) you need to use the IP of the VM not localhost. If you're using docker-machine you can find the right IP to use with docker-machine ip default.Finzer
In case, I just changed the port and it work great. I've tried different solutions before, but nothing worked.Bacchanalia
L
56

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
➜  ~ 
Loden answered 12/11, 2018 at 10:11 Comment(0)
R
16

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

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-server
Reversion answered 11/5, 2017 at 0:59 Comment(1)
This is what i was looking for. Above command gives a IP Address to connect at default port of 6379Rabassa
G
8

If you're using the Redis 6.2 configuration example as a starting place (like I was when I encountered this issue), the combination of the bind 127.0.0.1 directive and protected-mode yes was not allowing for communication between Docker containers. For my purposes I commented out the bind directive and set the protected-mode directive to no -- but heed the provided warnings about not exposing Redis to the open internet.

Gummous answered 8/1, 2020 at 1:15 Comment(0)
O
7

I recomend you to use docker-compose https://docs.docker.com/compose/

in you docker-compose.yml

redis:                                                                          
  image: redis
ports:
  - "6379:6379" 

After this you can run:

$ docker-compose run redis /bin/bash
$ redis-server
$ redis-cli ping

Should return: PONG

Oscillogram answered 20/10, 2016 at 12:48 Comment(0)
T
5

It looks like a networking problem caused by the redis client, as your redis server looks fine.

Mind that the redis docker page is a little confusing. It says that to connect to your redis via redis-cli, you should type in:

docker run -it --network some-network --rm redis redis-cli -h some-redis

This however creates another docker container which is basically the redis client. It cannot connect to your redis server because they are not connected to the same network.

You could do something a little different as following:

docker network create some-network
docker run --name redis-server --network some-network -d redis
docker run -it --network some-network --rm redis redis-cli -h redis-server

The other option is to indeed map the port 6379 like you did, however it will require from you to use a redis client which should be downloaded to your own computer.

Tieback answered 2/1, 2020 at 10:19 Comment(0)
L
1

You can spawn a Redis container with network_mode set to host. This would allow you to connect to the Redis server from your localhost.

docker-compose.yml

version: '3.7'

services:
  redis:
    image: 'redis:latest'
    network_mode: host
    ports:
      - '6379:6379'
    tty: true
docker compose up -d

Or you could exec into your container, then connect to the Redis server from your container like:

docker run --name some-redis -d redis
docker exec -it some-redis /bin/bash
redis-cli
Lunation answered 14/6, 2023 at 2:12 Comment(0)
D
0

Simply tell docker wich port to map explicitly:

 docker run  --publish=6379:6379 redis
Droplight answered 11/7, 2023 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.