Redis & Docker: A Hands On
Article Series
- A Fireside chat with Redis
- Redis & Docker: A Hands On (Current Blog)
- Getting our hands dirty with Redis
- Redis and My Personal IMDB Feat. Node.JS — Part I
- Redis and My Personal IMDB Feat. Node.JS — Part II
- Redis and My Personal IMDB Feat. Node.JS — Part III
- Redis and My Personal IMDB Feat. Node.JS — Part IV
- Redis and My Personal IMDB Feat. Node.JS — Part V
Now that we have a basic idea about Redis, it’s time to set it up.
There are two ways you can install docker on your machine.
- Installing Redis on a UNIX based machine (Linux, MacOS)
- Using Docker Desktop and containers (Windows, MacOS)
I prefer the second one, though the initial setup and learning curve is a little steep, I prefer to manage my tools via containers.
PS: One more reason to use Docker is that I use a Windows 10 machine and Redis is not supported for Windows (weeps!)
Do not worry, I will describe both ways in this blog. You can choose the one you consider easy.
Route 1: Install Redis on Linux
- Open the terminal to download a freshly brewed copy of the latest Redis release. Run the following command to download the archive file.
wget http://download.redis.io/redis-stable.tar.gz
2. Extract the Redis archive, move to the redis-stable directory and then compile Redis with make.
tar xvzf redis-stable.tar.gz // Unzips Redis
cd redis-stable // Moving into the directory
make // Compiling the Redis source
3. After the compilation, the src directory within Redis distribution will contain several new executables that are a part of Redis. A few below…
- redis-server: The Redis server itself. You will have to run this for a Redis instance to be up and running.
- redis-cli: The command line interface for interacting with Redis. You can run all Redis related commands here.
- redis-sentinel, redis-benchmark and a bunch of others: We will not be worrying about them for now. They are related to high availability, benchmarking and troubleshooting Redis. Those are way out of scope right now.
4. The most important ones for us would be redis-server and redis-cli, which we will be using extensively while learning Redis. So it’s better if we make them properly accessible.
To do that you can either,
- Copy redis-server and redis-cli, to /usr/local/bin, on an assumption that this location is added to your path variable. Once copied, you can run the commands from any location. Sounds cool right. To do that, run the following commands.
sudo cp src/redis-server /usr/local/bin/ // Copy redis-server
sudo cp src/redis-cli /usr/local/bin/ // Copy redis-cli
- Or simply, run the following command to take care of the above steps
sudo make install // Takes care of bunch of stuff
// to make Redis accessible.
5. That’s it. Redis is ready, all you need to do is start the server, open the cli and start getting your hands dirty.
To start the Redis Server, run
redis-server
Once the server is up, let’s connect to the server via the Redis command line interface or in short cli. To run the cli, use the following command
redis-cli
By default, redis-cli connects to the default Redis port 6379 on your local machine (localhost or 127.0.0.1).
Once connected to cli, type in “PING” and if all are good, Redis replies with “PONG”.
You can also set a simple String based key/value pair to test Redis, like in the below screen capture.
You can use “QUIT” to exit the cli.
Time to jump ship to help the Windows 10 people (like myself).
Route 2: Install Redis via Docker
Part 1: Installing Docker Desktop (supports only Windows 10)
There is no direct/easy way to install Redis on Windows. One way to experience the power of Redis is via Docker and it’s containers. Like I said before, though the initial setup is slightly complex, once you get used to Docker you would prefer it more.
PS: Docker method can also be used by MacOS users.
- Download the Docker Desktop app from Docker website.
2. You will have to create an Docker account before the download is available. Make a note of this account as you would be needing it during installation and setup.
3. The installation is straightforward. Double click the Docker Desktop Installer.exe from Downloads folder or wherever you had saved it during download.
4. Accept the license, authorize the installer and then proceed with the install. Mostly you wouldn’t need to fill any details, the defaults of the installer will do fine.
5. During installing you will be asked to authorize Docker.app. You will have to provide your password, if any, to authorize it. This helps Docker to install networking components and manage Hyper-V VMs.
6. After installation the app may ask you to enable Hyper-V, if not already enabled. Just click “Ok”. You don’t have to do anything here, the installer will take care of enabling it. Your PC may restart a few times during the process. Do not worry. It’s all good.
7. Once the installation is done, you can launch Docker from “Start”. It will take sometime during the first startup, as it needs to setup all possible initial settings.
8. Once the app is up and running, you will get a notification and you can also check it out in your Taskbar.
9. Now Docker will ask you to login. Remember the account we created in Step 2. Fill in those details here and you are gold.
10. Docker is all ready for some Redis action.
Part 2: Installing and Setting Up Redis on Docker
Time to get Redis on Docker.
- There is an official Docker Image for Redis @ Docker Hub.
- To use that, open the command prompt from start or via Run (Win + R).
3. Run the following command to install Redis on Docker
docker pull redis // Installs Redis on Docker
4. Next, start a Redis instance on Docker using the following command. The port has to mentioned twice like “port:port”, so you can connect from external apps (like the Node.js app we are going to build later).
docker run --name <container-name> -p <port>:<port> -d redis// Example: Setting name as harsh-redis and using the default Redis port
docker run --name harsh-redis -p 6379:6379 -d redis
To list all containers in Docker, you can use the following command
docker ps -a
5. To connect to the Redis CLI, run the following command
docker exec -it <container name> redis-cli// Example: Container Name - harsh-redis
docker exec -it harsh-redis redis-cli
Once connected, you can test Redis with “PING” command and if all good, it responds with “PONG”.
Create a sample key/value pair using “SET” and obtain it using “GET” for an additional confirmation.
Gotchas
Sometimes, if there is an improper shutdown, Docker may not start the containers properly. In such cases, you can trouble shoot with the following steps.
- Get the status of the Docker containers using the command
docker ps -a
2. You can see, the status of the container from the “STATUS” field.
Note: In the below screenshot, I have shutdown the container manually just for the case of troubleshooting.
3. Now, starting this container is very easy. Copy the container’s NAME from the above command’s output and run the following command to start it.
docker start -a <container name>
// For the above example, it would be
docker start -a harsh-redis
4. Redis is up. Let’s start hacking!
Now that we have setup Redis, it’s time to get our hands dirty. Next we are going to run a few commands on the Redis CLI and learn a few of the inbuilt data structures that Redis supports.
Join me in the next blog.