Configuring Redis on Docker in Laravel

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.

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.

Software Developer based in the UK, used to work in Singapore, but originally from Myanmar.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store