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

Tagged as



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.


Since we will use Docker Compose to create the Redis instance, you must first install docker. Check out 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'
    image: redis
    command: redis-server /usr/local/etc/redis/redis.conf
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - "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
Before I could connect to the Redis instance from Python, I had to comment the following line in the config:


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 file to the directory we created before:

import redis

# connect to Redis
server = redis.Redis(host="", port=6379)
# should return True

# should return [] since we haven't added any keys yet

# should return nothing since we haven't added the key yet

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

# should return [b'MyKey']

# should return "b'I love Python'"

# should return 1 as success code

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


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


About the Author

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

Comments and Discussions

-- There are no messages in this forum --