This quick article will show you how to configure Redis on Docker in Laravel for caching.
First, you need to install the Predis library running the following composer command.
composer require predis/predis
Then you need to declare the Redis image in the docker-compose.yml file as follow.
As you can see, it is on a network called app-network so that we need to declare the network in the docker-compose.yml file too as follow.
Then we need to set the environment variables in the .env file as follow.
The REDIS_HOST variable is set with the container name. Something we should pay attention here is that REDIS_PASSWORD=null. If we do not set the password to null, it won’t work.
Then we can set the cache as follow in our code.
Then, we can retrieve it back as follow.
That’s it. That is how we use Redis on Docker in Laravel.