|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Repository Overview |
| 6 | + |
| 7 | +This repository provides **minimal Dockerfile templates** for installing individual PHP extensions. Each template demonstrates how to properly install and configure a single PHP extension on official PHP Docker images. Users can reference these templates to compose their own Dockerfiles with desired extensions. |
| 8 | + |
| 9 | +## Repository Structure |
| 10 | + |
| 11 | +``` |
| 12 | +{PHP_VERSION}/{OS_VERSION}/{EXTENSION_NAME}/Dockerfile |
| 13 | +``` |
| 14 | + |
| 15 | +- PHP versions: 8.1, 8.2, 8.3, 8.4 |
| 16 | +- OS versions: `bookworm` (Debian-based), `alpine3.22` (Alpine Linux) |
| 17 | +- Extension naming: Standard extensions use their name (e.g., `bcmath`, `gd`), PECL extensions use `pecl_` prefix (e.g., `pecl_redis`, `pecl_xdebug`) |
| 18 | + |
| 19 | +Each Dockerfile is a standalone template showing the minimal steps to install that specific extension. |
| 20 | + |
| 21 | +## Build Commands |
| 22 | + |
| 23 | +The Makefile is used for **testing templates**, not for building production images. |
| 24 | + |
| 25 | +### Build and test all templates for a PHP/OS combination |
| 26 | +```bash |
| 27 | +make build PHP_V=8.3 OS_V=bookworm # Build test images |
| 28 | +make test PHP_V=8.3 OS_V=bookworm # Run all tests |
| 29 | +``` |
| 30 | + |
| 31 | +### Build and test everything |
| 32 | +```bash |
| 33 | +make all PHP_V=8.3 OS_V=bookworm |
| 34 | +``` |
| 35 | + |
| 36 | +### Clean up test images |
| 37 | +```bash |
| 38 | +make clean PHP_V=8.3 OS_V=bookworm |
| 39 | +``` |
| 40 | + |
| 41 | +### Work with staged changes only (for template development) |
| 42 | +```bash |
| 43 | +make new-build # Build only staged Dockerfiles |
| 44 | +make new-test # Test only staged Dockerfiles |
| 45 | +make new # Build and test staged Dockerfiles |
| 46 | +``` |
| 47 | + |
| 48 | +The "new-*" targets automatically detect PHP version, OS version, and extension name from staged file paths. Use these when developing new templates. |
| 49 | + |
| 50 | +## Dockerfile Template Patterns |
| 51 | + |
| 52 | +### Standard PHP Extensions |
| 53 | +For built-in PHP extensions without additional dependencies: |
| 54 | +```dockerfile |
| 55 | +FROM php:{VERSION}-{OS} |
| 56 | +LABEL maintainer="Andrea Falco <andrea@falco.sh>" |
| 57 | +RUN docker-php-ext-install -j$(nproc) {extension_name} |
| 58 | +``` |
| 59 | + |
| 60 | +### PECL Extensions |
| 61 | +For PECL extensions with version pinning: |
| 62 | +```dockerfile |
| 63 | +FROM php:{VERSION}-{OS} |
| 64 | +LABEL maintainer="Andrea Falco <andrea@falco.sh>" |
| 65 | + |
| 66 | +ARG PHPEXT_{EXTENSION}_VERSION={version} |
| 67 | +RUN set -eux; \ |
| 68 | + pecl bundle -d /usr/src/php/ext {extension}-${PHPEXT_{EXTENSION}_VERSION}; \ |
| 69 | + docker-php-ext-install -j$(nproc) {extension}; \ |
| 70 | + \ |
| 71 | + docker-php-source delete; \ |
| 72 | + rm -rf /tmp/* /var/tmp/* |
| 73 | +``` |
| 74 | + |
| 75 | +### Critical Template Requirements |
| 76 | +- **Always clean up** `/tmp/*` and `/var/tmp/*` after installation |
| 77 | +- **Always run** `docker-php-source delete` for PECL extensions to minimize image size |
| 78 | +- **Use** `set -eux` for proper error handling in multi-line RUN commands |
| 79 | +- **Pin** PECL extension versions using ARG for reproducibility |
| 80 | +- Templates must be **minimal** - only include what's necessary for that specific extension |
| 81 | + |
| 82 | +## Testing Templates |
| 83 | + |
| 84 | +Four test targets validate each template: |
| 85 | + |
| 86 | +1. **test-version**: Verifies PHP executable works (`php --version`) |
| 87 | +2. **test-module**: Confirms extension appears in loaded modules (`php -m`) |
| 88 | +3. **test-info**: Verifies extension shows up in PHP info (`php -i`) |
| 89 | +4. **test-tmp-files**: Ensures no temporary files left behind (validates cleanup) |
| 90 | + |
| 91 | +The tmp-files test is critical - templates must not leave `/usr/src/php`, `/tmp/*`, or `/var/tmp/*` dirty. |
| 92 | + |
| 93 | +## CI/CD |
| 94 | + |
| 95 | +GitHub Actions workflow (`.github/workflows/ci.yaml`) validates all templates: |
| 96 | +- Builds and tests every template combination (PHP versions × OS versions × extensions) |
| 97 | +- Matrix strategy: PHP 8.1, 8.2, 8.3, 8.4 × alpine3.22, bookworm |
| 98 | +- Runs on every push to any branch |
| 99 | +- Ensures templates remain valid as PHP versions evolve |
| 100 | + |
| 101 | +## Adding New Extension Templates |
| 102 | + |
| 103 | +When adding a new extension template: |
| 104 | + |
| 105 | +1. Create directory structure: `{PHP_VERSION}/{OS_VERSION}/{extension_name}/` |
| 106 | +2. Add Dockerfile following the patterns above |
| 107 | +3. For PECL extensions, use `pecl_` prefix in directory name |
| 108 | +4. Add template for **all supported PHP versions and OS versions** for consistency (unless extension doesn't support certain PHP versions) |
| 109 | +5. Stage changes and run `make new` to validate templates work correctly |
| 110 | +6. Update the extension table in README.md with links to new templates |
| 111 | +7. Ensure proper cleanup (test-tmp-files must pass) |
| 112 | + |
| 113 | +## Extension Version Management |
| 114 | + |
| 115 | +- Standard PHP extensions inherit version from base PHP image |
| 116 | +- PECL extensions have versions explicitly pinned using ARG in Dockerfiles |
| 117 | +- Check PECL website for latest compatible versions: https://pecl.php.net/ |
| 118 | +- Some extensions don't support latest PHP versions (marked with ✗ in README) |
| 119 | +- When updating PECL versions, update across all PHP/OS combinations for that extension |
| 120 | + |
| 121 | +## How Users Use These Templates |
| 122 | + |
| 123 | +Users copy relevant RUN commands from templates into their own Dockerfiles to compose multi-extension images: |
| 124 | + |
| 125 | +```dockerfile |
| 126 | +FROM php:8.3-bookworm |
| 127 | + |
| 128 | +# Copy from bcmath template |
| 129 | +RUN docker-php-ext-install -j$(nproc) bcmath |
| 130 | + |
| 131 | +# Copy from pecl_redis template |
| 132 | +ARG PHPEXT_REDIS_VERSION=6.1.0 |
| 133 | +RUN set -eux; \ |
| 134 | + pecl bundle -d /usr/src/php/ext redis-${PHPEXT_REDIS_VERSION}; \ |
| 135 | + docker-php-ext-install -j$(nproc) redis; \ |
| 136 | + docker-php-source delete; \ |
| 137 | + rm -rf /tmp/* /var/tmp/* |
| 138 | + |
| 139 | +# Add application code... |
| 140 | +``` |
| 141 | + |
| 142 | +Templates demonstrate the **minimal, correct way** to install each extension. |
0 commit comments