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:

  1. the service's environment: and env_file: in the stack file;
  2. /config/common.env, a file you usually mount from a Swarm config;
  3. /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#

ini
# /config/common.env
APP_ENV=production
LOG_CHANNEL=stack
DB_HOST=postgres
CACHE_STORE=redis

FEATURE_FLAGS=search,exports
  • One KEY=VALUE per line. The key must match ^[A-Za-z_][A-Za-z0-9_]*$.
  • Blank lines and lines starting with # are skipped. A trailing \r is ignored, so CRLF files work.
  • The value is everything after the first =, taken literally: no quotes are stripped, nothing is expanded, and there is no export. NAME="shop" sets the value "shop", with the quotes.
  • A line without =, or with an invalid key, stops the container before the app starts, with Invalid 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#

yaml
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-v7

The 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:

console
$ docker config create shop-common-env-v4 common.env
$ docker secret create shop-app-secrets-v8 app-secrets.env

Swarm 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#

VariableValueWhy
SSL_MODEoffserversideup/php's TLS switch. TLS is expected to end at the reverse proxy in front of Swarm.
CNB_APP_DIR/workspaceWhere the app lives.

PHP settings that serversideup/php reads from the environment in its php.ini work as usual, for example:

VariableSets
PHP_MEMORY_LIMITmemory_limit
PHP_MAX_EXECUTION_TIMEmax_execution_time
PHP_OPCACHE_ENABLE, PHP_OPCACHE_ENABLE_CLIopcache.enable, opcache.enable_cli
PHP_UPLOAD_MAX_FILE_SIZE, PHP_POST_MAX_SIZEupload limits
PHP_DATE_TIMEZONEdate.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.