Runtime Configuration Experimental
How a velyr-php image reads its configuration at runtime: environment variables, Swarm configs and secrets.
A velyr-php image reads its configuration at runtime, never at build time: the buildpack leaves no config cache behind, so the same image runs in every environment. Configuration reaches the app in three ways, from lowest to highest precedence:
- the service's
environment:andenv_file:in the stack file; /config/common.env, a file you usually mount from a Swarm config;/run/secrets/app_secrets, a file you usually mount from a Swarm secret.
The two files are optional. When they exist, the image loads them right before the process starts, and a key in a
file overrides the same key from the container environment. A key in app_secrets overrides common.env.
Why files#
A stack with dozens of services that all need the same forty variables either repeats them or puts them in an anchor that every deploy re-sends. Swarm configs and secrets are stored once in the cluster, mounted into each task, and versioned by name. Secrets are encrypted at rest in the Raft log and only reach the nodes that run a task that uses them.
File format#
# /config/common.env
APP_ENV=production
LOG_CHANNEL=stack
DB_HOST=postgres
CACHE_STORE=redis
FEATURE_FLAGS=search,exports- One
KEY=VALUEper line. The key must match^[A-Za-z_][A-Za-z0-9_]*$. - Blank lines and lines starting with
#are skipped. A trailing\ris ignored, so CRLF files work. - The value is everything after the first
=, taken literally: no quotes are stripped, nothing is expanded, and there is noexport.NAME="shop"sets the value"shop", with the quotes. - A line without
=, or with an invalid key, stops the container before the app starts, withInvalid environment assignment in <file> at line <n>. - When a key repeats, the last value wins.
This is stricter than a .env file read by Laravel. Do not paste a .env with quoted values into a config
without removing the quotes.
Mounting them in Swarm#
x-app: &app
image: ghcr.io/you/shop:${IMAGE_TAG}
configs:
- source: common-env
target: /config/common.env
mode: 0444
secrets:
- source: app-secrets
target: app_secrets # mounted at /run/secrets/app_secrets
mode: 0400
uid: "33"
gid: "33"
configs:
common-env:
external: true
name: shop-common-env-v3
secrets:
app-secrets:
external: true
name: shop-app-secrets-v7The images run as uid 33 (www-data), so a secret with mode 0400 needs uid: "33". With Swarm's default
(owned by root, mode 0444) it is readable by everyone in the container.
Create a new version and point the stack at it:
$ docker config create shop-common-env-v4 common.env
$ docker secret create shop-app-secrets-v8 app-secrets.envSwarm configs and secrets are immutable. Changing what a service reads means creating a new name and deploying the stack with it, which also rolls the services out, so a configuration change goes through the same health-gated rollout as code. Remove old versions once nothing references them.
Environment variables in the stack file#
Variables in environment: still work and have the lowest precedence. Keep there what differs per service
(a worker's QUEUE_CONNECTION, a feature flag for one service), and keep shared and secret values in the files.
A service can reference a shared map with environment = "*common-env" in velyr.toml, and add files with
env_file = ["worker.env"]. Those are written into the generated block as is; see the
velyr.toml reference.
Variables the image sets#
| Variable | Value | Why |
|---|---|---|
SSL_MODE | off | serversideup/php's TLS switch. TLS is expected to end at the reverse proxy in front of Swarm. |
CNB_APP_DIR | /workspace | Where the app lives. |
PHP settings that serversideup/php reads from the environment in its php.ini work as usual, for example:
| Variable | Sets |
|---|---|
PHP_MEMORY_LIMIT | memory_limit |
PHP_MAX_EXECUTION_TIME | max_execution_time |
PHP_OPCACHE_ENABLE, PHP_OPCACHE_ENABLE_CLI | opcache.enable, opcache.enable_cli |
PHP_UPLOAD_MAX_FILE_SIZE, PHP_POST_MAX_SIZE | upload limits |
PHP_DATE_TIMEZONE | date.timezone |
The full list is in the serversideup/php documentation.
The AUTORUN_* variables have no effect: the serversideup entrypoint does not run in a CNB image.
Configuration cache#
Without a cached config, Laravel reads config/*.php and the environment on every boot. Octane boots once per
worker, so the cost is small for web, but short-lived artisan commands pay it every time. To cache the config
when a container starts, after the environment is in place, see
Recipes: cache the config at boot.
Never bake a config cache into the image: it would freeze the build environment's values into every deployment.