Connecting to Redis running in Docker Container from Host machine

DockerRedis

Docker Problem Overview


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

Docker Solutions


Solution 1 - Docker

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'

Solution 2 - Docker

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

Solution 3 - Docker

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.

Solution 4 - Docker

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.

Solution 5 - Docker

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.

Solution 6 - Docker

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

Solution 7 - Docker

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3888307View Question on Stackoverflow
Solution 1 - DockerFarhad FarahiView Answer on Stackoverflow
Solution 2 - DockerBrian OcampoView Answer on Stackoverflow
Solution 3 - DockerDarekView Answer on Stackoverflow
Solution 4 - DockerRandy LermaView Answer on Stackoverflow
Solution 5 - DockermadhuView Answer on Stackoverflow
Solution 6 - DockerMargach ChrisView Answer on Stackoverflow
Solution 7 - DockermdisibioView Answer on Stackoverflow