Raphael De Lio

Software Engineer

How to Run Redis Locally on a Docker Container and Manage it with Redis Insight

My journey with Redis started one story ago. In my previous story, I discussed 10 things you didn’t know about Redis and listed many of its powerful capabilities most people are unaware of.

I’m very interested in understanding and experimenting with Redis’ capabilities. This is another story of a series where I am documenting my experience with Redis. Join me in this adventure. 🙂

In this tutorial, you will learn how to run Redis locally alongside Redis Insight to manage its data and how to perform simple CRUD operations.

In addition, we will also see how we can perform the same operations using Redis CLI and how to connect to it from within the container.

Let’s put our hands in the fire!

Running Redis Server and Redis Insight

Starting a Redis Server locally is very easy, and running it with Redis Insight is even easier! For running them, we are going to use Docker and although we’re running both applications, I will leave the command to run only the server as well:

Only Redis Server

docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest

Redis Server + Redis Insight

docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest

We will see the image being downloaded, and when it finishes, we can see that the container is running by executing docker ps in our terminal and that it’s exposing ports 6379 and 8001 as defined by -p 6739:6379 -p 8001:8001:

The image shows terminal output where a Redis Stack Server Docker container is being pulled and started. Here’s a step-by-step breakdown:
	1.	docker run Command Execution:
A docker run command is executed to create and start a container named redis-stack-server with the Redis Stack image and bind port 6379 from the host to the container.

docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest


	2.	Image Pull:
Since the image redis/redis-stack-server:latest was not found locally, Docker begins pulling the image layers:
	•	Various layers are shown being downloaded and marked as Pull complete.
	3.	Image Download Complete:
The image is successfully downloaded, and the digest for the pulled image is displayed:

sha256:6b590ce48ac98cce6253a721f99ab13786399f43934dc6783a1b744765b95729


	4.	Container Status:
After the container starts, the docker ps command is executed, which lists running containers. The output shows:
	•	Container ID: 9c459e469cdc
	•	Image: redis/redis-stack-server:latest
	•	Command: /entrypoint.sh
	•	Created: 9 seconds ago
	•	Status: Up 7 seconds
	•	Ports: Port 6379 on all interfaces (0.0.0.0) is mapped to the container’s 6379/tcp port.

This indicates that the Redis Stack Server container is up and running successfully.

Redis Insight

6379 is the port of our server and 8001 is the port of Redis Insight. We can connect to it by accessing localhost:8001

If we access it, we should see something like:

Redis Stack Database UI showing an empty key list. The left panel displays ‘No keys to display’, while the right panel prompts to select a key for details. The interface includes options to add a new key, search for keys, and monitor database metrics such as CPU and memory usage

On the top right, you can see a few stats of the database. On the top left you can check your database details and in the center left you can see your list of key values and on the center-right, you can see the details of these key values.

Remembering that conceptually, Redis is based on the key-value database paradigm. Every piece of data is associated with a key, either directly or indirectly.

Adding a new key-value

To add a new key value, we need to click on the +key button.

Our first key is going to be of type hash. The TTL, which is the expiration for the key, is gonna be empty, which means it will never expire.

The key name is going to be RaphaelDeLio and then we are going to add some values associated with that key:

Redis Stack UI showing a ‘New Key’ creation dialog. The key type is set to ‘Hash’ with no TTL limit. The key name is ‘RaphaelDeLio’ and it contains four fields: name (Raphael), lastName (De Lio), age (27), and job (Software Developer). Options to delete or add fields are available, with ‘Add Key’ and ‘Cancel’ buttons at the bottom.

We can click on Add Key and we will see our key in the key list and the fields associated with it:

Redis Stack UI displaying a hash key named ‘RaphaelDeLio’. The key has four fields: name (Raphael), lastName (De Lio), age (27), and job (Software Developer). The left panel lists the hash key with details such as ‘No limit’ TTL and size of 133 B, while the right panel shows field-value pairs with options to edit or delete each field.

You can edit or delete any field, or even the key itself, by clicking on the pencils or bins next to them.

Redis Stack UI showing a confirmation dialog to remove the ‘age’ field from the hash key ‘RaphaelDeLio’. The dialog includes a red ‘Remove’ button with a trash can icon.

Redis CLI

Connecting to Redis CLI

In order to send commands to Redis, we need to connect to Redis CLI. Redis CLI is installed within the container, and we can access it by running the following command in our terminal:

docker exec -it redis-stack redis-cli

Adding a new key

To add a new key, we are going to use the HSET command. This command is specific for setting keys of type hash, each type has a different prefix, such as LSET for listSET for string, among others we will see later in this story.

In order to add the same object we added with Redis Insight, we are going to run the following command:

HSET RaphaelDeLio name "Raphael" lastName "De Lio" age 27 job "Software Developer"

As a response, Redis should return the number of fields set in the key:

(integer) 4

Retrieving a key

There’s also a command for getting each type of key and they follow the same pattern we saw before.

We will cover two ways of retrieving hash keys, one is for getting all fields of this hash. We can do it by running:

HGETALL RaphaelDeLio

And we should see a response like:

1) "name"
2) "Raphael"
3) "lastName"
4) "De Lio"
5) "age"
6) "27"
7) "job"
8) "Software Developer"

And the second, is by retrieving an specific field, by running:

HGET RaphaelDeLio job

Which will return:

"Software Developer"

Editing a field or adding a new one

Editing a field or adding a new one is done by the same set command HSET, we can do it by running:

HSET RaphaelDeLio newKey "This is a new Key"
HSET RaphaelDeLio job "I'm editing an existing key!"

And you can test them, by running the GET commands we saw before.

Deleting a field or a key

To delete a hash field, you just need to run the HDEL command, as in:

HDEL RaphaelDeLio job

And to delete the key, you run:

DEL RaphaelDeLio

Disconnecting from Redis CLI

To disconnect from Redis CLI just press ctrl + c

What about the other types?

Hashes

Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (e.g. A User with a number of fields like name, surname, age, and so forth).

Every hash can store up to 2³² — 1 field-value pairs (more than 4 billion).

Strings

Strings are the most basic kind of Redis value. Redis Strings are binary safe, this means that a Redis string can contain any kind of data, for instance, a JPEG image or a serialized Ruby object.

A String value can be at a max of 512 Megabytes in length.

Commands for Strings:

  • SET: To add or edit a key
  • GET: To retrieve a key
  • DEL: To delete a key

Lists

Redis Lists are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis List by pushing new elements on the head (on the left) or on the tail (on the right) of the list.

Redis Stack UI displaying a list key named ‘StringList’. The list contains one element at index 0 with the value ‘This is my first string element’. The top panel shows key size (172 B), length (1), and TTL as ‘No limit’. An ‘Add Element’ button is available for adding more elements.
Redis Stack UI showing the addition of a new element to a list. The input box contains ‘This is gonna be my second element’. A dropdown menu offers two options: ‘Push to tail’ (selected) and ‘Push to head’. There are ‘Cancel’ and ‘Save’ buttons at the bottom.

Sets

Redis Sets are an unordered collection of Strings. It is possible to add, remove, and test for the existence of members in O(1).

Redis Sets have the desirable property of not allowing repeated members. Adding the same element multiple times will result in a set having a single copy of this element. Practically speaking this means that adding a member does not require a check if exists then add operation.

Redis Stack UI showing a ‘New Key’ creation dialog with the key type set to ‘Set’. The key name is ‘Set1’, and three members, all labeled ‘example’, are added. Options to delete or add members are available, with no TTL limit specified.

Commands:

  • SADD: Adds one member to a set
  • SREM: Removes one member from a set
  • SMEMBERS: List all members from a set

Sorted Sets

Redis Sorted Sets are, similarly to Redis Sets, non-repeating collections of Strings. The difference is that every member of a Sorted Set is associated with a score, that is used to keep the Sorted Set in order, from the smallest to the greatest score. While members are unique, scores may be repeated.

Redis Stack UI displaying a sorted set key named ‘SortedSet1’. The set contains four members: ‘First’ with score 1, ‘Second’ with score 2, ‘Second2’ with score 2, and ‘Third (Or fourth?)’ with score 3. Options to edit or delete each member are available, along with an ‘Add Members’ button at the top-right.

Commands:

  • ZADD: To add new members
  • ZREM: To remove existing members
  • ZRANGE: To list members by range

Streams

A Redis stream is a data structure that acts like an append-only log. Streams are useful for recording events in the order they occur.

For Streams, we need to provide an entry key, which can be a timestamp or sequence number. If we leave it as *, Redis will assign the Database timestamp to it.

Redis Stack UI displaying the creation of a new Stream key. The key name is ‘Stream1’, with no TTL limit set. The Entry ID is specified as ‘*’, and a field named ‘AirQuality’ with a value of ‘0’ is being added. Options to delete or add more fields are available.
Redis Stack UI displaying a stream key named ‘Stream1’. The stream contains three entries with unique Entry IDs and corresponding ‘AirQuality’ values: 4, 5, and 0. The key size is 661 B, entries total 3, and TTL is set to ‘No limit’. Options to delete individual entries and a ‘New Entry’ button are visible at the top-right.

Commands:

  • XADD: To append a new entry to a stream
  • XRANGE: List all entries matching a range of IDs
  • XDEL: Removes an entry from a stream

JSON

RedisJSON is part of the JSON module and allows you to add JSON objects as the value for your keys.

Redis Stack UI displaying a JSON key named ‘JSON1’. The JSON content shows a single field, ‘name’, with the value ‘Raphael De Lio’. The key size is 66 B, length is 1, and TTL is set to ‘No limit’. Options to refresh or delete the key are available.

Commands:

  • JSON.SET: To add or replace a new JSON or field in an existing JSON
  • JSON.DEL: To delete a JSON or field in a JSON
  • JSON.GET: To retrieve a JSON

Conclusion

In this story, you learned how to start a Redis Server and Redis Insight locally. You also learned about different types of keys supported by Redis and also how to perform simple CRUD operations with Redis Insight and Redis CLI.

I hope you have enjoyed this story. I will be diving more into Redis on the following stories, so don’t forget to follow me and subscribe to my next stories!

Leave a Reply