DEV Community

Ibrar Hussain
Ibrar Hussain

Posted on • Originally published at Medium

Setup Redis Docker Container to Use with Laravel App

If you are working on Laravel project optimization, you might have considered using cache, with Redis being a popular choice. Laravel provides support for multiple cache drivers, including Redis.

To start using Redis, you'll first need to install the phpredis package. You can do this by running the following command:

composer require predis/predis
Enter fullscreen mode Exit fullscreen mode

Next, update your .env file and change the CACHE_DRIVER value to redis:

CACHE_DRIVER=redis
Enter fullscreen mode Exit fullscreen mode

Now, let's set up Redis using a Docker container. If you don't have Docker Desktop installed, follow these steps:

Install Docker Desktop

  • Download Docker Desktop:
    Visit the Docker Desktop website and download the appropriate version for your operating system.

  • Install Docker Desktop:
    Follow the installation instructions for your operating system.

Set Up Redis with Docker

  • Create a Folder:
mkdir docker-workspace
Enter fullscreen mode Exit fullscreen mode
  • Navigate to the Folder:
cd docker-workspace
Enter fullscreen mode Exit fullscreen mode
  • Create a Docker Compose File:
touch docker-compose.yml
Enter fullscreen mode Exit fullscreen mode
  • Open docker-compose.yml and Add the Following Content:
version: "3.7"
services:
  # Redis Server for Cache
  redis-cache:
    image: redis:latest
    restart: always
    ports:
      - "6379:6379"
    command: redis-server --save 20 1 --loglevel warning
    volumes:
      - cache:/data

volumes:
  cache:
    driver: local

Enter fullscreen mode Exit fullscreen mode
  • Save and Close the File.

  • Start Docker Containers:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

With this setup, you now have a Redis server running in a Docker container, ready to be used for caching in your Laravel application.

For a more detailed guide on setting up Docker containers for other services like MySQL and Mailhog, you can refer to my previous articles (Mailhog, MySQL).

Happy coding!

Top comments (0)