feat: Add Redis support to all frameworks and comprehensive setup documentation

This commit is contained in:
Mohamed Youssef
2026-05-01 18:32:35 +03:00
parent 526364861f
commit 513f0ff4f9
3 changed files with 184 additions and 5 deletions
+8 -4
View File
@@ -40,7 +40,8 @@ RUN apt-get update \
FROM node AS symfony FROM node AS symfony
RUN install-php-extensions \ RUN install-php-extensions \
apcu \ apcu \
sodium sodium \
redis
# ------------------------- # -------------------------
# Laravel profile # Laravel profile
@@ -56,7 +57,8 @@ FROM base AS wordpress
RUN install-php-extensions \ RUN install-php-extensions \
mysqli \ mysqli \
exif \ exif \
imagick imagick \
redis
# ------------------------- # -------------------------
# Magento 2 profile # Magento 2 profile
@@ -67,14 +69,16 @@ RUN install-php-extensions \
xsl \ xsl \
soap \ soap \
bcmath \ bcmath \
sodium sodium \
redis
# ------------------------- # -------------------------
# OpenCart profile # OpenCart profile
# ------------------------- # -------------------------
FROM base AS opencart FROM base AS opencart
RUN install-php-extensions \ RUN install-php-extensions \
mysqli mysqli \
redis
# Fix permissions for storage & cache (FrankenPHP runs as UID 65534 = nobody) # Fix permissions for storage & cache (FrankenPHP runs as UID 65534 = nobody)
RUN if [ -d /app/storage ]; then chown -R 65534:65534 /app/storage /app/bootstrap/cache; fi RUN if [ -d /app/storage ]; then chown -R 65534:65534 /app/storage /app/bootstrap/cache; fi
+164 -1
View File
@@ -284,6 +284,155 @@ Changes take effect after container restart:
docker-compose restart php docker-compose restart php
``` ```
## Redis Caching
### Setup
Redis is included and pre-configured in `docker-compose.yml`. It runs in a separate container with:
- **Image**: `redis:alpine` (lightweight, fast)
- **Persistent Storage**: Redis data is stored in `redis_data` volume
- **Health Check**: Built-in health check to verify Redis is running
- **Networking**: Connected to `app-network` for internal PHP communication
### Redis Service Configuration
Redis is automatically started with `docker-compose up`. To verify it's running:
```bash
# Check Redis container status
docker-compose ps redis
# Connect to Redis CLI
docker-compose exec redis redis-cli
# Test Redis connection from PHP container
docker-compose exec php php -r "
\$redis = new Redis();
\$redis->connect('redis', 6379);
echo 'Redis connected: ' . (\$redis->ping() ? 'OK' : 'FAILED');
\$redis->close();
"
```
### Using Redis in Your Application
#### Laravel Example
Update your `.env`:
```env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=null
```
Install/configure in your Laravel app:
```bash
# Inside container
docker-compose exec php composer require predis/predis
# Or use PHP's native Redis extension (recommended)
```
#### PHP Native Example
```php
<?php
$redis = new Redis();
$redis->connect('redis', 6379);
// Set a cache value
$redis->set('user:1:name', 'John Doe', 3600); // 1 hour TTL
// Get a value
$name = $redis->get('user:1:name');
// Check if key exists
if ($redis->exists('user:1:name')) {
echo "Key exists";
}
// Delete a key
$redis->del('user:1:name');
// Increment counter
$redis->incr('page_views');
$redis->close();
?>
```
#### Symfony Example
Configure in `config/packages/cache.yaml`:
```yaml
framework:
cache:
default_redis_provider: 'redis://redis:6379'
pools:
cache.app:
adapter: cache.adapter.redis
provider: 'redis://redis:6379'
```
#### WordPress Example
Use a Redis plugin like "Redis Object Cache" and configure:
```php
define('WP_REDIS_HOST', 'redis');
define('WP_REDIS_PORT', 6379);
```
### Common Redis Operations
```bash
# Connect to Redis CLI
docker-compose exec redis redis-cli
# Inside redis-cli commands:
PING # Check connection
SET mykey "Hello" # Set a key
GET mykey # Get a value
DEL mykey # Delete a key
KEYS * # List all keys
FLUSHDB # Clear all keys
INFO # Show Redis info
MONITOR # Monitor commands in real-time
```
### Redis Persistence
- Data persists in the `redis_data` volume
- If you need to reset Redis data: `docker volume rm gold_redis_data`
- Configure persistence in `docker-compose.yml` if needed:
```yaml
redis:
command: redis-server --appendonly yes # Enable AOF persistence
```
### Troubleshooting Redis
```bash
# Check if Redis is running
docker-compose logs redis
# Restart Redis
docker-compose restart redis
# Clear Redis cache
docker-compose exec redis redis-cli FLUSHDB
# Monitor Redis activity
docker-compose exec redis redis-cli MONITOR
```
## Docker Architecture ## Docker Architecture
### Multi-Stage Build ### Multi-Stage Build
@@ -315,6 +464,14 @@ The Dockerfile uses multi-stage builds to optimize for different frameworks:
- **Volume**: `db_data` (persistent) - **Volume**: `db_data` (persistent)
- **Port**: `3306` (internal) - **Port**: `3306` (internal)
#### Redis Service (Cache)
- **Image**: `redis:alpine` (lightweight)
- **Volume**: `redis_data` (persistent)
- **Port**: `6379` (internal)
- **Health Check**: Built-in Redis ping monitoring
- **Available in all frameworks**: Laravel, Symfony, WordPress, OpenCart, Magento2
### Network & Volumes ### Network & Volumes
```yaml ```yaml
@@ -340,13 +497,19 @@ volumes:
### Framework-Specific Extensions ### Framework-Specific Extensions
**All Frameworks** (Laravel, Symfony, WordPress, OpenCart, Magento2):
- `redis`: Redis cache and session support
**Laravel/Magento2**: **Laravel/Magento2**:
- `redis`: Redis cache support - Additional caching optimizations
**Symfony**: **Symfony**:
- `apcu`: APCu caching - `apcu`: APCu caching
- `sodium`: Modern cryptography - `sodium`: Modern cryptography
**WordPress**:
- Additional compatibility features
## Troubleshooting ## Troubleshooting
### Port Already in Use ### Port Already in Use
+12
View File
@@ -48,11 +48,23 @@ services:
volumes: volumes:
- db_data:/var/lib/mysql - db_data:/var/lib/mysql
redis:
image: redis:alpine
restart: unless-stopped
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
labels:
glance.parent: redis
glance.name: Redis
# Volumes needed for Caddy certificates and configuration # Volumes needed for Caddy certificates and configuration
volumes: volumes:
caddy_data: caddy_data:
caddy_config: caddy_config:
db_data: db_data:
redis_data:
networks: networks:
app-network: app-network: