Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Redis running in Docker container via CLI

Tags:

python

redis

docker-compose

web:
  container_name: authserver
  restart: always
  build: ./authserver
  expose:
    - "8000"
  links:
    - redis:redis
  environment:
    DEBUG: 'true'

redis:
  restart: always
  image: redis:latest
  ports:
    - "6379:6379"

docker inspect authserver_redis_1

    "Gateway": "172.17.0.1",
    "IPAddress": "172.17.0.3",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "02:42:ac:11:00:03"

From the Python interpreter on my host machine (meaning from where I run docker-compose itself) I can play around with Redis and get/set values

client = redis.StrictRedis(host='172.17.0.3', port=6379, db=0)

client.set("key01", "value01")
print client.get("key01")
>>>>"value01"

The issue I'm having is that if I run redis-cli from the command line

redis-cli -h 172.17.0.3 -p 6379

on the same host machine (the same machine I'm interacting with the Python interpreter on) and then run HGETALL *, I expect to have key01 and value01 returned, but instead it returns

(empty list or set)

I had a hard time following both the redis-cli and python redis library docs, so I'm guessing I did some things wrong.

like image 635
david Avatar asked Feb 21 '17 23:02

david


1 Answers

ports:
    - "6379:6379"

This forwards redis from the container to your host, so you should just be able to connect to localhost as if redis was running directly your machine. If this doesn't work, you can do more digging by running

docker ps

to get a list of running container IDs, and then

docker exec -it <redis container ID> redis-cli

to connect directly to your redis container and run redis-cli.

like image 158
Chris Tanner Avatar answered Sep 24 '22 23:09

Chris Tanner