Skip to content

Instantly share code, notes, and snippets.

@osule
Last active September 21, 2020 07:49
Show Gist options
  • Save osule/ad17c5cc54e2819d028baead660f1000 to your computer and use it in GitHub Desktop.
Save osule/ad17c5cc54e2819d028baead660f1000 to your computer and use it in GitHub Desktop.
Experimenting with redis python client

I enrolled in the just concluded Redislabs course.

I didn't want to install redis locally because of storage contraints on my computer for the assignments and exams, and set to doing it all using Docker containers.

I document the steps for my setup for the courseware. The course also provides this image for the courseware on Docker Hub.

Set up Redis server

Start up server using host network mode.

Redis listens on port 6379 on the host.

docker run --rm --name redis --network host redis:latest

Test connection to the redis server using the Redis CLI client

docker run --rm -it --network host redis redis-cli -p 6379

Working in the notebook

Start jupyter notebook.

docker run --rm -it -p 127.0.0.1:8888:8888 jupyter/base-notebook start.sh jupyter notebook --ip='*' --port=8888

You must pass the ip option as a wildcard. Otherwise the notebook server would default to listening on localhost. That's documented here.

Obtain network gateway ip address.

This is needed to have the notebook connect to services running external to the notebook container. redis is one example in this case.

docker inspect $(docker ps -f "ancestor=jupyter/base-notebook" -f "publish=8888" --format "{{.ID}}") --format "{{.NetworkSettings.Networks.bridge.Gateway}}"

Create a new notebook file and install redis

pip install redis

In a new notebook cell, connect to redis

import redis

key = 'foo'
client = redis.Redis('replace-with-network-gateway-ip-address')
client.set(key, 1.5)
result = client.get(key)

Cleanup

Exit the running containers with key combination Ctrl+C.

Then cleanup the images.

docker rmi jupyter/base-notebook redis

Have fun with redis!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment