SPVM
Dockerize your Drupal
Search Menu

How to set up Redis with Docker and Drupal

Safe trade zone to exchange goods in Montreal

How to set up docker for your Drupal project

Dark Light
Redis is a powerful caching system, therefore it is a great tool to use with Drupal. This article will show you how to set up a Redis with docker for your Drupal project.

Redis can be used as a powerful caching system to use with Drupal. This article will show you how to set up a Redis with docker for your Drupal project.

If you are new to how to set up Docker for Drupal, I strongly suggest you read first the article Dockerize your Drupal environment

To set up Redis with Drupal, you’ll need to follow these steps:

Install Redis image on your Docker project:

You need to set up a volume to redis save the data persistently. You use the key volume for this proposal. You also need to add the service redis. This is an example of the docker-compose file

docker-compose.yml

volume:
  redis-cache:
services:
  redis:
    image: redis:6.2.7
    restart: always
    ports:
      - 6379:6379
    command: --port 6379
    expose:
      - 6379
    volumes:
      - redis-cache:/data:delegated

You also need to install the Redis PHP Extension from your Dockerfile. In this example, I used the php7.4 and apache as my based system, and then I installed redis from pecl package

dockerfile

FROM php:7.4-apache
ARG env
#To allow install php package https://hub.docker.com/_/php
RUN rm /etc/apt/preferences.d/no-debian-php
#Installation of Redis
RUN pecl install redis \
    && docker-php-ext-enable redis

If you are using the recommended compose system to manage your dependencies, you can use the following command from your project root.

composer require drupal/redis

and then enable the module with drush

drush en redis

Otherwise, you need to install and configure Redis Drupal Module using the following steps:

  • Log in to your Drupal site as an administrator.
  • Go to the Drupal website and download the Redis module (https://www.drupal.org/project/redis).
  • Extract the downloaded module and place it in your Drupal modules directory (usually sites/all/modules).
  • Enable the Redis module by navigating to Admin > Extend in your Drupal administration interface.

Once the module is installed, you need to enable it. You can do it with the drush command.

drush en redis.

Now, you need to set up the project. In the setting file of your Drupal you can add the following code.

/**
 * Redis config
 *
 * Configuration Redis for the cache
**/
try {
  $redis = new Redis();
  $redis->connect('Redis', 6379);
  if ($redis->IsConnected()) {
    $redis->auth(NULL);
    $response = $redis->ping();
    if ($response) {
      # Configuration Redis for the cache
      $settings['redis.connection']['host'] = 'redis';
      $settings['redis.connection']['password'] = NULL;
      $settings['redis.connection']['port'] = '6379';
      $settings['redis.connection']['instance'] = 'cache';
      $settings['redis.connection']['interface'] = 'PhpRedis';
      $settings['cache']['default'] = 'cache.backend.redis';
      $settings['container_yamls'][] = 'modules/redis/example.services.yml';
      $conf['redis_perm_ttl'] = 2592000;
      $settings['redis_compress_length'] = 100;
      $settings['redis_compress_level'] = 3;
    }
  }
} catch (Exception $e) {
}

That is it!!! You can check that everything is working properly by going to the redis report page at /admin/reports/redis If everything is fine you should see something like this one.

Thank you for reading this article

Related Posts

How to programmatically delete a node from Drupal

One can use the Drupal entityQuery to get all the nodes you want to delete. Then use either the node function delete to delete all versions of a specific node or use the function removeTranslation to delete a specific translated version. In this article I show you how to use them.
Total
0
Share