optimize-docker-opcache

Optimizes OPcache configuration for PHP Docker containers. Configures memory, file limits, JIT, and validation for production and development.

59 stars

Best use case

optimize-docker-opcache is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Optimizes OPcache configuration for PHP Docker containers. Configures memory, file limits, JIT, and validation for production and development.

Teams using optimize-docker-opcache should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/optimize-docker-opcache/SKILL.md --create-dirs "https://raw.githubusercontent.com/dykyi-roman/awesome-claude-code/main/skills/optimize-docker-opcache/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/optimize-docker-opcache/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How optimize-docker-opcache Compares

Feature / Agentoptimize-docker-opcacheStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Optimizes OPcache configuration for PHP Docker containers. Configures memory, file limits, JIT, and validation for production and development.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# OPcache Configuration Optimization

Provides production and development OPcache tuning for PHP Docker containers, including JIT and preloading.

## OPcache Impact Overview

```
┌─────────────────────────────────────────────────────────────────┐
│                  OPCACHE PERFORMANCE IMPACT                      │
├─────────────────────────────────────────────────────────────────┤
│  Without OPcache:                                               │
│  Request → Parse → Compile → Execute → Response      ~50ms     │
│  With OPcache:                                                  │
│  Request → Execute (cached bytecode) → Response      ~15ms     │
│  With OPcache + JIT:                                            │
│  Request → Execute (native code) → Response          ~10ms     │
│  With OPcache + JIT + Preload:                                  │
│  Request → Execute (preloaded) → Response             ~5ms     │
└─────────────────────────────────────────────────────────────────┘
```

## Production vs Development Settings

| Setting | Production | Development | Purpose |
|---------|-----------|-------------|---------|
| opcache.enable | 1 | 1 | Enable OPcache |
| opcache.enable_cli | 1 | 0 | Cache for CLI scripts |
| opcache.memory_consumption | 256 | 128 | Shared memory (MB) |
| opcache.interned_strings_buffer | 32 | 16 | Interned strings (MB) |
| opcache.max_accelerated_files | 20000 | 10000 | Max cached files |
| opcache.validate_timestamps | 0 | 1 | Check file changes |
| opcache.revalidate_freq | 0 | 2 | Revalidation interval (s) |
| opcache.jit | 1255 | disable | JIT mode |
| opcache.jit_buffer_size | 128M | 0 | JIT buffer |

## Memory Consumption Calculation

```
memory_consumption = total_php_files * avg_bytecode_size (20-50 KB per file)

  Small project (500 files):    15MB  → set 64MB
  Medium project (2000 files):  60MB  → set 128MB
  Large project (5000 files):  175MB  → set 256MB
  Enterprise (10000+ files):   350MB  → set 512MB
```

## max_accelerated_files Calculation

PHP rounds up to the next prime from: 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987

| Total PHP Files | Set To | PHP Uses |
|----------------|--------|----------|
| < 200 | 1000 | 983 |
| 200-900 | 4000 | 3907 |
| 900-3800 | 8000 | 7963 |
| 3800-8000 | 20000 | 16229 |
| 8000-16000 | 32531 | 32531 |

## JIT Configuration (PHP 8.0+)

```
opcache.jit = CRTO (4-digit mode)
  C: CPU optimization (0=off, 1=on)
  R: Register allocation (0=off, 1=local, 2=global)
  T: Trigger (0=first run, 3=hot counter, 5=tracing)
  O: Optimization (0=none, 5=full)
```

| Mode | Value | Use Case |
|------|-------|----------|
| Tracing (recommended) | 1255 | Web applications |
| Function | 1205 | Simpler, less memory |
| Disabled | disable | Development, debugging |

## Preloading (PHP 7.4+)

```php
<?php
// config/preload.php
$directories = [
    '/app/src/Domain/', '/app/src/Application/',
    '/app/vendor/symfony/http-kernel/',
];
foreach ($directories as $dir) {
    if (!is_dir($dir)) continue;
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS)
    );
    foreach ($it as $file) {
        if ($file->getExtension() === 'php') opcache_compile_file($file->getRealPath());
    }
}
```

| Category | Preload? | Reason |
|----------|----------|--------|
| Domain entities / Value objects | Yes | Loaded on every request |
| Framework core | Yes | Always loaded |
| Controllers | Maybe | Only relevant routes |
| Tests / Migrations | No | Not in production |

## Monitoring OPcache

| Metric | Healthy | Action Needed |
|--------|---------|--------------|
| Hit rate | > 99% | < 95%: check validate_timestamps |
| Memory free | > 20% | < 10%: increase memory_consumption |
| Wasted memory | < 5% | > 10%: restart PHP-FPM |
| Cache full | false | true: increase max_accelerated_files |

## Production OPcache INI

```ini
[opcache]
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 32
opcache.max_accelerated_files = 20000
opcache.validate_timestamps = 0
opcache.revalidate_freq = 0
opcache.save_comments = 1
opcache.fast_shutdown = 1
opcache.jit = 1255
opcache.jit_buffer_size = 128M
opcache.preload = /app/config/preload.php
opcache.preload_user = www-data
opcache.log_verbosity_level = 1
opcache.error_log = /proc/self/fd/2
```

## Development OPcache INI

```ini
[opcache]
opcache.enable = 1
opcache.enable_cli = 0
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.validate_timestamps = 1
opcache.revalidate_freq = 2
opcache.save_comments = 1
opcache.jit = disable
opcache.jit_buffer_size = 0
```

## Dockerfile Integration

```dockerfile
ARG APP_ENV=production
COPY docker/php/opcache-${APP_ENV}.ini /usr/local/etc/php/conf.d/opcache.ini
COPY config/preload.php /app/config/preload.php
```

## Before/After Comparison

| Metric | No OPcache | OPcache | OPcache+JIT+Preload |
|--------|-----------|---------|---------------------|
| First request | 200ms | 200ms | 50ms (preloaded) |
| Subsequent requests | 200ms | 15ms | 5ms |
| Memory per worker | 30MB | 35MB | 38MB |
| Throughput (req/s) | 50 | 300 | 500 |
| CPU usage | High | Medium | Low |

## Generation Instructions

1. **Count PHP files:** `find /app -name "*.php" | wc -l` (including vendor)
2. **Calculate memory:** files * 35KB, round up to nearest power of 2
3. **Set max_accelerated_files:** Next prime above file count
4. **Configure JIT:** 1255 for production, disable for development
5. **Create preload script:** Include domain, application, and framework core
6. **Set validate_timestamps:** 0 for immutable containers, 1 for development
7. **Monitor:** Check hit rate and memory usage after deployment

Related Skills

optimize-docker-startup

59
from dykyi-roman/awesome-claude-code

Optimizes Docker container startup time for PHP applications. Reduces initialization overhead through preloading, caching, and entrypoint optimization.

optimize-docker-php-fpm

59
from dykyi-roman/awesome-claude-code

Optimizes PHP-FPM configuration in Docker containers. Tunes process manager, request handling, and resource allocation for production workloads.

optimize-docker-layers

59
from dykyi-roman/awesome-claude-code

Analyzes and optimizes Docker layer caching for PHP projects. Identifies layer ordering issues, cache invalidation problems, and provides recommendations for faster builds.

optimize-docker-image-size

59
from dykyi-roman/awesome-claude-code

Optimizes Docker image size for PHP projects. Reduces image footprint through Alpine, multi-stage builds, layer cleanup, and dependency minimization.

optimize-docker-compose-resources

59
from dykyi-roman/awesome-claude-code

Optimizes Docker Compose resource allocation for PHP stacks. Configures memory limits, CPU constraints, and service scaling.

optimize-docker-build-time

59
from dykyi-roman/awesome-claude-code

Optimizes Docker build time for PHP projects. Analyzes layer caching, BuildKit features, parallel builds, and dependency installation.

docker-troubleshooting-knowledge

59
from dykyi-roman/awesome-claude-code

Docker troubleshooting knowledge base. Provides debugging patterns, common error solutions, and diagnostic commands for PHP containers.

docker-security-knowledge

59
from dykyi-roman/awesome-claude-code

Docker security knowledge base for PHP. Provides hardening patterns, vulnerability scanning, secrets management, and OWASP container guidelines.

docker-scanning-knowledge

59
from dykyi-roman/awesome-claude-code

Docker image scanning knowledge base. Provides vulnerability detection, compliance checking, and SBOM generation for PHP container images.

docker-production-knowledge

59
from dykyi-roman/awesome-claude-code

Docker production knowledge base for PHP. Provides deployment patterns, health checks, graceful shutdown, logging, and monitoring.

docker-php-extensions-knowledge

59
from dykyi-roman/awesome-claude-code

Docker PHP extensions knowledge base. Provides installation patterns for common extensions, build dependency management, and PECL usage.

docker-orchestration-knowledge

59
from dykyi-roman/awesome-claude-code

Docker orchestration knowledge base. Provides patterns for Swarm, Kubernetes basics, service scaling, and load balancing for PHP.