Skip to content

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.

  • 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 main branch. This tutorial uses ~/code/storefront and ~/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.

  1. Write the template.

    A template is a directory with a localspace.yaml file. 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 branch
    repos:
    storefront: ~/code/storefront
    orders-api: ~/code/orders-api
    services:
    storefront:
    repo: storefront
    port: 3000 # the port that the service listens on, which it gets as $PORT
    expose: true # the browser reaches the service through a public URL
    env:
    ORDERS_URL: ${orders.public_url}
    orders:
    repo: orders-api
    port: 8080
    expose: true
    setup: pnpm prisma migrate deploy # runs before each start
    env:
    DATABASE_URL: ${orders-db.url}
    REDIS_URL: ${cache.url}
    resources:
    orders-db:
    kind: postgres
    version: 16
    cache:
    kind: redis

    The repos field 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.

  2. Check the template.

    Terminal window
    lo template validate shop

    The command checks the fields, the values and the references. It shows each problem with its line in the file. It starts nothing.

  3. Make the first workspace.

    Terminal window
    lo new shop-a --template shop

    lo new records the workspace shop-a. Each service takes the branch main, which is the default of the template. The command does not start anything.

  4. Start the workspace.

    Terminal window
    lo up shop-a

    lo up makes 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.

  5. List the workspaces.

    Terminal window
    lo ls

    The output has a line for each workspace with its template, its status and the number of services that run.

  6. Open the storefront.

    Terminal window
    lo open shop-a storefront

    The 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 in ORDERS_URL.

  7. Read the logs.

    Terminal window
    lo logs shop-a

    The command prints the last lines of each service and each resource. Add the name of a service to read only that service, and add -f to follow new lines.

  8. Start a second workspace on a feature branch.

    Terminal window
    lo new shop-b --template shop storefront=feature/new-checkout
    lo up shop-b

    The storefront of shop-b runs from the branch feature/new-checkout. The orders API runs from main. The two workspaces run at the same time. They have different ports, databases and URLs, so they do not collide. Run lo ls again to see the two workspaces.

  9. Stop the workspaces.

    Terminal window
    lo down shop-a
    lo down shop-b

    lo down stops the services and the resources. It keeps the worktrees, the data and the ports. Run lo up shop-a to start the workspace again.

  10. Remove the workspaces.

    Terminal window
    lo rm shop-a
    lo rm shop-b

    lo rm stops the workspace, deletes its resources and removes the worktrees that localspace made. It forgets the workspace.

  • 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 ui to see the two workspaces in the web dashboard.