Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to Redis running in Docker Container from Host machine

Tags:

docker

redis

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

like image 593
user3888307 Avatar asked Dec 29 '16 00:12

user3888307


People also ask

How do I connect to host from inside container?

Accessing the Host With the Default Bridge Mode You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

Can docker container communicate with host?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0. 0.1 ) will refer to the docker host.


2 Answers

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' 
like image 199
Farhad Farahi Avatar answered Oct 21 '22 02:10

Farhad Farahi


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

like image 28
Brian Ocampo Avatar answered Oct 21 '22 01:10

Brian Ocampo