Docker Guide
OxPHP runs as a container. This guide covers building, configuring, and running it with Docker, from a minimal single-stage image to a multi-stage setup with separate development and production targets.
Minimal Dockerfile
The simplest way to containerize your application:
FROM ghcr.io/oxphp/oxphp:0.16.0
COPY --chown=www-data:www-data . /var/www/html/publicThis copies your application into the container. The default DOCUMENT_ROOT is /var/www/html/public. For Laravel, Symfony, or any project that already ships a public/ subdirectory, use COPY --chown=www-data:www-data . /var/www/html instead so the framework's own public/ aligns with the default. The server listens on port 80 by default.
Multi-stage Dockerfile
For real-world applications, use a multi-stage Dockerfile with separate dev and prod targets. The dev target includes PHP CLI, Composer, and Xdebug. The prod target builds on the minimal OxPHP image with only what's needed in production.
A ready-to-use version of this Dockerfile lives at examples/dockerfile/Dockerfile in the repository. It defaults to PHP 8.4 on Alpine 3.23 (ARG PHP_VERSION=8.4, ARG ALPINE_VERSION=3.23) and takes OxPHP from the matching -alpine3.23 tag, so build it with --build-arg PHP_VERSION=8.5 --build-arg ALPINE_VERSION=3.24 to match the walkthrough below. Its header lists what PHP 8.6 needs beyond PHP_VERSION=8.6. Copy it into your project and adjust the extensions to match your needs.
# ── Stage: php-base — shared PHP extensions ──────────────────
FROM php:8.5-zts-alpine3.24 AS php-base
RUN apk add --no-cache \
icu-dev \
icu-libs \
postgresql-dev \
libpq \
&& docker-php-ext-install \
pdo \
pdo_mysql \
pdo_pgsql \
intl \
&& apk del icu-dev postgresql-dev
# ── Stage: php-dev — add Xdebug on top of base ───────────────
FROM php-base AS php-dev
COPY --from=ghcr.io/php/pie:1-bin /pie /usr/local/bin/pie
RUN apk add --no-cache $PHPIZE_DEPS linux-headers unzip \
&& pie install --no-cache xdebug/xdebug \
&& apk del $PHPIZE_DEPS linux-headers unzip
# ── Stage: composer ───────────────────────────────────────────
FROM composer:2 AS composer
# ── Stage: oxphp — pull OxPHP artifacts ──────────────────────
FROM ghcr.io/oxphp/oxphp:0.16.0 AS oxphp
# ── Target: dev ──────────────────────────────────────────────
# Includes: PHP CLI, Composer, Xdebug, OxPHP binary + extension
FROM php-dev AS dev
RUN apk add --no-cache libgcc
# Composer
COPY --from=composer /usr/bin/composer /usr/local/bin/composer
# OxPHP binary
COPY --from=oxphp /usr/local/bin/oxphp /usr/local/bin/oxphp
# Bridge library
COPY --from=oxphp /usr/local/lib/liboxphp_bridge.so /usr/local/lib/
# OxPHP PHP extension
RUN EXT_DIR=$(php -r 'echo ini_get("extension_dir");') && \
echo "$EXT_DIR" > /tmp/ext_dir
COPY --from=oxphp /usr/local/lib/php/extensions/ /tmp/oxphp-ext/
RUN cp /tmp/oxphp-ext/*/oxphp_sapi.so "$(cat /tmp/ext_dir)/" && \
rm -rf /tmp/oxphp-ext /tmp/ext_dir
# PHP config
RUN echo "extension=oxphp_sapi.so" > /usr/local/etc/php/conf.d/oxphp-ext.ini
# Dev-friendly OPcache (validates timestamps)
RUN { \
echo "[opcache]"; \
echo "opcache.enable=1"; \
echo "opcache.enable_cli=1"; \
echo "opcache.validate_timestamps=1"; \
echo "opcache.revalidate_freq=0"; \
} > /usr/local/etc/php/conf.d/opcache-dev.ini
# Xdebug — connect back to host
RUN { \
echo "[xdebug]"; \
echo "xdebug.mode=debug"; \
echo "xdebug.start_with_request=trigger"; \
echo "xdebug.client_host=host.docker.internal"; \
echo "xdebug.client_port=9003"; \
} > /usr/local/etc/php/conf.d/xdebug-config.ini
RUN adduser -D -H -u 82 -G www-data -s /sbin/nologin www-data 2>/dev/null || true
RUN mkdir -p /var/www/html/public && chown -R www-data:www-data /var/www/html
ENV LD_LIBRARY_PATH=/usr/local/lib
# Framework layout: project ships a public/ subdir (Laravel/Symfony/Slim).
# For a bare index.php at the project root, copy into /var/www/html/public instead.
COPY --chown=www-data:www-data . /var/www/html
EXPOSE 80 443
CMD ["oxphp"]
# ── Stage: prod-extensions — compile extensions for prod ─────
FROM php-base AS prod-extensions
RUN EXT_DIR=$(php -r 'echo ini_get("extension_dir");') && \
mkdir -p /ext-out && \
cp "$EXT_DIR"/pdo.so \
"$EXT_DIR"/pdo_mysql.so \
"$EXT_DIR"/pdo_pgsql.so \
"$EXT_DIR"/intl.so \
/ext-out/
# ── Target: prod — minimal, based on OxPHP image ─────────────
FROM oxphp AS prod
USER root
RUN apk add --no-cache icu-libs libpq
COPY --from=prod-extensions /ext-out/*.so /usr/local/lib/php/extensions/no-debug-zts-20250925/
RUN { \
echo "extension=pdo_mysql.so"; \
echo "extension=pdo_pgsql.so"; \
echo "extension=intl.so"; \
} > /usr/local/etc/php/conf.d/app-extensions.ini
# Framework layout: project ships a public/ subdir (Laravel/Symfony/Slim).
# For a bare index.php at the project root, copy into /var/www/html/public instead.
COPY --chown=www-data:www-data . /var/www/html
USER www-data
EXPOSE 80 443
CMD ["oxphp"]Build each target:
# Development image (includes PHP CLI, Composer, Xdebug)
docker build --target dev -t myapp:dev .
# Production image (minimal)
docker build --target prod -t myapp:prod .The dev target is based on php:8.5-zts-alpine3.24, matching the default :0.16.0 tag, with OxPHP copied in, so you get full access to PHP CLI and Composer. The prod target is based on the OxPHP image directly, which keeps the production image small.
The php-dev stage installs Xdebug with PIE, copied from ghcr.io/php/pie:1-bin, which needs unzip while it runs. The official PHP images ship no pecl from 8.6 on. A Dockerfile that still calls pecl install keeps working on PHP 8.4 and 8.5.
To use another OxPHP tag in the oxphp stage, change the PHP stages to match it:
- For a
:*-php8.4*tag, swap8.5for8.4inphp-baseandno-debug-zts-20250925forno-debug-zts-20240924in theprodtarget. - For a
:*-php8.6*tag (0.16.0 or later), basephp-baseonphp:8.6-alpine3.24. Every official 8.6 image is ZTS, and the tags dropped-zts. In theprodtarget, use the extension directory thatphp -r 'echo ini_get("extension_dir");'prints in that image. - For an
-alpine3.23tag, basephp-baseonphp:<minor>-zts-alpine3.23. Extensions built on one Alpine version do not load on the other: anintl.sobuilt on 3.23 needslibicuuc.so.76, which 3.24 does not have. - For a
-trixietag, base the stages onphp:<minor>-zts-trixie(php:8.6-trixiefor 8.6) and useapt-get; see Debian (glibc) image.
PHP 8.6 needs two more changes. Leave Xdebug out of php-dev, or use a pre-release of it: Xdebug 3.5 requires PHP older than 8.6. If your application needs ext-mbstring, add mbstring to php-base (with oniguruma-dev to build it and oniguruma to keep), because the official 8.6 images ship without it. The prod target already has it, since the OxPHP 8.6 images add mbstring and zip back.
Installing PHP extensions in production
The production image ships the PHP toolchain (php, docker-php-ext-install, phpize) of the PHP image it is built on: php:8.5-zts-alpine by default, php:8.4-zts-alpine or php:8.6-alpine for the :*-php8.4* and :*-php8.6* tags, and php:<minor>-zts-trixie or php:8.6-trixie for the -trixie ones. It sets no USER directive, so your Dockerfile can install PHP extensions directly.
Quick-start pattern (single stage)
The shortest useful example:
FROM ghcr.io/oxphp/oxphp:0.16.0
RUN docker-php-ext-install mysqli pdo_mysql
COPY --chown=www-data:www-data . /var/www/html/public
CMD ["oxphp"]--chown=www-data:www-data on the COPY is important: files are owned by www-data (uid 82, or 33 in the Debian image) inside the image, so an orchestrator-level --user www-data drop lands on a webroot the unprivileged process can read and (where needed) write to.
The container starts as root, and oxphp serve then drops to www-data by default before serving any traffic (see the security note below). Pin the identity explicitly at the orchestrator level if you want a specific uid or a non-www-data user.
Best practice pattern (two stages, smaller image)
Installing an extension in the final image keeps its build packages (-dev libraries) there too. For the smallest image, compile extensions in a dedicated builder stage and copy only the compiled .so files into the runtime stage. The Multi-stage Dockerfile walkthrough above does this, bases prod on ghcr.io/oxphp/oxphp:0.16.0, and is the recommended starting point.
examples/dockerfile/Dockerfile in the repository goes further: its prod target is based on bare alpine with an explicit apk dependency list, copying only the oxphp binary, libphp.so, compiled PHP extensions, and the required shared libraries. This cuts the base image from ~188 MB down to ~76 MB (~60% reduction, excluding your app code) at the cost of tracking PHP/Alpine version bumps in the apk list. The same file also ships a prod-cli target, a short-lived image for php artisan migrate, Composer, and other maintenance commands that should stay out of the serving path.
The walkthrough above shows explicit USER root / USER www-data toggles for defense-in-depth. They are optional, since the base image sets no USER.
Running CLI tools and migrations
The same prod image can run php CLI commands for migrations, Composer, or ad-hoc inspection. docker run replaces the default CMD with the command you pass: the container runs the command and exits without also starting the OxPHP server.
# Run Laravel migrations against the prod image.
# Container runs as root by default — the CLI has write access to
# root-owned mounted volumes.
docker run --rm \
-v "$(pwd):/var/www/html" \
ghcr.io/oxphp/oxphp:0.16.0 \
php artisan migrate
# If the mounted volume is owned by www-data, pass Docker's --user:
docker run --rm --user www-data \
-v "$(pwd):/var/www/html" \
ghcr.io/oxphp/oxphp:0.16.0 \
php artisan migratedocker exec <container> docker-php-ext-install <ext> also works on a running container without any additional flags, which is useful for debugging a live container. For production, persist the extension in your Dockerfile so it survives restarts.
Running as root or non-root
Security note
The prod image has no USER directive, so the container starts as root, as nginx:alpine, php:*-fpm-alpine and frankenphp:alpine do. Root lets OxPHP bind privileged ports, but traffic is not served as root: oxphp serve and oxphp run drop to www-data by default. They bind as root, then drop permanently before any request is handled or any PHP worker runs. The official image ships the www-data account, so this needs no configuration.
To run as a specific uid or a user other than www-data, or for extra defense-in-depth, pin the identity at the orchestrator level:
docker run --user www-data ghcr.io/oxphp/oxphp:0.16.0services:
oxphp:
image: ghcr.io/oxphp/oxphp:0.16.0
user: www-datasecurityContext:
runAsNonRoot: true
runAsUser: 82
runAsGroup: 82runAsNonRoot: true is defense-in-depth: if runAsUser is ever removed or overridden to 0, the kubelet rejects the pod instead of silently running as root.
A container started as non-root this way skips the default drop, since it is already unprivileged, and cannot bind ports below 1024. To keep a privileged bind and serve as non-root, see Run as non-root on port 80. To keep serving as root on purpose, pass oxphp serve --user=root.
The www-data user (uid/gid 82; 33 in the Debian image, where runAsUser and runAsGroup above become 33) is pre-created by the base image, and /var/www/html is chowned to it at build time, so any of these drop paths, including the default self-drop, lands on a readable webroot.
CLI invocations like docker run … php artisan migrate run the php binary directly, not oxphp serve/run, so they do not self-drop; they run as the container's start user (root by default). Use Docker's --user for those, as shown above.
Run as non-root on port 80 (serve --user)
Dropping privileges at the orchestrator level (above) has one limitation: a process that starts as www-data cannot bind a privileged port (below 1024). To serve on :80/:443 you would otherwise need CAP_NET_BIND_SERVICE, a su-exec-style entrypoint, or a high port (such as :8080) behind a port mapping.
OxPHP does both in one process: it binds the listeners as root, then drops permanently before it accepts a connection or runs a PHP worker. The default user is www-data, so on the official image a container started as root already serves a privileged port as www-data, with no flag and no extra capabilities. Use --user=<spec> to drop to a different user, or --user=root to keep root.
Start the container as root, and do not also set user:, because the bind needs root. The example below passes --user=www-data explicitly to be self-documenting, but it matches the default:
services:
oxphp:
image: ghcr.io/oxphp/oxphp:0.16.0
command: ["oxphp", "serve", "--user=www-data"]
ports:
- "80:80"
- "443:443"
environment:
- LISTEN_ADDR=0.0.0.0:80<spec> accepts a user name, name:group, a numeric uid, or uid:gid. The drop (initgroups, setgid, setuid) is irreversible: OxPHP checks that root cannot be regained, and on Linux it also sets no_new_privs. An explicit --user fails fast: started as non-root, serve --user exits with an error. The default drop is best-effort instead, and a process started as non-root skips it.
Choose one model, not both. Use the orchestrator drop (user: / runAsUser) when a high port or an external load balancer terminates :80. Use serve --user when you want OxPHP itself to own the privileged bind. Setting user: and serve --user makes the bind fail: the container is no longer root.
File-permission checklist for the dropped user. After the drop, everything OxPHP touches at runtime must be accessible to <spec>:
| Resource | Requirement |
|---|---|
DOCUMENT_ROOT |
Readable. /var/www/html is chowned to www-data at image build, so this is satisfied by default. |
Session save path (session.save_path, default /tmp) |
Writable. |
Upload tmp dir (upload_tmp_dir) |
Writable, when file uploads are used. |
Response spool directory (TMPDIR, default /tmp) |
Writable. Otherwise every PHP response that does not stream and is larger than RESPONSE_BUFFER_BYTES is answered with 503, and the server logs a warning at startup. See Response buffering. |
OPcache file cache (opcache.file_cache) |
Writable, when a secondary file cache is enabled. |
| File-based access log | Writable, when logging to a file rather than stdout. |
TLS private key (TLS_KEY) |
Readable by the dropped user, which means group- or world-readable, not root-only 0600. The key is read after the drop, so a root-only key makes TLS startup fail. |
Docker Compose
services:
oxphp:
build:
context: .
target: prod
ports:
- "80:80"
- "443:443"
- "9090:9090"
environment:
- LISTEN_ADDR=0.0.0.0:80
- DOCUMENT_ROOT=/var/www/html/public
- ENTRY_FILE=index.php
- INTERNAL_ADDR=0.0.0.0:9090
- LOG_LEVEL=info
- ACCESS_LOG=error
- PHP_WORKERS=4
- DRAIN_TIMEOUT_SECONDS=25
- COMPRESSION_BROTLI_LEVEL=5
restart: unless-stoppedMount your source directory as a volume so file changes are reflected without rebuilding. The dev target has OPcache timestamp validation enabled, so PHP picks up changes automatically.
services:
oxphp:
build:
context: .
target: dev
ports:
- "80:80"
- "9090:9090"
volumes:
- ./src:/var/www/html:ro
- ./custom.ini:/usr/local/etc/php/conf.d/custom.ini:ro
environment:
- LISTEN_ADDR=0.0.0.0:80
- DOCUMENT_ROOT=/var/www/html/public
- ENTRY_FILE=index.php
- INTERNAL_ADDR=0.0.0.0:9090
- LOG_LEVEL=debug
- ACCESS_LOG=allVolume mounts
| Host Path | Container Path | Purpose |
|---|---|---|
./src |
/var/www/html |
Application files (PHP scripts, static assets). Use :ro in production |
./custom.ini |
/usr/local/etc/php/conf.d/custom.ini |
PHP runtime configuration (OPcache, sessions, JIT). Use :ro |
./certs |
/etc/ssl/oxphp |
TLS certificate and private key. Use :ro |
Port reference
| Port | Environment Variable | Purpose |
|---|---|---|
80 |
LISTEN_ADDR |
Main HTTP server |
443 |
LISTEN_ADDR |
Main HTTPS server (when TLS is configured) |
9090 |
INTERNAL_ADDR |
Internal server: /health, /metrics, /config |
The internal server is disabled by default. Set INTERNAL_ADDR to enable it. In production, keep the internal port reachable only by your orchestrator or monitoring system; do not expose it publicly.
PHP configuration
Customize PHP settings by creating a custom.ini file and mounting it into the container. This is the recommended way to configure OPcache, JIT, sessions, and other PHP runtime settings.
; Do NOT add zend_extension=opcache — the image already loads OPcache.
; The [opcache] section below configures it directly.
[opcache]
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.validate_timestamps = 0
opcache.jit_buffer_size = 64M
opcache.jit = tracing
[Session]
session.save_path = /tmp
session.use_cookies = 1
session.use_only_cookies = 1Do not add zend_extension=opcache to this file. The OxPHP image already loads OPcache: it is compiled into PHP on 8.5 and 8.6, and on 8.4 the PHP base image's conf.d/docker-php-ext-opcache.ini loads it. The extra line costs a message every time PHP starts.
In development, set opcache.validate_timestamps = 1 and opcache.revalidate_freq = 0 so PHP picks up file changes without a container restart.
See OPcache for recommended settings and JIT configuration.
Memory limits
PHP counts memory per thread: each worker, and each thread of the oxphp_async() pool, has its own memory_limit. Running past it raises PHP's Allowed memory size … exhausted fatal error, and the server goes on.
From PHP 8.4.27 and 8.5.12 on, PHP also has a hard limit above it: memory_hard_limit_percent percent of memory_limit. By default that is twice the limit, or the limit plus 1 GiB for a limit above 1 GiB. An allocation that would take a thread's memory past the hard limit makes PHP end the process on the spot, and the server has no hook to stop it. The exception is an allocation PHP is able to refuse, such as str_repeat() asked for more than is left, which fails with the usual error.
Under PHP-FPM the hard limit ends one child process. Under OxPHP it ends the server, in every routing mode, and every request in flight on every worker ends with it. The process exits with code 137, and the only trace is one line on stderr:
Hard memory limit of 134217728 bytes exhausted (tried to allocate 125833216 bytes) in /var/www/html/public/report.php on line 7The line number in it can be wrong. PHP gives the last line it recorded, and some operations (a concatenation such as $s . $s among them) allocate without recording their own, so the line given can be that of a statement that ran earlier.
One large allocation is enough to cross the hard limit, such as doubling a string that takes nearly all of memory_limit ($s . $s). So is the last of many small ones made by an internal function that keeps allocating before PHP gets to report the error.
To move the hard limit, set memory_hard_limit_percent (100 or more, or -1 for the default) in your own .ini file:
memory_limit = 128M
memory_hard_limit_percent = 400The lower the value, the closer the hard limit sits to memory_limit. At 100 the two are equal, and running past memory_limit ends the server unless PHP can refuse the allocation. A higher value only moves the limit: a request that overshoots memory_limit but stays under the new hard limit ends with the usual fatal error instead of ending the server, as long as the memory is there.
Each thread's PHP memory may now grow to memory_limit × memory_hard_limit_percent / 100 before PHP steps in, so size it against what the container has to spare beyond the other workers' usual use. Where the memory is not there, the kernel ends the process when the container runs out, with the same exit code 137 and without PHP's line naming the script. Docker reports the container as OOMKilled.
Bringing the server back after such an exit is left to the container runtime or process supervisor, for example restart: unless-stopped in the production Compose example above.
Health checks
Add a Docker health check to let Docker or your orchestrator monitor container health. This requires INTERNAL_ADDR to be set.
services:
oxphp:
environment:
- INTERNAL_ADDR=0.0.0.0:9090
healthcheck:
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:9090/health"]
interval: 10s
timeout: 5s
retries: 3
start_period: 5sHEALTHCHECK --interval=10s --timeout=5s --retries=3 --start-period=5s \
CMD wget --quiet --tries=1 --spider http://localhost:9090/health || exit 1The /health endpoint returns 200 when the server is healthy and 503 when degraded. The response JSON includes uptime, total request count, and active connection count. For Kubernetes, use the dedicated probe endpoints instead (/health/liveness, /health/readiness and /health/startup), each built for its own probe type; see Health Checks.
Prompts for your agent
These prompts assume an agent with the OxPHP documentation MCP server connected. The agent reads the pages it needs itself.
Write me a Dockerfile and a docker-compose.yml that run this project on OxPHP. Read the `getting-started/docker` page through the OxPHP documentation MCP server.
Keep the services my application already depends on, install the PHP extensions it actually needs, and explain any OxPHP-specific line you add.Review my Dockerfile for running on OxPHP. Read the `getting-started/docker` page through the OxPHP documentation MCP server.
Tell me what will break or waste space in production, and show me the corrected file.What's next
- Configuration: full environment variable reference
- Routing: Traditional, Framework, SPA, and Worker routing modes
- Worker Mode: persistent PHP workers for framework applications
- TLS: HTTPS with built-in TLS termination
- Health Checks: health endpoint details and Kubernetes integration
- Graceful Shutdown: drain behavior and shutdown sequence