Run your first workspace
In this tutorial, you write a template for a small shop. The shop has a storefront and an orders API in two repositories. The orders API uses a Postgres database and a Redis. Then you run two workspaces of the shop at the same time.
Before you start
Section titled “Before you start”- Install localspace and run
lo setup. - Start Docker. The database, the Redis and the proxy run in containers.
- Have two git repositories on your machine, each with a
mainbranch. This tutorial uses~/code/storefrontand~/code/orders-api. The storefront listens on port 3000. The orders API listens on port 8080 and uses Prisma.
If your repositories are in other directories or use other ports, change the template in step 1.
-
Write the template.
A template is a directory with a
localspace.yamlfile. localspace reads the templates from~/.localspace/templates/. Make the directory and the file:Terminal window mkdir -p ~/.localspace/templates/shop~/.localspace/templates/shop/localspace.yaml defaults:ref: main # the branch of a service when you select no other branchrepos:storefront: ~/code/storefrontorders-api: ~/code/orders-apiservices:storefront:repo: storefrontport: 3000 # the port that the service listens on, which it gets as $PORTexpose: true # the browser reaches the service through a public URLenv:ORDERS_URL: ${orders.public_url}orders:repo: orders-apiport: 8080expose: truesetup: pnpm prisma migrate deploy # runs before each startenv:DATABASE_URL: ${orders-db.url}REDIS_URL: ${cache.url}resources:orders-db:kind: postgresversion: 16cache:kind: redisThe
reposfield gives the path of each repository. Each service names its repository, the port that it listens on and the variables of its environment. A value such as${orders-db.url}is a reference. localspace gives the reference a different value in each workspace. -
Check the template.
Terminal window lo template validate shopThe command checks the fields, the values and the references. It shows each problem with its line in the file. It starts nothing.
-
Make the first workspace.
Terminal window lo new shop-a --template shoplo newrecords the workspaceshop-a. Each service takes the branchmain, which is the default of the template. The command does not start anything. -
Start the workspace.
Terminal window lo up shop-alo upmakes a git worktree for each repository and gives each service a free host port. It starts the database and the Redis, and it makes the environment of each service. Then it starts the services and waits until each service is ready. The progress shows in the terminal. -
List the workspaces.
Terminal window lo lsThe output has a line for each workspace with its template, its status and the number of services that run.
-
Open the storefront.
Terminal window lo open shop-a storefrontThe command opens the public URL of the storefront in your browser. The URL is similar to
https://storefront.shop-a.lo. The storefront gets the URL of the orders API of the same workspace inORDERS_URL. -
Read the logs.
Terminal window lo logs shop-aThe command prints the last lines of each service and each resource. Add the name of a service to read only that service, and add
-fto follow new lines. -
Start a second workspace on a feature branch.
Terminal window lo new shop-b --template shop storefront=feature/new-checkoutlo up shop-bThe storefront of
shop-bruns from the branchfeature/new-checkout. The orders API runs frommain. The two workspaces run at the same time. They have different ports, databases and URLs, so they do not collide. Runlo lsagain to see the two workspaces. -
Stop the workspaces.
Terminal window lo down shop-alo down shop-blo downstops the services and the resources. It keeps the worktrees, the data and the ports. Runlo up shop-ato start the workspace again. -
Remove the workspaces.
Terminal window lo rm shop-alo rm shop-blo rmstops the workspace, deletes its resources and removes the worktrees that localspace made. It forgets the workspace.
Next steps
Section titled “Next steps”- Run one repository with no template.
- Read the guide to templates for the full list of fields.
- Read the guide to branches to switch a running workspace to other branches.
- Run
lo uito see the two workspaces in the web dashboard.