Getting Started Experimental
From a Laravel app to a running Docker Swarm stack with velyr-pack, step by step.
This guide takes a Laravel app with Octane from source to a running Docker Swarm stack: a web service behind a healthcheck, a queue worker and a scheduler. It takes about fifteen minutes, most of it the first image build.
What you need#
- A Laravel app whose
composer.jsonrequireslaravel/frameworkandlaravel/octane. pack0.40 or newer and a Docker daemon to build the image.- A Docker Swarm to deploy to (
docker swarm initon one machine is enough to try it). - A registry the Swarm nodes can pull from. This guide uses
ghcr.io/you/shop. - The
velyr-packCLI, see Install.
1. Declare the processes#
Create velyr.toml at the root of the app. A process is something the image can run; health marks the one
that serves HTTP:
# velyr.toml
[processes.web]
command = ["php", "artisan", "octane:start", "--server=frankenphp", "--host=0.0.0.0", "--port=8000"]
health = { path = "/up", port = 8000, start_period = "60s" }
[processes.worker]
command = ["php", "artisan", "queue:work", "--max-time=3600"]
grace = "60s"
[processes.scheduler]
command = ["php", "artisan", "schedule:work"]/up is the health route Laravel 11 and newer register in bootstrap/app.php. If your app does not have it,
point health.path at any route that answers 200 without a database round trip.
grace is how long a worker gets to finish its current job when Swarm stops it.
2. Keep local files out of the image#
Create project.toml next to it. It plays the role of .dockerignore for pack build:
# project.toml
[_]
schema-version = "0.2"
[io.buildpacks]
exclude = [
".git", "node_modules", "vendor", "tests",
"storage/logs/*", "storage/framework/cache/*", "storage/framework/sessions/*", "storage/framework/views/*",
"bootstrap/cache/*.php", ".env", ".env.*",
]Excluding vendor matters: the buildpack runs composer install --no-dev itself and caches it between builds.
3. Build the image#
Pick the builder for your PHP version. The builder tag is the velyr-pack release plus the PHP minor:
$ pack build ghcr.io/you/shop:1 \
--builder ghcr.io/velyrlabs/velyr-php-builder:v0.1.0-alpha.4-php8.5 \
--publish--publish pushes straight to the registry; drop it to load the image into the local Docker daemon instead.
The first build downloads the builder and compiles nothing: PHP and its extensions come prebuilt in the image.
Later builds reuse the Composer cache.
Check that the image declares your processes:
$ docker run --rm --entrypoint /cnb/process/web ghcr.io/you/shop:1 --helpEach process is an executable at /cnb/process/<name>, and web is the default because it is the only process
with health, so docker run ghcr.io/you/shop:1 starts Octane.
4. Write the stack file#
Create stack.yml. Everything shared by the app services goes in an anchor that you write by hand; the services
themselves go between the two markers, which must be indented two spaces under services::
# stack.yml
x-app: &app
image: ghcr.io/you/shop:${IMAGE_TAG:-1}
environment:
APP_ENV: production
APP_KEY: ${APP_KEY}
DB_HOST: postgres
REDIS_HOST: redis
networks: [internal]
services:
# velyr:begin
# velyr:end
redis:
image: redis:7-alpine
networks: [internal]
networks:
internal:
driver: overlayA real deployment also needs a database and a reverse proxy in front of web; they are ordinary hand-written
services, and velyr-pack does not touch them.
5. Declare the environment and generate the services#
Add an environment to velyr.toml. It names the stack file, says the image was built by a velyr builder, and
lists one entry per Swarm service:
[envs.production]
file = "stack.yml"
image = "cnb"
service_extends = ["*app"]
cpus = "0.5"
memory = "512M"
services = [
{ name = "web", process = "web", replicas = 2, cpus = "2", memory = "1G" },
{ name = "worker", process = "worker", args = ["--queue=default"] },
{ name = "scheduler", process = "scheduler", note = "Exactly one replica: never scale the scheduler" },
]Generate:
$ velyr-pack compose productionstack.yml now has the three services between the markers. The worker looks like this:
worker:
<<: *app
init: true
entrypoint: ["/cnb/process/worker"]
command: ["--queue=default"]
stop_grace_period: 60s
deploy:
mode: replicated
replicas: 1
update_config: {parallelism: 1, order: stop-first, failure_action: rollback, monitor: 30s}
rollback_config: {parallelism: 1, order: stop-first, failure_action: pause}
resources:
limits:
cpus: "0.5"
memory: 512Margs became the container command: the image runs php artisan queue:work --max-time=3600 --queue=default.
The web service also gets an HTTP healthcheck and order: start-first. Rollout policy explains
every line.
Commit velyr.toml and stack.yml together. The generated block is meant to be read and reviewed, not edited.
6. Deploy#
$ APP_KEY=base64:... docker stack deploy -c stack.yml shop
$ docker service ls --filter label=com.docker.stack.namespace=shopdocker stack deploy returns as soon as Swarm accepts the change. To know that a rollout finished, watch the
update state of a service until it reads completed:
$ docker service inspect shop_web --format '{{.UpdateStatus.State}}'
completed7. Ship a change#
To release new code, build a new tag and deploy it:
$ pack build ghcr.io/you/shop:2 --builder ghcr.io/velyrlabs/velyr-php-builder:v0.1.0-alpha.4-php8.5 --publish
$ IMAGE_TAG=2 APP_KEY=base64:... docker stack deploy -c stack.yml shopSwarm replaces web one task at a time and keeps the old task serving until the new one is healthy; the worker
and the scheduler stop before their replacement starts. A web task that never becomes healthy rolls the service
back to tag 1.
To change the deployment itself (a replica count, a limit, a new worker), edit velyr.toml, run
velyr-pack compose production again and deploy.
Next steps#
- Keep secrets out of the stack file with Swarm configs and secrets.
- Run migrations, cache the config at boot and run many consumers from one process: Recipes.
- Build, check and deploy from CI: CI/CD.
- Every key of the manifest: velyr.toml reference.