Click here to Skip to main content
14,975,219 members
Articles / Containers / Docker
Technical Blog
Posted 4 Jan 2018

Tagged as

Stats

9.3K views

Setting Up a Redis Test Environment using Docker Compose

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Jan 2018CPOL2 min read
In this short tutorial, I want to show you how to set up a simple Redis instance using Docker Compose. We will connect to it and test it using Python and the redis package.

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:

Bash
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:

Python
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:

Python
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:

Python
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:

Python
import redis

# connect to Redis
server = redis.Redis(host="127.0.0.1", port=6379)

server.ping()
# should return True

server.keys()
# should return [] since we haven't added any keys yet

server.get('MyKey')
# should return nothing since we haven't added the key yet

server.set('MyKey', 'I love Python')
# should return True

server.keys()
# should return [b'MyKey']

server.get('MyKey')
# should return "b'I love Python'"

server.delete('MyKey')
# should return 1 as success code

server.get('MyKey')
# should return nothing because we just deleted the key

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Philipp_Engelmann
Software Developer (Senior)
Germany Germany
Hi there 🙂
My name is Philipp Engelmann, I work as a web developer at FIO SYSTEMS AG in Leipzig. I am interested in C#, Python, (REST-)API-Design, software architecture, algorithms and AI. Check out my blog at https://cheesyprogrammer.com/

Comments and Discussions

 
-- There are no messages in this forum --