Software
Since we will use Docker Compose to create the Redis instance, you must first install docker
. Check out https://docs.docker.com/engine/installation for further information.
To run the Python code, you have to first install Python and the pip package manager.
To download the PyPi package for Redis, open a terminal and type:
sudo pip install redis
Setting up Redis
Docker compose makes it very easy to set up and run a Redis instance. We will directly use the redis
image from docker-hub.
To configure the Docker image, create a new directory for this project and add the following docker-compose.yml file to it:
version: '2'
services:
redis:
image: redis
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./redis.conf:/usr/local/etc/redis/redis.conf
ports:
- "6379:6379"
As mentioned above, we use the redis
image from docker hub directly. In the command
section, we tell Docker to startup a redis server using the config file from /usr/local/etc/redis. To configure Redis, I added a volume that points to a custom redis.conf file which will then be used by redis instead of the default config.
Configuring Redis
You can get the default redis.conf file from http://download.redis.io/redis-stable/redis.conf.
Before I could connect to the Redis
instance from Python, I had to comment the following line in the config:
bind 127.0.0.1
by adding a #
in front of it. For further information about Redis configuration, checkout its documentation.
Starting the Redis Server
With both the docker-compose.yml and redis.conf files set up, we can now use Docker Compose to startup the Redis server:
docker-compose up
If you run this command for the first time, docker has to pull the Redis image and will require some extra time to get started. If everything is set up correctly, you should see a couple of success messages from Redis, ending with something like Ready to accept connections
.
Connecting to Redis from Python
With Redis up and running, it’s time to send some commands to it. Since we will use a Python client, I added a redis-client.py file to the directory we created before:
import redis
server = redis.Redis(host="127.0.0.1", port=6379)
server.ping()
server.keys()
server.get('MyKey')
server.set('MyKey', 'I love Python')
server.keys()
server.get('MyKey')
server.delete('MyKey')
server.get('MyKey')
Here, we are simply adding and removing some keys to test the most basic Redis commands. Feel free to play around with this file and try out some more advanced tasks. After you are done testing, simply terminate the process that runs the Redis image. If you try to execute the Python code now, you should run into an error, since there is no Redis server listening on localhost on Port 6379.
That’s it for this quick introduction. While this example is very simplistic, you can easily build up from it by adjusting the redis.conf file or adding more services to the docker-compose.yml file. For example, you could set up a second service that starts up a web server which you can then, in turn, connect to the Redis instance.
If you have any questions, problems or feedback, please let me know.