install Redis using docker

How to quickly install Redis using docker in 5 min [2022]

Redis stands for Remote Dictionary Server, which is a No-SQL database. Redis is a critical middleware and used by most of the fortune 500 companies. This blog will see learn how to install Redis using docker and will go over:

  • what is Redis
  • How to install Redis.
  • what is Redis CLI?
  • Important redis commands
  • Advantage of Redis
  • Few extra tips

To follow along, I assume that you have docker installed in your system; if not, follow this link to install docker.

What is redis?

Redis is an open-source, in-memory key-value data store. Redis is very fast and can be used as a cache, database, queue, and message broker. In most cases, people use Redis as an in-memory dataset. Based on your use case, you can persist your data periodically dump to a disk. We can use Redis from most of the programming languages.

Redis is so fast because it will save your data in memory, eliminating the time to access disks.

Now we have a basic understanding of Redis. Let’s proceed further and install Redis on your system.

Install redis using docker

Install redis using docker

There are many options available to install Redis in your system, but you should always go with docker if you are looking for a quick solution. This tutorial will explain how to install Redis in your system using docker.

Let’s verify if docker is running in your system.

➜  ~ docker --version
Docker version 20.10.3, build 48d30b5

Now pull the redis docker image from dockerhub by typing the below command

docker pull redis
Using default tag: latest
latest: Pulling from library/redis
69692152171a: Already exists
a4a46f2fd7e0: Already exists
bcdf6fddc3bd: Already exists
2902e41faefa: Pull complete
df3e1d63cdb1: Pull complete
fa57f005a60d: Pull complete
Digest: sha256:7e2c6181ad5c425443b56c7c73a9cd6df24a122345847d1ea9bb86a5afc76325
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

Let’s verify if the image gets successfully pulled

➜ docker images
REPOSITORY                    TAG        IMAGE ID       CREATED        SIZE
redis                         latest     fad0ee7e917a   2 days ago     105MB

Now let’s deploy the Redis container by typing below command

docker run -d --name my-redis redis:latest
a924225f4a1ad768024d0c1df5b12c7757b9b26c8f33ba3ed5e0010a0a34fac6

Let’s verify if the container gets created

➜ docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                PORTS                              NAMES
a924225f4a1a   redis:latest           "docker-entrypoint.s…"   41 seconds ago   Up 39 seconds         6379/tcp                           my-redis

Fantastic, the Redis container is running fine. Now let’s get inside the Redis container to access Redis.

docker exec -it my-redis /bin/bash
[email protected]:/data#

Now let’s Go inside the Redis CLI to confirm that our Redis installation is running OK.

[email protected]:~# redis-cli
127.0.0.1:6379>

To come out of Redis CLI, type the below command.

127.0.0.1:6379> exit
[email protected]:~#

Congratulation, you have successfully installed Redis on your system. Now let’s proceed further and learn about the Redis CLI.

Install redis on centOS 7

If you don’t want to install redis as the docker container, then you can follow this tutorial to install redis on centos 7. By default, redis comes with the centos yum package manager. You can type the below command to install redis in centos.

yum install redis

In case if redis is not present in the yum package manager, you can follow my other detailed blog on how to set up redis on centos 7 to get a detailed explanation.

What is Redis CLI?

Redis-cli is the Redis command-line interface that allows you to send commands to Redis and read the response directly in the terminal.

By default redis-cli connects to the 127.0.0.1 and port 6379. You can easily change this by using the below command:

[email protected]:~# redis-cli -h <host> -p <port>

Now let’s proceed further and see some of the helpful Redis commands

Redis Commands

Redis commands

Redis has a rich CLI to type your queries and get the output. In this tutorial, we will see some of the basic Redis commands. So let’s get started.

To get Redis documentation

Check the Redis CLI documentation by typing.

[email protected]:~# redis-cli --help
redis-cli 6.2.4

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
                     You can also use the REDISCLI_AUTH environment
                     variable to pass this password more safely
...................<suppressed output>............
...................<suppressed output>............
Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --quoted-input set '"null-\x00-separated"' value
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'

  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)

When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands
and settings.

Check if redis server is running

Connect to the Redis-CLI by typing the Redis-CLI command and then type the ping command to verify if the redis servers are running.

127.0.0.1:6379> ping
PONG

To get Redis configuration

We can easily fetch all the configurations present in our redis instance with the config command. Let’s fetch all the redis configs.

127.0.0.1:6379> CONFIG GET *
  1) "rdbchecksum"
  2) "yes"
  3) "daemonize"
  4) "no"
  5) "io-threads-do-reads"
  6) "no"
  7) "lua-replicate-commands"
  8) "yes"
........<supressed o/p>...........
........<supressed o/p>...........
 331) "notify-keyspace-events"
 332) ""
 333) "bind"
 334) ""
 335) "oom-score-adj-values"
 336) "0 200 800"

If you wish to fetch any particular config, you can quickly get that by typing property name after config

127.0.0.1:6379> config get tls-replication
1) "tls-replication"
2) "no"

Insert data into redis

with the set command; we can insert data into the redis server. Let’s run this command and verify the outcome.

127.0.0.1:6379> set my_key my_value
OK
127.0.0.1:6379>
127.0.0.1:6379> set my_key2 my_value2
OK

Retrieve data from Redis

We can easily retrieve data from the redis server using the get command.

127.0.0.1:6379> get my_key
"my_value"

Get all keys in redis

If you wish to get all the keys present in your redis instance, type the below command to retrieve all keys.

127.0.0.1:6379> KEYS *
1) "my_key"
2) "my_key2"

Retrieve key by regular expression

You can also retrieve the keys present in the redis server by typing the keys <REGX> command.

127.0.0.1:6379> KEYS *key2*
1) "my_key2"

Delete a key in redis

Deleting a key in redis is relatively easy; type the del command followed by key name

127.0.0.1:6379> Del my_key2
(integer) 1

Verify if the key exists in redis

With EXISTS command, we can easily verify if the key is present in redis or not.

127.0.0.1:6379> keys *
1) "name"
2) "my_key"
3) "hello"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> EXISTS name
(integer) 1
127.0.0.1:6379>

SETNX command in redis

Using the SETNX command, the user can set the key if the key does not exist. If the key exists SETNX command will not replace the old values saved in the key. Let’s see this with a quick example

Using normal set command. Let’s insert data into the redis server.

127.0.0.1:6379> set key_test1 value_test_set1
OK
127.0.0.1:6379> get key_test1
"value_test_set1"

Now let’s override the data by again setting up the value of the same key

127.0.0.1:6379> set key_test1 value_test_set2
OK
127.0.0.1:6379> get key_test1
"value_test_set2"

As you can see, with the set command, we can override the existing data. Now let’s again try to set the key with the SETNX command

127.0.0.1:6379> setnx key_test1 value_test_set1
(integer) 0
127.0.0.1:6379> get key_test1
"value_test_set2"

As you can see, we got (integer) 0 as the response, which means the value of the key is safe and not overwritten.

Let’s try to set a key that does not exist by the setnx command

127.0.0.1:6379> setnx key_test3 value_test_set3
(integer) 1

we got (integer) 1 in the output it means the key was successfully set.

MSET and MGET command in redis

With MSET, we can set multiple keys in redis, and by using MGET, we can get multiple keys. These commands are helpful if you wish to set multiple values in 1 go.

Let’s set some keys

127.0.0.1:6379> MSET key1 value1 key2 value2 key3 value3
OK

Let’s try to retrieve all keys which we have set.

127.0.0.1:6379> MGET key1 key2 key3
1) "value1"
2) "value2"
3) "value3"

INCR and DECR redis commands

Using the INCR command, you can increase the value of a key by +1, and by DECR, you can decrease the value by -1. Let’s see both the command in action.

Let’s store some data in redis

127.0.0.1:6379> mset key_incr1 10  key_incr2 3  key_incr3 6
OK
127.0.0.1:6379>
127.0.0.1:6379> mget key_incr1 key_incr2 key_incr3
1) "10"
2) "3"
3) "6"
127.0.0.1:6379>

Let’s use INCR and DECR to increase and decrease the value

127.0.0.1:6379> incr key_incr1
(integer) 11
127.0.0.1:6379> incr key_incr2
(integer) 4
127.0.0.1:6379> decr key_incr3
(integer) 5
127.0.0.1:6379> mget key_incr1 key_incr2 key_incr3
1) "11"
2) "4"
3) "5"

EXPIRE command in redis

with EXPIRE command; we can set an expiration timer to a key. This command is handy if you want keys to be deleted at a particular time.

127.0.0.1:6379> SET rupee 1000
OK
127.0.0.1:6379> expire rupee 100
(integer) 1

rupee key will expire in 100 seconds.

TTL command in redis

With TTL command display the time in which key will expire

127.0.0.1:6379> TTL rupee
(integer) 84
127.0.0.1:6379> TTL rupee
(integer) 83
127.0.0.1:6379> TTL rupee
(integer) 81

As you can see, the time is continuously decreasing.

Now we have a basic understanding of a few redis commands, let’s proceed further and see some of the advantages of redis.

Advantage of Redis

  • Redis is blazing fast with average read or write operations taking less than a millisecond it is because Redis saves the data into memory.
  • We can set up Redis to replicate data in multiple servers which enables high availability and fast data retrieval.
  • Redis can be easily scalable by adding more servers based on your requirement.
  • There is wide community support available for Redis.

Redis Data Types

Redis not only saves a plain key-value store but also supports different kinds of values. Some of the types of the value that redis supports are:-

  • Binary-safe strings
  • Lists
  • Sets
  • Sorted Sets
  • Hashes
  • Bit array
  • HyperLogLogs
  • Streams

More detailed information about the redis data type can be found here.

Conclusion

We have learned how to deploy a Redis instance within a Docker container. We also learned how to use the Redis-CLI, and we have seen some of the Redis commands. I hope you like this tutorial. If so, please do let me know in the comment box.

More to Read?

Docker layers

Which docker base image to use

What docker images are available

Leave a Comment

Your email address will not be published.

Scroll to Top