Learn Redis CLI Basics With WordPress and Docker (r) (r)

Oct 4, 2023
An illustration representing the Redis command line interface (CLI).

-sidebar-toc>

This Redis command-line interface (CLI) is a way to communicate with your WordPress site's Redis server. Using Redis CLI Redis CLI (invoked via the command line as the redis-cli command line option) You can solicit and receive replies from the Redis server in a programmatic manner, track the actions executed by the Redis instance, check the speed of response for an instance, and observe the status of the server's state in real-time.

This guide teaches you how to integrate Redis and your WordPress site and use the Redis CLI to execute create reading, update and remove (CRUD) operations using your terminal.

Connect Redis to WordPress Utilizing Dev

Dev creates the Dockerized WordPress website locally using up-to-date versions of its dependencies which include an Nginx Web server, PHP, and a MySQL compatible MariaDB database. Additionally, it supports CLI for WordPress.

Dev gives three options to create locally-hosted WordPress websites: creating a new site that has default settings, a new website with customized settings as well as cloning a site located on your local development platform.

This tutorial employs the first option with the default settings as well as the latest releases that include Nginx, PHP, MariaDB, and WordPress.

Once you've followed those steps, you can access your dashboard from Dev:

Screenshot of the Dev dashboard and the details of a new WordPress site.
The Dev dashboard after creating the brand new WordPress site.

It is possible to open Docker Desktop to list all the containers that are running as dependencies within the Dev project. To do this, Click the Containers icon in the top right-hand corner of the left-hand sidebar

Screenshot: Docker Desktop list of containers running in Dev.
The Docker Desktop's list of Dev containers.

After your WordPress site and container dependencies are running smoothly and you're all set to connect to a Redis database to your website:

  1. Hit the WP Admin button on Dev to navigate to your WordPress admin dashboard.
  2. Select the tab for Plugins tab located on the left side.
  3. Select to Add New Search for "Redis object cache."
Screenshot: Searching for Redis Object Cache while adding plugins within WordPress.
Are you looking for Redis Object Cache plugin within WordPress.

Click "Install Now" next to the plugin. Then, confirm it is in use by clicking on the button Installed Plugins tab.

Screenshot: A list of installed WordPress plugins, with Redis Object Cache highlighted.
Installed WordPress plugins, such as Redis Object Cache.

Once activated after activation, after the first time it's activated, Redis Object Cache plugin will report that it is unable to connect to a Redis database. The solution is to create containers with an Redis instance within Docker.

Open a terminal, and make a container for the network called redisnet where you can execute your Redis image

docker network create -d bridge redisnet

Create and then start a Redis image as a stand-alone container inside the container that is part of the network:

docker run -d -p 6379:6379 --name demo_redis --network redisnet redis

After starting the container image Check within Docker Desktop that the Redis image is running:

Screenshot: Indicator that demo_redis image is running in Docker Desktop.
Docker Desktop running the demo_redis image.

Remember that your WordPress website was automatically launched within a Docker container. You are able to verify its container network name and ID:

docker network Ls
Screenshot: Output of Docker's network list command.
Names and IDs for networks within Docker Desktop.

The shortened version of the network ID for the dev_network is D1Fa155f7a4d. It will be utilized later on.

Another way to connect your Redis cluster and server to the local WordPress website is to directly connecting the Redis as well as the Dev networks. To begin, check the ID of the container to Redis. Here, with the Docker command that lists containers, we're employing the --l (latest) switch to only show the most recent container that was created:

docker PS"-l"
Screenshot: The output of the Docker command to show information about containers.
Container information is generated using Docker's ps command.

In this example the container ID that is truncated appears as C3FFFC630e44C..

Connect the Redis container onto the Dev network using your ID for the container instead of ours in the following command.

docker network connect dev_network c3ffc630e44c

The Redis container should be added successfully your Redis containers image into the database of containers running within dev_network. To confirm the IP address for Redis, you need to verify the IP address of Redis container, execute the following command. Replace the dev_network ID with the one you have found with the network ls command in your application:

docker inspect d1fa155f7a4d
Screenshot: Output of the docker inspect command.
Terminal showing some of the output from the inspector docker command.

In the image above, the IP address of the demo_redis container is 172.172.0.6. Make a note of the IP address in your application prior to starting the connection.

Find where the site's root is located. WordPress website from your local machine, which is located on the Site Path displayed in Dev. Open the wp-config.php file in a text editor and add this code into the section that contains customization of configuration variables.

define('WP_REDIS_CLIENT', 'predis');
 define('WP_REDIS_HOST', '172.172.0.6');
 define('WP_REDIS_PORT', '6379');

The IP address that you are using to host WP_REDIS_HOST is the one that you discovered in demo_redis. demo_redis after you ran the docker inspect command.

This configuration code adds the Redis host, client and port so that the plugin can access the Redis server within the same Docker container and on the same network in the same way as Dev.

To confirm your plugin's connection and running:

  1. Visit your WordPress plugins page within the admin dashboard.
  2. Select on the Settings link for the Redis object cache.
  3. On the Settings page, choose the Enable Cache for Objects Cache link.
Screenshot: Enabling the Redis Object Cache in WordPress.
Redis Object Cache is writeable and reachable, but needs to be turned on.

If the Redis object cache is turned on, the Settings page will look something similar to this:

Screenshot: The Redis Object Cache Settings page with the plugin enabled.
The Redis Page for Object Cache Settings displays Redis is connected and running.

The Redis CLI is now available for launch.

The Redis CLI tool ( redis-cli) comes bundled with a Redis server. You can install the Redis server for Windows, macOS, and Linux operating platforms.

Above, when we used to run the docker ps command, we were able to determine the ID of the container that runs the Redis image. It was with c3ffc630e44c. This altered ID to access the demo_redis and launch a command-line shell within it:

docker exec -it c3ffc630e44c /bin/sh

Now you can start the redis-cli program:

Screenshot: redis-cli invoked in Docker.
redis-cli invoked from the shell of Docker.

It is possible to try to ping the server in order to test the connection:

Ping
Screenshot: pinging the Redis server using redis-cli.
Server response to Ping in redis cli.

In order to connect with the Redis server with its IP address and port and verify the connection, execute the below commands (using an IP address of the Redis container that you are using as a demo container):

redis-cli 172.172.0.6 -p 6379 ping
Screenshot: Pinging the Redis server via an IP address.
The ping is sent to the Redis server via its IP address and port.

It is now possible to connect successfully with the Redis server, both on your personal machine as well as the Docker container.

Then, confirm the fact that your WordPress site is storing information within the Redis cache making use of redis-cli and its keys * command. It will display all keys stored that are stored in your database.

Screenshot: Listing the keys on the Redis server.
Retrieving all keys in the Redis database with an Asterisk wildcard search.

You can see that some WordPress data has already been transferred to Redis cache. Redis cache.

Redis CLI CRUD Operations

The CLI tool lets you perform CRUD functions within your Redis database using this CLI tool.

Let's find out the value of a key we copied from earlier Redis CLI keys Request.

get :u.:~8r]mC->Re/gG!&mGU. [{+;]t7o
Screenshot: Results of a Redis database query.
Redis response to "get" request made using the redis-cli.

We can alter the data in the Redis database using redis-cli and utilizing the set, get and del commands to make a key/value pair, query the new key and then delete it:

Screenshot: Creating, querying and deleting Redis key/value data.
Simple CRUD operations with an Redis database with the redis-cli.

As mentioned above, trying to find the value of a key that has been deleted is a failure. zero.

There are numerous more advanced functions that can be performed with the CLI Redis.

Summary

Redis CLI is an easy option to try Redis commands in your site before pushing it to an actual environment. It is also a great option to keep track of activity in an Redis instance.

In Docker, a Redis instance comes with all of the software you require to handle your databasewhich includes redis-cli that are all contained into a single container.

Meanwhile Dev's Dockerized approach makes it easy to create a WordPress website on a local machine and then deploy the site to production in only a couple of clicks on a mouse.

Steve Bonisteel

Steve Bonisteel is a Technical Editor at who began his writing career as print journalist, running around after fire trucks and ambulances. The journalist has covered internet-related technologies since the mid 1990s.