Install Redis On Docker

Installing and Running the Redis Container. Redis has an official container on DockerHub. This will work fine as is for my purposes. To pull the Redis container image, simply run the following in the terminal. Docker pull redis Once pulled, the image can be run via: docker run -p 127.0.0.1:6379: 6379 /tcp -name container-redis-test -d redis. It is simple to start using Redis using docker: docker pull redis docker run -p 6379:6379 -rm -name redis redis. Now you have running instance on port 6397. Attention: All data will be deleted, when Redis will be stopped. To connect the redis-cli, start another docker: docker run -it -link redis:redis -rm redis redis-cli -h redis -p 6379. Hussein Galal is a Linux System Administrator, with experience in Linux, Unix, Networking, and open source technologies like Nginx, Apache, PHP-FPM, Passenger, MySQL, LXC, and Docker. You can follow Hussein on Twitter @galalhussein. I recently used Docker and Rancher to set up a Redis cluster on Digital Ocean. Redis clustering provides a way to share.

While designed for web development, the PHP scripting language also provides general-purpose use. Redis is an open source key-value store that functions as a data structure server.

This is a quick start document that targets people without prior experience with Redis. Reading this document will help you:

  • Download and compile Redis to start hacking.
  • Use redis-cli to access the server.
  • Use Redis from your application.
  • Understand how Redis persistence works.
  • Install Redis more properly.
  • Find out what to read next to understand more about Redis.

The suggested way of installing Redis is compiling it from sources as Redis has no dependencies other than a working GCC compiler and libc. Installing it using the package manager of your Linux distribution is somewhat discouraged as usually the available version is not the latest.

You can either download the latest Redis tar ball from the redis.io web site, or you can alternatively use this special URL that always points to the latest stable Redis version, that is, http://download.redis.io/redis-stable.tar.gz.

In order to compile Redis follow these simple steps:

At this point you can test if your build has worked correctly by typing make test, but this is an optional step. After compilation the src directory inside the Redis distribution is populated with the different executables that are part of Redis:

  • redis-server is the Redis Server itself.
  • redis-sentinel is the Redis Sentinel executable (monitoring and failover).
  • redis-cli is the command line interface utility to talk with Redis.
  • redis-benchmark is used to check Redis performances.
  • redis-check-aof and redis-check-rdb (redis-check-dump in 3.0 and below) are useful in the rare event of corrupted data files.

It is a good idea to copy both the Redis server and the command line interface into the proper places, either manually using the following commands:

  • sudo cp src/redis-server /usr/local/bin/
  • sudo cp src/redis-cli /usr/local/bin/

Or just using sudo make install.

In the following documentation we assume that /usr/local/bin is in your PATH environment variable so that you can execute both the binaries without specifying the full path.

The simplest way to start the Redis server is just executing the redis-server binary without any argument.

In the above example Redis was started without any explicit configuration file, so all the parameters will use the internal default. This is perfectly fine if you are starting Redis just to play a bit with it or for development, but for production environments you should use a configuration file.

In order to start Redis with a configuration file use the full path of the configuration file as first argument, like in the following example: redis-server /etc/redis.conf. You should use the redis.conf file included in the root directory of the Redis source code distribution as a template to write your configuration file.

External programs talk to Redis using a TCP socket and a Redis specific protocol. This protocol is implemented in the Redis client libraries for the different programming languages. However to make hacking with Redis simpler Redis provides a command line utility that can be used to send commands to Redis. This program is called redis-cli.

The first thing to do in order to check if Redis is working properly is sending a PING command using redis-cli:

Running redis-cli followed by a command name and its arguments will send this command to the Redis instance running on localhost at port 6379. You can change the host and port used by redis-cli, just try the --help option to check the usage information.

Another interesting way to run redis-cli is without arguments: the program will start in interactive mode, you can type different commands and see their replies.

At this point you are able to talk with Redis. It is the right time to pause a bit with this tutorial and start the fifteen minutes introduction to Redis data types in order to learn a few Redis commands. Otherwise if you already know a few basic Redis commands you can keep reading.

By default Redis binds to all the interfaces and has no authentication at all. If you use Redis in a very controlled environment, separated from the external internet and in general from attackers, that's fine. However if Redis without any hardening is exposed to the internet, it is a big security concern. If you are not 100% sure your environment is secured properly, please check the following steps in order to make Redis more secure, which are enlisted in order of increased security.

  1. Make sure the port Redis uses to listen for connections (by default 6379 and additionally 16379 if you run Redis in cluster mode, plus 26379 for Sentinel) is firewalled, so that it is not possible to contact Redis from the outside world.
  2. Use a configuration file where the bind directive is set in order to guarantee that Redis listens on only the network interfaces you are using. For example only the loopback interface (127.0.0.1) if you are accessing Redis just locally from the same computer, and so forth.
  3. Use the requirepass option in order to add an additional layer of security so that clients will require to authenticate using the AUTH command.
  4. Use spiped or another SSL tunneling software in order to encrypt traffic between Redis servers and Redis clients if your environment requires encryption.

Note that a Redis exposed to the internet without any security is very simple to exploit, so make sure you understand the above and apply at least a firewalling layer. After the firewalling is in place, try to connect with redis-cli from an external host in order to prove yourself the instance is actually not reachable.

Of course using Redis just from the command line interface is not enough as the goal is to use it from your application. In order to do so you need to download and install a Redis client library for your programming language. You'll find a full list of clients for different languages in this page.

For instance if you happen to use the Ruby programming language our best advice is to use the Redis-rb client. You can install it using the command gem install redis.

These instructions are Ruby specific but actually many library clients for popular languages look quite similar: you create a Redis object and execute commands calling methods. A short interactive example using Ruby:

You can learn how Redis persistence works on this page, however what is important to understand for a quick start is that by default, if you start Redis with the default configuration, Redis will spontaneously save the dataset only from time to time (for instance after at least five minutes if you have at least 100 changes in your data), so if you want your database to persist and be reloaded after a restart make sure to call the SAVE command manually every time you want to force a data set snapshot. Otherwise make sure to shutdown the database using the SHUTDOWN command:

This way Redis will make sure to save the data on disk before quitting. Reading the persistence page is strongly suggested in order to better understand how Redis persistence works.

Docker Install Redis Cluster

Running Redis from the command line is fine just to hack a bit with it or for development. However at some point you'll have some actual application to run on a real server. For this kind of usage you have two different choices:

  • Run Redis using screen.
  • Install Redis in your Linux box in a proper way using an init script, so that after a restart everything will start again properly.

A proper install using an init script is strongly suggested. The following instructions can be used to perform a proper installation using the init script shipped with Redis 2.4 in a Debian or Ubuntu based distribution.

We assume you already copied redis-server and redis-cli executables under /usr/local/bin.

  • Create a directory in which to store your Redis config files and your data:

  • Copy the init script that you'll find in the Redis distribution under the utils directory into /etc/init.d. We suggest calling it with the name of the port where you are running this instance of Redis. For example:

  • Edit the init script.

Make sure to modify REDISPORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number.

Redis
  • Copy the template configuration file you'll find in the root directory of the Redis distribution into /etc/redis/ using the port number as name, for instance:

  • Create a directory inside /var/redis that will work as data and working directory for this Redis instance:

  • Edit the configuration file, making sure to perform the following changes:

    • Set daemonize to yes (by default it is set to no).
    • Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).
    • Change the port accordingly. In our example it is not needed as the default port is already 6379.
    • Set your preferred loglevel.
    • Set the logfile to /var/log/redis_6379.log
    • Set the dir to /var/redis/6379 (very important step!)
  • Finally add the new Redis init script to all the default runlevels using the following command:

You are done! Now you can try running your instance with:

Make sure that everything is working as expected:

  • Try pinging your instance with redis-cli.
  • Do a test save with redis-cli save and check that the dump file is correctly stored into /var/redis/6379/ (you should find a file called dump.rdb).
  • Check that your Redis instance is correctly logging in the log file.
  • If it's a new machine where you can try it without problems make sure that after a reboot everything is still working.

Note: In the above instructions we skipped many Redis configuration parameters that you would like to change, for instance in order to use AOF persistence instead of RDB persistence, or to setup replication, and so forth. Make sure to read the example redis.conf file (that is heavily commented) and the other documentation you can find in this web site for more information.

Redis

Redis and Express are tools that provide a simple and clean approach to their problem domains. What follows is a Docker/Docker Compose v2 setup.

The repo is available at: https://github.com/HugoDF/express-redis-docker.

The docker-compose.yml is available at: github.com/HugoDF/express-redis-docker/docker-compose.yml

Redis is “an open source, in-memory data structure store, used as a database, cache and message broker”. It’s as simple and unopinionated as a database as it gets, it’s known for its performance and simplicity as a key-value store. It has great flexibility and can also be used as a message queue, circular buffer (and pretty much anything else a developer can come up with short of a relational database).

Express is a “fast, unopinionated, minimalist web framework for Node.js”. In other words, it’s a wrapper around Node’s server, that provides a way to write what’s called “middleware” to share and compose functionality across HTTP endpoints as well as define said endpoints.

Getting Redis and Express to work together is trivial. Getting Redis and Express to work together in a way that’s fool and future-proof, and reproducible across local and deployed environments, is slightly harder. That’s where Docker and Docker Compose come in.

Docker is a containerisation system, Docker Compose is a way to define how multiple Docker containers interact. In the context of Node web application development, Docker tends to be used to define a container that has the required system-level dependencies (eg. Node version, any extra database drivers). Docker Compose would be used to define dependencies outside of the Node application, for example databases.

To start off, we should create a new directory/initialise npm:

We can then install Express:

And create a server.js file that looks like the following:

That can be run using:

We can check that it’s working as expected

To begin you’ll want to install Docker Community Edition (https://www.docker.com/community-edition).Then we can add a Dockerfile and a docker-compose.yml:

Now run the app inside of Docker/Docker Compose:

And check that it still works

Some extra context:

  • The Dockerfile defines what container the application will be running in (here a Node 10 default container)
  • docker-compose.yml:
    • build explains which image should be used by the app service definition (in this case, it points to what would be created by running the Dockerfile)
    • volumes defines what should be mounted where (in this case, we’ll mount the whole directory as /var/www/app
    • ports maps ports from the host system to ports inside the container
    • environment sets environment variables for the container
    • command determines what will be run on startup of the container, here it runs npm install followed by the server startup command

To add Redis to our Express app we should use the redis package:

Then we should probably wrap all the callback-based methods we want to use (see the api docs for the redis package, https://github.com/NodeRedis/node_redis#api).Let’s do this using a redis-client.js module:

To running Redis inside of Docker Compose in such a way that our app can access it:

We can now access the Redis client from the app container:

We can now create a HTTP API that will allow us to store data using query params and retrieve them using a get request (this isn’t RESTful at all but oh well 🙂 ).

If you have any questions about the above code let me know @hugo__df. It is using a couple of more advanced features like async/await and destructuring as well as Express features to get query and path parameters (see https://expressjs.com/en/api.html#req.query, https://expressjs.com/en/api.html#req.params).

Get the app running again: docker-compose up

  1. Store some data:
  1. Retrieve that data:

Note to more advanced Express users, and API design gurus.The reason I used an old school query-param based system is to avoid the boilerplate of using body-parser with POST requests

Full repository available at: https://github.com/HugoDF/express-redis-docker

Cover photo by Ben Koorengevel on Unsplash

Get The Jest Handbook (100 pages)

Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.

Docker Install Redis Linux

or

Docker Install Redis Windows

Join 1000s of developers learning about Enterprise-grade Node.js & JavaScript

RedisInsight is an intuitive and efficient GUI for Redis, allowing you to interact with your databases and manage your data—with built-in support for most popular Redis modules. It is a 100% free Redis GUI tool that allows you to visualise, monitor, and optimize while developing your applications with Redis

RedisInsight provides built-in support for the RedisJSON, RediSearch, RedisGraph, Redis Streams, and RedisTimeSeries modules to make it even easier to query, visualize, and interactively manipulate search indexes, graphs, streams, and time-series data.

A full-featured pure desktop GUI client, RedisInsight is available for Windows, macOS, and Linux and is fully compatible with Redis Enterprise. It works with any cloud provider as long as you run it on a host with network access to your cloud-based Redis server. RedisInsight makes it easy to discover cloud databases and configure connection details with a single click. It allows you to automatically add Redis Enterprise Software and Redis Enterprise Cloud databases.

Getting Started with RedisInsight#

  • MacOS
  • Linux
  • Windows
  • Docker
  • Kubernetes

Using MacOS#

Step 1. Download RedisInsight#

To use RedisInsight on a local Mac, you can download from the RedisInsight page on the RedisLabs website:

Click this link to access a form that allows you to select the operating system of your choice.

Fill out the rest of the form and click “Download.” Please note that the package name is the combination of the platform and version as shown here:

redisinsight-platform-version

Step 2. Install RedisInsight#

Click on the RedisInsight executable and install it in your system.

Head over to your web browser and go to http://localhost:8001

Congratulations! You have successfully installed RedisInsight and are now ready to inspect your Redis data, monitor database health, and perform runtime server configuration with this browser-based management interface for your Redis deployment.

Step 3. Connect to Redis database#

Assuming that you already have Redis database up and running, select 'Connect to a Redis database'

Step 4. Add Redis Database#

Enter the requested details, including Name, Host (endpoint), Port, and Password in the form, as shown below. You can skip username for now. Then click “ADD REDIS DATABASE”:

Step 5. Run the Redis CLI#

Finally, although RedisInsight is a great GUI, sometimes you want to work directly in the command-line interface (CLI). To do so, click “CLI” in the menu on the left side of the RedisInsight UI:

Then paste the appropriate Redis commands in the command section, marked with “>>” as shown below, and press Enter.

You can see the output displayed at the top of the screen. If it says “OK,” the command was executed successfully.

Using Linux#

Step 1. Download RedisInsight#

To use RedisInsight on your Linux machine, you can download from the RedisInsight page on the RedisLabs website:

Open this link to open up a form that allows you to select the operating system of your choice.

Fill out the rest of the form and click “Download.” Please note that the package name is the combination of the platform and version as shown here:

redisinsight-linux64

Step 2. Install RedisInsight#

Open a terminal and navigate to the folder containing the downloaded file.

Make your downloaded file into an executable.

Step 3. Start RedisInsight.#

To access your RedisInsight GUI, open a web browser and navigate to http://127.0.0.1:8001.

Congratulations! You have successfully installed RedisInsight and are now ready to inspect your Redis data, monitor database health, and perform runtime server configuration with this browser-based management interface for your Redis deployment.

Step 4. Connect to Redis database#

Assuming that you already have Redis database up and running, select 'Connect to a Redis database'

Enter the requested details, including Name, Host (endpoint), Port, and Password in the form, as shown below. You can skip username for now. Then click “ADD REDIS DATABASE”:

Step 5. Run the Redis CLI#

Finally, although RedisInsight is a great GUI, sometimes you want to work directly in the command-line interface (CLI). To do so, click “CLI” in the menu on the left side of the RedisInsight UI:

Then paste the appropriate Redis commands in the command section, marked with “>>” as shown below, and press Enter.

You can see the output displayed at the top of the screen. If it says “OK,” the command was executed successfully.

Using Windows#

Step 1. Download RedisInsight#

To install RedisInsight on Windows, there is no need to install any .NET framework. RedisInsight should install and run on a fresh Windows system.

To use RedisInsight on your Windows machine, you can download from the RedisInsight page on the RedisLabs website:

Open this link to open up a form that allows you to select the operating system of your choice.

Step 2. Install RedisInsight#

Fill out the rest of the form and click “Download.” Please note that the package name is the combination of the platform and version as shown here:

redisinsight-win-msi

Step 3. Accessing RedisInsight#

After the web server starts, open http://127.0.0.1:8001 and add a Redis database connection.

Congratulations! You have successfully installed RedisInsight and are now ready to inspect your Redis data, monitor database health, and perform runtime server configuration with this browser-based management interface for your Redis deployment.

Step 4. Connect to a Redis database#

Assuming that you already have Redis database up and running, select 'Connect to a Redis database'

Enter the requested details, including Name, Host (endpoint), Port, and Password in the form, as shown below. You can skip username for now. Then click “ADD REDIS DATABASE”:

Step 5. Run the Redis CLI#

Finally, although RedisInsight is a great GUI, sometimes you want to work directly in the command-line interface (CLI). To do so, click “CLI” in the menu on the left side of the RedisInsight UI:

Then paste the appropriate Redis commands in the command section, marked with “>>” as shown below, and press Enter.

You can see the output displayed at the top of the screen. If it says “OK,” the command was executed successfully.

Using Docker#

Step 1. Install Docker#

The first step is to install docker for your operating system. Run the docker version command in a terminal window to make sure that docker is installed correctly.

Note - On Windows and Mac, install docker version 18.03 or higher. You can run docker version to find out your docker version.

Step 2. Run RedisInsight Docker image#

Next, run the RedisInsight container. The easiest way is to run the following command:

docker run -d -v redisinsight:/db -p 8001:8001 redislabs/redisinsight:latest

Step 3. Accessing RedisInsight#

Next, point your browser to http://localhost:8001.

RedisInsight also provides a health check endpoint at http://localhost:8001/healthcheck/ to monitor the health of the running container.

Note: Make sure the directory you pass as a volume to the container has necessary permissions for the container to access it. For example, if the previous command returns a permissions error, run the following command:

In addition, you can add some additional flags to the docker run command:

You can add the -it flag to see the logs and view the progress.

On Linux, you can add --network host. This makes it easy to work with redis running on your local machine.

To analyze RDB files stored in S3, you can add the access key and secret access key as environment variables using the -e flag.

For example: -e AWS_ACCESS_KEY=aws_access_key -e AWS_SECRET_KEY=aws_secret_access_key

If everything worked, you should see the following output in the terminal:

Visit http://0.0.0.0:8001 in your web browser.

Head over to your web browser and go to http://localhost:8001

Congratulations! You have successfully installed RedisInsight and are now ready to inspect your Redis data, monitor database health, and perform runtime server configuration with this browser-based management interface for your Redis deployment.

Step 4. Connect to a Redis database#

Assuming that you already have Redis database up and running, select 'Connect to a Redis database'

Enter the requested details, including Name, Host (endpoint), Port, and Password in the form, as shown below. You can skip username for now. Then click “ADD REDIS DATABASE”:

Step 5. Run the Redis CLI#

Finally, although RedisInsight is a great GUI, sometimes you want to work directly in the command-line interface (CLI). To do so, click “CLI” in the menu on the left side of the RedisInsight UI:

Then paste the appropriate Redis commands in the command section, marked with “>>” as shown below, and press Enter.

You can see the output displayed at the top of the screen. If it says “OK,” the command was executed successfully.

Using Kubernetes#

Step 1. Create the RedisInsight deployment and service#

Below is an annotated YAML file that will create a RedisInsight deployment and a service in a k8s cluster.

Step 2. Create a new file redisinsight.yaml with the content below#

# RedisInsight service with name 'redisinsight-service'
kind: Service
name: redisinsight-service # name should not be 'redisinsight'
# environment variables that
# application's environment
# `REDISINSIGHT_PORT`
type: LoadBalancer
- port: 80
selector:
---
### RedisInsight deployment with name 'redisinsight'
apiVersion: apps/v1
metadata:
labels:
spec:
selector:
app: redisinsight #which pods is the deployment managing, as defined by the pod template
metadata:
app: redisinsight #label for pod/s
containers:
- name: redisinsight #Container name (DNS_LABEL, unique)
imagePullPolicy: IfNotPresent #Always pull image

Install Redis On Docker Windows 7

- name: db #Pod volumes to mount into the container's filesystem. Cannot be updated.
ports:
- containerPort: 8001 #exposed conainer port and protocol
volumes:
emptyDir: {} # node-ephemeral volume https://kubernetes.io/docs/concepts/storage/volumes/#emptydir

Step 3. Create the RedisInsight deployment and service#

Step 4. Accessing RedisInsight#

Once the deployment and service are successfully applied and complete, you shoould be able to access RedisInsight.This can be accomplished by listing the using the External IP address of the service we created to reach redisinsight.

NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redisinsight-service <cluster-ip> <external-ip> 80:32143/TCP 1m

If you are using minikube, run minikube list to list the service and access RedisInsight at http://minikube-ip:minikube-service-port.

------------- ---------------------- -------------- ---------------------------------------------
------------- ---------------------- -------------- ---------------------------------------------
default redisinsight-service 80 http://minikube-ip:minikubeservice-port
------------- ---------------------- -------------- ---------------------------------------------

Step 5. Create the RedisInsight deployment without a service.#

Below is an annotated YAML file that will create a RedisInsight deployment in a K8s cluster.

Install Redis On Docker 10

Create a new file redisinsight.yaml with the content below#

kind: Deployment
name: redisinsight #deployment name
app: redisinsight #deployment label
replicas: 1 #a single replica pod
matchLabels:
app: redisinsight #which pods is the deployment managing, as defined by the pod template
metadata:
app: redisinsight #label for pod/s
containers:
- name: redisinsight #Container name (DNS_LABEL, unique)
imagePullPolicy: IfNotPresent #Always pull image
# If there's a service named 'redisinsight' that exposes the
# deployment, we manually set `REDISINSIGHT_HOST` and
# `REDISINSIGHT_PORT` to override the service environment
- name: REDISINSIGHT_HOST
- name: REDISINSIGHT_PORT
volumeMounts:
- name: db #Pod volumes to mount into the container's filesystem. Cannot be updated.
ports:
- containerPort: 8001 #exposed conainer port and protocol
livenessProbe:
path : /healthcheck/ # exposed RI endpoint for healthcheck
initialDelaySeconds: 5 # number of seconds to wait after the container starts to perform liveness probe
periodSeconds: 5 # period in seconds after which liveness probe is performed
failureThreshold: 1 # number of liveness probe failures after which container restarts
- name: db
emptyDir: {} # node-ephemeral volume https://kubernetes.io/docs/concepts/storage/volumes/#emptydir

Step 6. Create the RedisInsight deployment#

Note - If the deployment will be exposed by a service whose name is ‘redisinsight’, set REDISINSIGHT_HOST and REDISINSIGHT_PORT environment variables to override the environment variables created by the service.

Once the deployment has been successfully applied and the deployment complete, access RedisInsight. This can be accomplished by exposing the deployment as a K8s Service or by using port forwarding, as in the example below:

Open your browser and point to http://localhost:8001

Congratulations! You have successfully installed RedisInsight and are now ready to inspect your Redis data, monitor database health, and perform runtime server configuration with this browser-based management interface for your Redis deployment.

Assuming that you already have Redis database up and running, select 'Connect to a Redis database'

Enter the requested details, including Name, Host (endpoint), Port, and Password in the form, as shown below. You can skip username for now. Then click “ADD REDIS DATABASE”:

Step 7. Run the Redis CLI#

Finally, although RedisInsight is a great GUI, sometimes you want to work directly in the command-line interface (CLI). To do so, click “CLI” in the menu on the left side of the RedisInsight UI:

Then paste the appropriate Redis commands in the command section, marked with “>>” as shown below, and press Enter.

Docker

You can see the output displayed at the top of the screen. If it says “OK,” the command was executed successfully.

RedisInsight Overview (RedisConf'21)#

Further References#