Recipes Experimental
Patterns for Laravel apps on velyr-pack: config cache, migrations, one-off commands, schedulers, storage and more.
Patterns for running a Laravel app with velyr-pack. Each one is a few lines of velyr.toml or stack file.
Cache the config at boot#
The buildpack leaves no config cache, because the config depends on the runtime environment. To cache it when a
container starts, wrap the process command in a small script that runs before exec:
#!/bin/sh
# docker/boot.sh: cache the config with the runtime environment, then run the process.
set -e
php artisan config:cache --no-interaction
exec "$@"[processes.web]
command = ["sh", "docker/boot.sh", "php", "artisan", "octane:start", "--server=frankenphp", "--host=0.0.0.0", "--port=8000"]
health = { path = "/up", port = 8000, start_period = "120s" }
[processes.scheduler]
command = ["sh", "docker/boot.sh", "php", "artisan", "schedule:work"]exec replaces the shell with the process, so signals reach PHP and the grace period works. The script runs
after the image has loaded common.env and app_secrets, so the cache holds the real
values. Count the extra boot time in start_period.
This is also the place for anything the serversideup entrypoint used to do with AUTORUN_*: event:cache,
view:cache, storage:link.
Run one-off artisan commands#
Services of a cnb environment can only run declared processes. Declare a catch-all once:
# Any other artisan command: the arguments are the command.
[processes.artisan]
command = ["php", "artisan"]Then every service or one-off run passes its command as arguments:
{ name = "horizon", process = "artisan", args = ["horizon"] },
{ name = "reverb", process = "artisan", args = ["reverb:start", "--port=8080"] },$ docker run --rm --entrypoint /cnb/process/artisan ghcr.io/you/shop:2 aboutRun migrations#
Run migrations as a Swarm job before deploying the new version, on the stack's network and with the same configuration as the app:
$ docker service create --name shop-migrate-2 \
--mode replicated-job \
--network shop_internal \
--config source=shop-common-env-v4,target=/config/common.env \
--secret source=shop-app-secrets-v8,target=app_secrets,uid=33,gid=33,mode=0400 \
--entrypoint /cnb/process/artisan \
ghcr.io/you/shop:2 migrate --force
$ docker service logs -f shop-migrate-2
$ docker service rm shop-migrate-2It uses the artisan process above. A replicated job runs its task once and stops; the deploy step can wait for
it, check that it succeeded and then run docker stack deploy. Write migrations that the previous version can
still run against, because start-first keeps old web tasks serving while new ones start.
Do not run migrations from the web process at boot: with two or more replicas they race.
One process, many services#
A process takes arguments, so one declaration covers every queue or consumer group:
[processes.consume]
command = ["php", "artisan", "queue:work", "--max-time=3600"]
grace = "120s"
[envs.production]
# ...
services = [
{ name = "queue-default", process = "consume", args = ["--queue=default"], replicas = 3 },
{ name = "queue-mail", process = "consume", args = ["--queue=mail"], memory = "256M" },
{ name = "queue-reports", process = "consume", args = ["--queue=reports"], memory = "1G", grace = "15m" },
]Each service still gets its own replicas, limits and grace period, and they all share the rollout policy.
A scheduler that runs once#
[processes.scheduler]
command = ["php", "artisan", "schedule:work"]
# in the env
{ name = "scheduler", process = "scheduler", replicas = 1, cpus = "1", memory = "1G", note = "Exactly one replica: never scale the scheduler" },The scheduler is a worker, so it rolls out stop-first: the old task stops before the new one starts, and the
same minute never runs twice. Do not give it scale, and use onOneServer() for tasks if you ever run it in more
than one stack.
Size it for the top of the minute: every runInBackground() task boots the framework in a child process, and
they all start together. init: true, which every generated service has, reaps those children when they exit.
Persistent storage#
The app lives in /workspace. To share storage/ between services (uploaded files on local disk, a shared log
directory), mount a volume there:
{ name = "web", process = "web", volumes = ["storage:/workspace/storage"] },Declare the storage volume yourself, outside the generated block. Prefer object storage for uploads when you
have more than one node: a local volume only exists on the node that created it.
A custom Caddyfile#
Put your Caddyfile at docker/frankenphp/Caddyfile and the buildpack uses it instead of Octane's stub. For
another path, build with pack build --env BP_OCTANE_CADDYFILE=deploy/Caddyfile. Start from Octane's
vendor/laravel/octane/src/Commands/stubs/Caddyfile so the variables Octane passes keep working.
Images that need ffmpeg#
Build with the media run image, at the same release and PHP version as the builder:
$ pack build ghcr.io/you/shop:2 \
--builder ghcr.io/velyrlabs/velyr-php-builder:v0.1.0-alpha.4-php8.5 \
--run-image ghcr.io/velyrlabs/velyr-php-run-media:v0.1.0-alpha.4-php8.5Adopt velyr-pack in an existing stack#
You do not need the builder to start. With image = "dockerfile" (the default), velyr-pack writes command:
lines for any image:
- Wrap the services you want to manage with the markers, and describe them in
velyr.tomlwith your current image's commands. - Run
velyr-pack compose <env>and review the diff. Expect the rollout policy,init: trueand limits to be added; everything else should match what you had. - Deploy, then add
velyr-pack compose -checkto CI. - Later, build with the velyr-php builder, switch the environment to
image = "cnb"and regenerate. Only thecommand:andentrypoint:lines change.
Split a large stack#
One environment writes one file. When a stack grows past what you want in one deploy (or past a deploy tool's limits on file size or environment size), split it into several environments, each with its own stack file and the services that belong together:
[envs.web]
file = "stacks/web.yml"
# ...
[envs.workers]
file = "stacks/workers.yml"
# ...All environments share the same [processes], so the image is the same for every stack.