84

I see lots of people struggling with this, sort of feel like maybe there is a bug in the redis container image, and others seem to be chasing a similar problem.

I'm using the standard redis image on DockerHub. (https://github.com/dockerfile/redis)

running it like this:

docker run -it -p 6379:6379 redis bash

Once I'm in I can start the server, and do a redis ping from the container image.

Unfortunately, I cannot connect to the redis container from my host.

I have tried setting, such as below.

bind 127.0.0.1

and removed the bind from the configuration

and tried turn off protected mode

protected-mode no

I know it is reading the configuration file, since I changed ports just to test, and I was able to do that.

I'm running Windows 10, so maybe it is a windows networking issue. I never have a problem with docker normally. I'm puzzled

user3888307
  • 2,825
  • 5
  • 22
  • 32
  • How are you trying to connect to redis container? Are you trying to access bash inside redis container or trying to connect to redis container using 6379 port? – Rafaf Tahsin Dec 29 '16 at 04:11

9 Answers9

79

The problem is with your bind, You should set the following:

bind 0.0.0.0

This will set redis to bind to all interfaces available, in a containerized environment with one interface, (eth0) and a loopback (lo) redis will bind to both of the above. You should consider adding security measures via other directives in config file or using external tools like firewalls. because with this approach everyone can connect to your redis server.

The default setting is bind 127.0.0.1 and this setting will cause redis to only listen on loopback interface, and it will be only accessible from inside the container. (for security)

To run redis with custom configuration file:

sudo docker run -d --name redis-test -p 6379:6379  -v /path/to/redisconf/redis.conf:/redis.conf redis redis-server /redis.conf

Now to verify on docker host with redis-tools installed:

redis-cli                           
127.0.0.1:6379> 
127.0.0.1:6379> set farhad likes:stackoverflow
OK
127.0.0.1:6379> get farhad
"likes:stackoverflow"
127.0.0.1:6379> 

You can also connnect to your redis container from an external host via:

redis-cli -h 'IP-address-of-dockerhost-running-redis-container'
Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70
  • Thank you for your help, didn't quite get me there. It seems like Redis doesn't seem to like it when I try to connect from my host computer, but I spun up a second docker image, and got the IP address of the redis docker image, and I was able to connect. From a second docker image I was able to connect to the redis server like this: redis-cli -h 172.17.0.2 – user3888307 Dec 29 '16 at 22:43
  • You should be able to connect to it if you can connect from another container. Just make sure your have `bind 0.0.0.0` in your custom config and you `expose 6378` when creating the container. from the docker hos your can connect by running `sudo redis-cli`. If all fails, give me step by step of what you are doing so I can reproduce the problem. – Farhad Farahi Dec 30 '16 at 06:29
  • Thanks Farhad for your help. It seems to be something with the redis image on docker hub, I installed redis on an Ubuntu Server and it worked as I would have expected.I will just use that to hit redis from my local host – user3888307 Dec 31 '16 at 03:20
  • @FarhadFarahi thankyou. thankyou. thankyou. setting the bind has ended about 5 days of stress for me. ha – Ashley Reid Jun 23 '20 at 23:54
  • 1
    There should be a warning with big bold red letters before this answer "If you only apply this, your server will get hacked". – pkExec Feb 18 '21 at 12:49
  • 1
    @pkExec maybe try to read the answer before copy pasting? "You should consider adding security measures via other directives in config file or using external tools like firewalls. because with this approach everyone can connect to your redis server.", I can make it bold if it helps, but not red :) – Farhad Farahi Feb 22 '21 at 11:39
  • In my test, seems, by default, docker container is accessible from remote, as long as your map port to host. – Eric Sep 14 '21 at 07:27
75

This is simpler way to set up a Redis container.

Download image and run container

docker run -d --name some-redis -p 6379:6379 redis

If you don't have the image, this command will pull it. And then, if you need to access from redis-cli to console, can use:

docker exec -it some-redis bash

For enter to container console, and kind in the console:

root@72c388dc2cb8:/data# redis-cli

Output:

127.0.0.1:6379> 

This was enough for my use case (easy and fast local development).

Brian Ocampo
  • 1,345
  • 1
  • 12
  • 22
  • 11
    This looks like a good solution! I was able to open `redis-cli` directly by running the following command `docker exec -it some-redis redis-cli` and skip over the `bash` step. – dana Oct 27 '20 at 16:30
  • 39
    This is totally the opposite of what was asked in the question. He wants to connect to the redis-server running in a container from the host environment (on which the container is running). That is, if you started a redis container on your linux machine, he wants to access it from the linux machine and not from inside the redis container. – cyphx Dec 10 '20 at 07:22
8

It might be easier now with version 4.0.9 (Docker Toolbox on Win10). Simply connect with a redis client, then:

set bind 0.0.0.0
save

The new setting sticks after stop/start.

Darek
  • 4,687
  • 31
  • 47
5

Here are some instructions to make this work properly.

Install Official Docker not Distro repo.

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
systemctl enable docker ; systemctl start docker; systemctl status docker

Refer to Install using the convenience script

Connect to Redis container from host

mkdir -p /etc/redis/
chown -R 1000:1000 /etc/redis
sudo docker run -d --name redis -p 6379:6379 --restart unless-stopped -v /etc/redis/:/data redis redis-server /data

NOTE: The important part that is key to your solution is to have port expose (-p 6379:6379) to your docker host and route to container port. Refer to Redis Docker Documentation

Install "redis-tools" in your docker host. Centos install redis via epel release.

Randy Lerma
  • 59
  • 1
  • 1
4

create Redis container using below command

sudo docker run -d --name redis-test -p 6379:6379  -v /redis/redis.conf:/redis.conf redis redis-server /redis.conf --appendonly yes --requirepass "redis"

you can access the Redis in the same machine using Redis-CLI and if you are using other machines use host machine IP. if you are accessing Redis container in the same host another docker container uses the private IP of the machine.

madhu
  • 1,083
  • 3
  • 12
  • 31
1

docker-compose.yml

version: '3.7'

services:
  redis_1:
    image: 'redis:6.0.6'
    ports:
      - '6371:6379'
  redis_2:
    image: 'redis:6.0.6'
    ports:
      - '6372:6379'

In your host machine:

docker compose up
redis-cli -p 6371 ping
redis-cli -p 6372 ping
Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
0

In case you want to run Redis Cluster in docker containers.

The answer is in the Redis self-documented redis.conf file and here is an extract from the file on how to solve the problem like a pro.

All credits to the redis.conf file, redis version 6 and above.

########################## CLUSTER DOCKER/NAT support  ########################

# In certain deployments, Redis Cluster nodes address discovery fails, because
# addresses are NAT-ted or because ports are forwarded (the typical case is
# Docker and other containers).
#
# In order to make Redis Cluster working in such environments, a static
# configuration where each node knows its public address is needed. The
# following two options are used for this scope, and are:
#
# * cluster-announce-ip
# * cluster-announce-port
# * cluster-announce-bus-port
#
# Each instructs the node about its address, client port, and cluster message
# bus port. The information is then published in the header of the bus packets
# so that other nodes will be able to correctly map the address of the node
# publishing the information.
#
# If the above options are not used, the normal Redis Cluster auto-detection
# will be used instead.
#
# Note that when remapped, the bus port may not be at the fixed offset of
# clients port + 10000, so you can specify any port and bus-port depending
# on how they get remapped. If the bus-port is not set, a fixed offset of
# 10000 will be used as usual.
#
# Example:
#
# cluster-announce-ip 10.1.1.5
# cluster-announce-port 6379
# cluster-announce-bus-port 6380
Margach Chris
  • 1,404
  • 12
  • 20
0

Found many articles, questions, github-issues about not being able to connect from Windows host to redis running as a docker container on wsl2, with many suggested fixes involving IP mappings. However, with recent (2022-ish) versions of wsl2 and redis image I had a similar issue but the fix was simple. I was using docker-compose and skeleton setup was

services:
  redis:
    image: redis:7.0
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data

but, as copy and paste from a non-WSL docker environment, I also had

    network_mode: host

and that was preventing me from connecting from the Windows host to the container running under WSL. As soon as I removed that line, docker created a default network for the service and I could connect (using 127.0.0.1:6379).

mdisibio
  • 3,148
  • 31
  • 47
0

If you are using WSL2 from Windows and running redis in Docker or Podman,

  • Get and run the podman container (replace with docker if docker):

    podman run -d --rm --name redis_server -v redis-data:/var/redis/data -p 6379:6379 redis

The above command creates and mounts persistent volume called redis-data (if doesn't exist) and expose it over port 6379

  • Get the ip address in your WSL2 Ubuntu/Pengwin using ifconfig -a, for example.
  • Use the IP and port to connect to redis-insight from your Windows machine

enter image description here

Karthik R
  • 5,523
  • 2
  • 18
  • 30