Installing Redis on Docker

Create a Redis key-value store Docker container.


Redis is an open source key-value store that functions as a data structure server. Its Docker image happens to be one of the easiest to install and get up and running.

The following demonstrates how to install and test Redis on Docker.


You may need to prefix the following commands with sudo, or run sudo -i to run as root

Open a command / terminal and run the following:

docker run --name my-redis redis

Docker will check if you already have the redis image downloaded, otherwise it will download and install it. You should see something like the following:

root@ubuntu:~# docker run --name my-redis redis
Unable to find image 'redis:latest' locally
latest: Pulling from library/redis
8ec398bc0356: Pull complete
da01136793fa: Pull complete
cf1486a2c0b8: Pull complete
a44f7da98d9e: Pull complete
c677fde73875: Pull complete
727f8da63ac2: Pull complete
Digest: sha256:90d44d431229683cadd75274e6fcb22c3e0396d149a8f8b7da9925021ee75c30
Status: Downloaded newer image for redis:latest
1:C 12 Jan 2020 02:19:37.342 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 12 Jan 2020 02:19:37.342 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 12 Jan 2020 02:19:37.342 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 12 Jan 2020 02:19:37.343 * Running mode=standalone, port=6379.
1:M 12 Jan 2020 02:19:37.343 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 12 Jan 2020 02:19:37.343 # Server initialized
1:M 12 Jan 2020 02:19:37.343 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 12 Jan 2020 02:19:37.343 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 12 Jan 2020 02:19:37.343 * Ready to accept connections

At this point Redis is running and ready to accept local connections. However, for testing purposes, this is not really convenient because Redis is running inside a container, and we're outside of the container, so we're unable immediately interact with it.

What we can do is run the container in the background (detached) and then execute commands against it, such as docker-cli to tests its functionality.

Run container detached

First stop the container by typing ctrl-c and then run docker ps -a to see a list of all containers. The Redis container should show as stopped. Now start the container with docker start my-redis. It should now be running in detached state.

You can default to running containers as detached with -d option. Example: docker run -d --name my-redis redis
root@ubuntu:~# docker start my-redis

Execute commands

We can now attach to the running container so that we can execute commands. Type docker exec -it my-redis bash. This will connect to the Redis container and execute an interactive bash shell.

root@ubuntu:~# docker exec -it my-redis bash

Redis CLI

You should see the prompt has now changed to indicate you're connected to the container. Type redis-cli to start the Redis CLI tool, and enter a redis command such as keys * to show all keys in the database.

root@3e5e9c65c2fd:/data# redis-cli> keys *
(empty list or set)>

Now create some data.> set my-key test
OK> get my-key
"test"> incr my-sequence
(integer) 1> incr my-sequence
(integer) 2> incr my-sequence
(integer) 3> decr my-sequence
(integer) 2> keys *
1) "my-sequence"
2) "my-key">

Now type exit to exit the cli tool, and exit again to exit the bash shell.> exit
root@3e5e9c65c2fd:/data# exit

Connect to Redis from a remote server

So far our Redis container is only accepting local connections. We had to run the redis-cli tool from within the container in order to connect to Redis server. We'll now set-up the Redis container to accept remote connections.

We'll need to stop and remove the container first. Type docker stop my-redis followed by docker rm my-redis. Check to see the container has been removed by typing docker ps -a. Redis should no longer show in the list.

root@ubuntu:~# docker stop my-redis
root@ubuntu:~# docker rm my-redis
root@ubuntu:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS

Now we'll run a new container as detached with -d option and also use the -p option to publish the redis port to the host.

root@ubuntu:~# docker run  -d -p 6379:6379 --name my-redis redis
root@ubuntu:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS
220b2f1d53cd        redis               "docker-entrypoint.s…"   10 seconds ago      Up 9 seconds>6379/tcp   my-redis

Running docker ps should show us the new Redis container and also show us the port mapping:>6379/tcp which tells us that the container's TCP port 6379 has been mapped to host TCP port also 6379. Now we can connect to Redis outside of the container.

Run Redis CLI on host

Install Redis CLI on the host to interact with Redis running in the container.

apt-get install redis-tools

As before, launch CLI tool with redis-cli and run some Redis commands.

root@ubuntu:~# redis-cli> keys *
(empty list or set)> set name Neil
OK> get name
"Neil"> incr seq
(integer) 1> incr seq
(integer) 2> decr seq
(integer) 1> keys *
1) "seq"
2) "name">

Redis is now available for use in your applications.

Useful links

Redis Docker hub
Redis docs
Docker CLI docs