Template examples
This page gives complete templates for common systems. Each example is correct, and you can copy it. Change the paths, the names and the commands for your code. For each field, see the template reference. To learn how to write a template, see the templates guide.
Use an example
Section titled “Use an example”-
Save the YAML as
localspace.yamlin a directory with the name of the template, for example~/.localspace/templates/shopping/localspace.yaml. -
Change each path in
repos:to a git repository on your machine. -
Run
lo template validate <template>. The command starts nothing. -
Make a workspace and start it:
Terminal window lo new <workspace> --template <template>lo up <workspace>
The first example is different. It is a .localspace.yaml file in a repository, and it has no repos:.
The tables in the examples show the values that the services get with the default machine settings. A value in angle brackets, such as <port>, is different in each workspace.
One repository with a web service and a database
Section titled “One repository with a web service and a database”A small system has one repository. The file .localspace.yaml is at the root of the repository, and lo up runs it in a git worktree. The workspace has the name of the branch. This example uses setup, command, a seed file and two references.
services: web: port: 3000 expose: true setup: pnpm prisma migrate deploy command: pnpm exec next dev --port $PORT env: DATABASE_URL: ${db.url} NEXTAUTH_URL: ${web.public_url}
resources: db: kind: postgres seed: db/seed.sqlCommit the file and db/seed.sql, so that each worktree has them. Then start a workspace for a branch:
git worktree add ../shop-feature-x -b feature/xcd ../shop-feature-xlo upThe workspace is feature-x. Run lo down in the same worktree to stop it. See run one repository.
| Variable | Value in the workspace feature-x |
|---|---|
PORT |
The host port of web. |
DATABASE_URL |
postgres://postgres@127.0.0.1:<port>/db?sslmode=disable |
NEXTAUTH_URL |
https://web.feature-x.lo |
Web, API and worker in three repositories
Section titled “Web, API and worker in three repositories”A larger system has one repository for each part. This template has a web app, an API and a worker. The API and the worker use the same Postgres database and the same Redis server. The example uses depends_on, ${api.internal_url} and ${api.public_url}.
defaults: ref: main
repos: web: ~/code/web api: ~/code/api worker: ~/code/worker
services: web: repo: web port: 3000 expose: true depends_on: [api] env: API_URL: ${api.internal_url} NEXT_PUBLIC_API_URL: ${api.public_url}
api: repo: api port: 8080 expose: true depends_on: [db, cache] setup: pnpm prisma migrate deploy ready: /health env: DATABASE_URL: ${db.url} REDIS_URL: ${cache.url} CORS_ORIGIN: ${web.public_url}
worker: repo: worker depends_on: [db, cache, api] command: pnpm exec tsx src/main.ts env: DATABASE_URL: ${db.url} REDIS_URL: ${cache.url} API_URL: ${api.internal_url}
resources: db: kind: postgres version: 16 seed: run: pnpm prisma db seed service: api cache: kind: redisThe server code of web calls the API with the internal URL. The browser cannot reach an internal URL, so NEXT_PUBLIC_API_URL has the public URL.
depends_on sets the start order: db and cache, then api, then web and worker. The seed command runs in api after its setup.
lo new shop-a --template shoplo up shop-a| Variable | Value in the workspace shop-a |
|---|---|
API_URL of web |
http://127.0.0.1:<api port> |
NEXT_PUBLIC_API_URL |
https://api.shop-a.lo |
CORS_ORIGIN |
https://web.shop-a.lo |
REDIS_URL |
redis://127.0.0.1:<port>/0 |
A monorepo with pnpm and Turborepo
Section titled “A monorepo with pnpm and Turborepo”All the code is in one repository, and the services are two applications in it. The two services use repo: shop with a different dir. They run from the same worktree.
defaults: ref: main
repos: shop: ~/code/shop
services: api: repo: shop dir: apps/api port: 4000 install: false setup: cd ../.. && pnpm install --frozen-lockfile && pnpm turbo run build --filter=@shop/db command: pnpm exec tsx watch src/main.ts ready: /health env: DATABASE_URL: ${db.url}
web: repo: shop dir: apps/web port: 3000 expose: true install: false depends_on: [api] command: pnpm exec next dev --port $PORT env: API_URL: ${api.internal_url}
resources: db: kind: postgresA monorepo installs its packages one time at the root. install: false removes the install step that localspace finds for the directory of each service. The setup of api installs at the root and builds the shared package.
web has no setup. It starts after api, so the packages are in place, and only one install runs in the worktree.
lo new shop-a --template shop-monorepolo up shop-aContainers next to processes
Section titled “Containers next to processes”A system can have a service that you do not write, such as a PDF renderer. Run it from an image. A service from the repository of your system can also run as a container, if the repository has a Dockerfile.
defaults: ref: main
repos: api: ~/code/api reports: ~/code/reports
services: api: repo: api port: 8080 expose: true depends_on: [gotenberg, reports] env: PDF_URL: ${gotenberg.internal_url} REPORTS_URL: ${reports.internal_url}
reports: repo: reports run: container port: 9000 ready: /health rebuild_on: - Dockerfile - go.sum env: PDF_URL: ${gotenberg.internal_url}
gotenberg: image: gotenberg/gotenberg:8 port: 3000 ready: /healthgotenberg runs from a prebuilt image. It has no repo, so you cannot select a branch for it. reports runs as a container that localspace builds from the Dockerfile of the repository. The template has no build field.
When you switch the branch of reports with lo set, the service rebuilds if Dockerfile or go.sum changed.
api runs as a process when its repository has no Dockerfile. The address of gotenberg is different for each consumer:
| Consumer | PDF_URL |
|---|---|
api, a process |
http://127.0.0.1:<port> |
reports, a container |
http://gotenberg:3000 |
A shared service
Section titled “A shared service”A heavy service that does not change between workspaces can run one time for all of them. The author of the template marks it shareable. Each workspace selects if it shares the service.
defaults: ref: main
repos: storefront: ~/code/storefront customer-service: ~/code/customer-service
services: storefront: repo: storefront port: 3000 expose: true env: CUSTOMER_URL: ${customer.internal_url}
customer: repo: customer-service port: 8080 shareable: true env: DATABASE_URL: ${customer-db.url}
resources: customer-db: kind: postgrescustomer-db is only for customer, so it belongs to the shared instance. Two workspaces then share the service and its data:
lo new shop-a --template shop-shared --share customerlo new shop-b --template shop-shared --share customerlo up shop-alo up shop-blo shared lslo shared ls shows one instance, customer@main, with the workspaces shop-a and shop-b. In the two workspaces, CUSTOMER_URL is the address of that instance.
To share the service in each new workspace, write shared: true in place of shareable: true. To run a separate copy in one workspace, use --own customer. See shared services for the rules.
A service with a remote
Section titled “A service with a remote”A service can also run on staging. The template lists the remote, and each workspace selects where the service runs. This example uses remotes, default and attrs with a secret.
defaults: ref: main
repos: booking-service: ~/code/booking-service payment-service: ~/code/payment-service
services: booking: repo: booking-service port: 8080 attrs: api_key: dev-key remotes: staging: url: https://booking.staging.acme.com ready: /health attrs: api_key: ${secret:booking/staging_key} default: staging
payment: repo: payment-service port: 8081 depends_on: [booking] env: BOOKING_URL: ${booking.internal_url} BOOKING_API_KEY: ${booking.api_key}attrs of a service gives the value that the attribute has on your machine. Each remote must give the same attribute, so payment works with each choice.
-
Store the secret of the remote. The command reads the value from the standard input:
Terminal window printf '%s' '<key>' | lo secret set booking/staging_key -
Select where
bookingruns when you make the workspace:Terminal window lo new shop-staging --template shop-remotelo new shop-local --template shop-remote booking=locallo new shop-qa --template shop-remote booking=https://booking.qa.acme.com \--attr booking.api_key='${secret:booking/qa_key}'
The first workspace uses the default remote staging. The second workspace runs booking on your machine. The third workspace uses a URL that the template does not list.
| Variable | shop-staging |
shop-local |
shop-qa |
|---|---|---|---|
BOOKING_URL |
https://booking.staging.acme.com |
http://127.0.0.1:<port> |
https://booking.qa.acme.com |
BOOKING_API_KEY |
The secret, shown as ******** |
dev-key |
The secret of --attr |
localspace does not start or stop a remote. See use services that run elsewhere.
A database on a remote
Section titled “A database on a remote”A workspace can read a database on a test server, with no local database. The URL has a password, so keep it in a secret. A resource with a remote gives url, host, port and name from that URL.
defaults: ref: main
repos: orders-api: ~/code/orders-api
services: orders: repo: orders-api port: 8080 expose: true env: DATABASE_URL: ${orders-db.url}
resources: orders-db: kind: postgres seed: seeds/orders.sql remotes: staging: url: ${secret:orders-db/staging_url}-
Store the URL in the secret store:
Terminal window printf '%s' 'postgres://<user>:<password>@db.staging.acme.com:5432/orders' | lo secret set orders-db/staging_url -
Make the workspaces:
Terminal window lo new orders-local --template orders-remote-dblo new orders-staging --template orders-remote-db orders-db=remote:staging
orders-local has a local database that starts from seeds/orders.sql. In orders-staging, DATABASE_URL is the URL of the secret, and lo env shows ********. localspace does not seed or reset a remote database.
External parts
Section titled “External parts”Some parts are not services of the template, and they have no local copy. Examples are the sandbox of a payment provider and a search API. List them in the top-level external:. A reference reads them with their name, as for a service.
defaults: ref: main
repos: checkout: ~/code/checkout
services: checkout: repo: checkout port: 3000 expose: true env: STRIPE_API_URL: ${stripe.url} STRIPE_SECRET_KEY: ${stripe.key} STRIPE_WEBHOOK_SECRET: ${stripe.webhook_secret} SEARCH_URL: ${search.url} SEARCH_API_KEY: ${search.api_key}
external: stripe: url: https://api.stripe.com attrs: key: ${secret:stripe/test_key} webhook_secret: ${secret:stripe/webhook_secret} search: url: https://search.sandbox.acme.com ready: /health attrs: api_key: ${secret:search/sandbox_key}url gives ${stripe.url}. Each name in attrs gives an attribute, such as ${stripe.key}. A variable that has a secret shows as ******** in lo env.
Each part in this example is one remote, so it has a URL. The name of a part must not be the name of a service or a resource.
printf '%s' '<key>' | lo secret set stripe/test_keylo new checkout-a --template checkoutlo up checkout-aSee remotes and external parts.
Two templates that are linked
Section titled “Two templates that are linked”A backend and a frontend have separate templates. A frontend workspace uses the backend workspace that runs the same branch, and uses the backend on main if there is none. start: true starts a backend workspace that is stopped.
defaults: ref: main
repos: backend: ~/code/backend
services: api: repo: backend port: 8080 expose: true attrs: api_key: dev-key env: DATABASE_URL: ${db.url}
resources: db: kind: postgresdefaults: ref: main
repos: frontend: ~/code/frontend
external: api: from: - workspace: { template: backend, ref: same } - workspace: { template: backend, ref: main } service: api start: true
services: web: repo: frontend port: 3000 expose: true env: API_URL: ${api.internal_url} API_KEY: ${api.api_key} PUBLIC_API_URL: ${api.public_url}web reads api as it reads a service of the same template. API_URL is the host and the host port of the backend service. PUBLIC_API_URL is the public URL of that service for a browser. API_KEY is the value in the attrs of api.
lo new backend-main --template backendlo new backend-x --template backend --match feature-xlo new frontend-x --template frontend --match feature-xlo up frontend-xlo up frontend-x starts api in backend-x, then starts web. If there is no backend-x, it starts api in backend-main. See connect workspaces of different templates.
Custom host names
Section titled “Custom host names”A service can have a public URL in a domain that you select. Some systems must have this, for example for a cookie or an OAuth redirect. List the domain in domains: and give the service a host.
domains: [dev.acme.com]
defaults: ref: main
repos: api: ~/code/api front: ~/code/front
services: api: repo: api port: 8000 expose: true host: api.{workspace}.dev.acme.com env: CORS_ORIGIN: ${front.public_url} COOKIE_DOMAIN: .${workspace.slug}.dev.acme.com
front: repo: front port: 3000 expose: true host: front.{workspace}.dev.acme.com env: API_URL: ${api.public_url}{workspace} gives each workspace a separate name, so workspaces run side by side. A template cannot change the name resolution of your machine alone. Approve the domain one time:
lo proxy dns add dev.acme.comlo proxy dns setupYou can also add --yes to lo new. Then make the workspace:
lo new shop-a --template shop-domainlo up shop-a| Variable | Value in the workspace shop-a |
|---|---|
API_URL |
https://api.shop-a.dev.acme.com |
CORS_ORIGIN |
https://front.shop-a.dev.acme.com |
COOKIE_DOMAIN |
.shop-a.dev.acme.com |
The names are only for your machine. See a name in a domain that you select.
Repositories on different branches
Section titled “Repositories on different branches”The repositories of a system do not always use the same default branch. Give each repository a ref, and defaults.ref is the value for the rest.
defaults: ref: main
repos: api: ~/code/api web: path: ~/code/web ref: develop admin: path: ~/code/admin ref: release/next
services: api: repo: api port: 8080 expose: true web: repo: web port: 3000 expose: true env: API_URL: ${api.public_url} admin: repo: admin port: 3100 expose: true env: API_URL: ${api.public_url}A new workspace runs api from main, web from develop and admin from release/next. Use --match to put the repositories that have a branch for one ticket on that branch:
lo template branches shop-refs --match feature-xlo new shop-b --template shop-refs --match feature-xlo new shop-c --template shop-refs web=feature/other--match feature-x selects the one branch of each repository that has the word feature-x as a complete part of its name, such as feature-x or fix/feature-x. A repository with no such branch keeps its default ref. If a repository has more than one such branch, lo new fails and lists them. A pair, such as web=feature/other, selects a branch for one repository. See select branches.
Related
Section titled “Related”- Write a template shows how to build a template step by step.
- Template reference gives each field.
- References gives each
${...}form. - Use services that run elsewhere tells how remotes and external parts work.
- Connect workspaces of different templates tells how an external part reaches a service of a different workspace.