How to Install Redis on Ubuntu using Docker
Introduction
In this demonstration we’ll be installing Redis on Ubuntu using Docker. This will be a step by step walk through with the commands you’ll need to follow along. If you’ve been wanting to learn Redis and play around with it’s syntax,commands, and capabilities, installing a Docker container with Redis is on it is a good way to go. You can play with it as much as you’d like then kill the container when you’re done.
Prerequisities
- You should have Docker installed on your computer whether it’s Mac, PC, or Linux Note: We’re doing the demo on a Mac with OSX but since we’re using Docker you should have the same process as long as you have Docker up and running.
How To Install Redis on Ubuntu using Docker
Create an ubuntu container
Our first step is to create an ubuntue docker container. We can do so by running this command:
1 | docker run -it ubuntu /bin/bash |
This creates an instance of the ubuntu docker image locally on our machine. You can check see all the containers on your machine by using this command:
1 | docker ps |
We can see we have a docker container with the id fe431bc3c9c2
made from the image ubuntu
and its randomly assigned name is silly_rubin
.
1 | $ docker ps |
This will give you a table similar to this (we’ve reformatted it to fit):
CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES |
---|---|---|---|---|---|---|
fe431bc3c9c2 | ubuntu | “/bin/bash” | 17 hours ago | Up 17 hours | silly_rubin |
What is the -ti flag?
* This gives us an interactive shell into the docker container. The terminal will act as if you are inside the shell of your silly_rubin
docker container.
Notice that your shell now looks different than before:
1 | root@fe431bc3c9c2:/# |
This is the shell for your new container which we’ll install Redis in.
Update and upgrade apt-get
Our next step is to update and upgrade the package manager apt-get so that we can download the latest Redis package.
Let’s first update apt-get with the following command:
1 | root@fe431bc3c9c2:/# apt-get update |
When that’s finished we’ll run the upgrade:
1 | root@fe431bc3c9c2:/# apt-get upgrade |
Install Redis using the apt-get Package Manager
Now we’ll use apt-get to install Redis. The package name that we’re looking for is redis-server
. Run the following command to install it:
1 | root@fe431bc3c9c2:/# apt-get install redis-server |
You now have Redis installed!
Make a copy of the config file just in case
It’s good practice to make a copy of the config file so you have the original in case you mess anything up. The file is called redis.conf
and it’s full location is etc/redis/redis.conf
. We’ll make a copy named redis.conf.default
and leave it in the same folder with this command:
1 | root@fe431bc3c9c2:/# cp etc/redis/redis.conf etc/redis/redis.conf.default |
Start Redis
Now we will start Redis! Note: It will take over this terminal but we’ll continue in another terminal.
Start Redis with the following command:
1 | root@fe431bc3c9c2:/# redis-server |
You’ll receive an output that looks similar to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 804:C 08 Jul 22:32:27.220 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 804:C 08 Jul 22:32:27.220 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=804, just started 804:C 08 Jul 22:32:27.220 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.9 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 804 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 804:M 08 Jul 22:32:27.222 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 804:M 08 Jul 22:32:27.222 # Server initialized 804:M 08 Jul 22:32:27.223 # 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. 804:M 08 Jul 22:32:27.223 * Ready to accept connections |
You can see next to the logo that it gives you the port number Port: 6379
and at the very end says Ready to accept connections
.
Verify it’s working
Now to verify that Redis is working we’ll issue a few Redis commands. Since it’s taken over this terminal, open up a new one . When you open up a new terminal though you are no longer in your container but you can execute a command that will get you into it.
In the new terminal use this docker command to get into your container’s terminal. ( You’ll need to replace silly_rubin
with your container name ):
1 | docker exec -it silly_rubin bash |
You should see the container prompt again that looks something like this:
1 | root@fe431bc3c9c2:/# |
Run the Redis CLI ( Command Line Interface )
Now we can work directly with Redis by running the Redis Command Line Interface. To start it run this command:
1 | root@fe431bc3c9c2:/# redis-cli |
You should see a prompt like this:
1 | 127.0.0.1:6379> |
First we’ll do the most basic Redis command just to verify it’s working. If we execute the ping
command it should respond with “PONG”. Let’s try it:
1 2 | 127.0.0.1:6379> ping PONG |
It worked!
Now try setting a key value pair. We’ll use the key “foo” and the value of 100.
1 2 3 4 | 127.0.0.1:6379> SET foo 100 `` We've set the value and we can get it like this: |
127.0.0.1:6379> GET foo
`
Great! We’ve verified it’s working!
Conclusion
We’ve gone through the steps of installing Redis on Ubuntu using Docker. If you’re already on a Ubuntu system then of course you don’t need the Docker part of this tutorial but if you’re running OSX or Windows then you’ll probably find the Docker implementation very helpful. With Redis installed you can now explore its commands and functionality. What we showed here is just for educational purposes and shouldn’t be used in a production environment. If you’re looking to use Redis in production please reach out to us at Object Rocket and we can connect you with an expert who’ll be more than glad to help you.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started