How to use Redis docker php ext install and enable Redis CMD

Posted December 13, 2023
How to use Redis docker php ext install and enable Redis CMD

Redis is an in-memory data structure store. It’s easily integrated into the PHP app. When Running Redis as a Docker Container, you’ll need the Redis extension installed.

This guide demystifies the steps of using docker php ext install (pecl) Redis and docker php ext enable Redis commands. These commands lets enable the Redis extension within your Dockerfile, and into Dockerized PHP applications.

Now, get ready to to understand and learn how Docker uses the docker php ext command to install and enable Redis within Docker and Docker.

Step 1: Installing Redis Extension with docker php ext Install Redis

The docker php ext command works well if used with a Dockerfile. Assume your standard PHP app structure is as follows:

php-redis/
β”‚ β”œβ”€β”€ redis.dockerfile
β”‚ └── docker-compose.yml
└── redis/
    └── index.php

The Dockerfile installs and enable the Redis PHP extension with the following Docker commands:

FROM php:7.0-apache

# Use pecl to get Redis extension ready
RUN pecl install redis \
   # Redis is installed, enable it
    && docker-php-ext-enable redis
  • Here, you’re using Apache to run PHP 7.0 and you can update the version if needed.
  • To install Redis on Docker, you use RUN pecl install redis.
  • Once installed, you must enable the same Redis Extension using the docker-php-ext-enable redis command.

Step 2: Creating Docker Container with Redis

To put docker php ext install redis and docker php ext enable redis commands into action, let’s create a simple Docker container and and learn how to use the Redis Extension with PHP on Docker.

Go ahead and create a PHP and Redis Containers in the docker-compose.yml example file with the following code sample:

version: '3'
services:
  # PHP Apache Container
  php-apache:
    build:
      # Dockerfile path and the matching name
      context: .
      dockerfile: redis.dockerfile
    ports:
      # Map port 4000 to access the PHP container
      - "4000:80"
    # Add code files to Docker
    volumes:
      - ./redis:/var/www/html
    depends_on:
      # redis-server container will start first
      - redis

  # Redis Server Container
  redis-server:
    # Using the official Redis image from Docker Hub
    image: redis
    ports:
      # Mapping port 6379 on host to port 6379 in the container
      - "6379:6379"

In you docker-compose file:

  • You must add the ./redis directory and share it so Docker can access it.
  • PHP container must use depends_on. This will allow Docker to always ensure that redis-server is Running before serving your PHP Redis script.
  • Redis must be exposed to port 6379 so that PHP can connect to it through this port.

Step 3: Using Redis Docker Extension with PHP on Docker

Based on your file structure, the PHP code will be hosted in the redis/index.php file as follows:

<?php
$redis = new Redis();
$redis->connect('redis-server', 6379);

echo "Connection to server successfully <br>";

echo "Server is running: " . $redis->ping() . "<br>";
?>

Based on this script you must note that $redis->connect('redis-server', 6379); is the most import here because:

  • redis-server must be your container running Redis, and you named it in your docker-compose.yml file.
  • 6379 is the Redis port running on Docker.

Step 4: Building and Running Redis PHP Docker Container

Run the following command to package Redis Extension and PHP on Docker:

# Run on detouch mode with -d flag
docker-compose up -d --build

How to use Redis docker php ext install and enable Redis CMD

How to use Redis docker php ext install and enable Redis CMD

Now Navigate to http://localhost:4000 and access your app as follows:

How to use Redis docker php ext install and enable Redis CMD

This confirms you a successful container to excute docker php ext install redis and docker php ext enable redis Commands within Docker with access to PHP

Related: Get into this Xdebug docker php ext install and enable command Guide.

Conclusion

You’ve crafted a simple Redis PHP container, showcasing the docker php ext install redis and docker php ext enable redis commands with Docker. This guide serves as a foundational resource, illustrating Redis integration into Dockerized PHP applications. I hope you found these steps helpful.

How to use Redis docker php ext install and enable Redis CMD

Written By:

Joseph Chege