diff --git a/Dockerfile b/Dockerfile index ad73115..4c23985 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,7 +40,8 @@ RUN apt-get update \ FROM node AS symfony RUN install-php-extensions \ apcu \ - sodium + sodium \ + redis # ------------------------- # Laravel profile @@ -56,7 +57,8 @@ FROM base AS wordpress RUN install-php-extensions \ mysqli \ exif \ - imagick + imagick \ + redis # ------------------------- # Magento 2 profile @@ -67,14 +69,16 @@ RUN install-php-extensions \ xsl \ soap \ bcmath \ - sodium + sodium \ + redis # ------------------------- # OpenCart profile # ------------------------- FROM base AS opencart RUN install-php-extensions \ - mysqli + mysqli \ + redis # 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 diff --git a/README.md b/README.md index 8c898c8..3d5787e 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,155 @@ Changes take effect after container restart: 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 +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 ### Multi-Stage Build @@ -315,6 +464,14 @@ The Dockerfile uses multi-stage builds to optimize for different frameworks: - **Volume**: `db_data` (persistent) - **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 ```yaml @@ -340,13 +497,19 @@ volumes: ### Framework-Specific Extensions +**All Frameworks** (Laravel, Symfony, WordPress, OpenCart, Magento2): +- `redis`: Redis cache and session support + **Laravel/Magento2**: -- `redis`: Redis cache support +- Additional caching optimizations **Symfony**: - `apcu`: APCu caching - `sodium`: Modern cryptography +**WordPress**: +- Additional compatibility features + ## Troubleshooting ### Port Already in Use diff --git a/docker-compose.yml b/docker-compose.yml index 20ead63..8509ade 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,11 +48,23 @@ services: volumes: - 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: caddy_data: caddy_config: db_data: + redis_data: networks: app-network: