Use resources
A resource is a stateful dependency of a system, such as a database. Each workspace has a separate copy of each resource, so the data of one workspace does not change the data of a different workspace. This guide shows how to add a resource, how to seed it, and how to reset it.
Add a resource to a template
Section titled “Add a resource to a template”-
Add the resource to
resources:in the template. Give it akind.localspace.yaml resources:orders-db:kind: postgresversion: 16cache:kind: redis -
Refer to the resource in the
envof each service that uses it:localspace.yaml services:orders:repo: orders-apiport: 8080env:DATABASE_URL: ${orders-db.url}REDIS_URL: ${cache.url} -
Run
lo template validate <template>. The command finds an unknown kind and an unknown attribute.
lo up starts each resource and waits until it answers. It does this before it starts the services.
| Kind | Description | Default version |
|---|---|---|
postgres |
A PostgreSQL server. | 16 |
redis |
A Redis server. | 7 |
mongo |
A MongoDB server. | 8 |
s3 |
S3-compatible object storage from SeaweedFS. | The image is fixed. |
volume |
A named volume of the container engine, for the files of a container. | None |
version is the major version of the server. If you give no version, the kind uses its default. A plugin can add more kinds. If you write an unknown kind, lo template validate lists the kinds that are installed.
A postgres resource can have readonly_role: true. The resource then has a role that can only read, and the attribute readonly_url gives its address.
An image with extensions
Section titled “An image with extensions”Some applications use a database extension that the official image does not have, such as pgvector. Give the image in image:
resources: db: kind: postgres version: 16 image: pgvector/pgvector:pg16The image must work as the official image of the kind does: the same variables and the same data directory. localspace continues to make the database, load the seed and make the read-only role. A resource with an image always runs in a separate instance, because a shared server runs the official image. --isolation db=database is refused for it.
References to a resource
Section titled “References to a resource”A reference has the form ${resource.attribute}. localspace gives each consumer the address that works for it. A container and a host process get different addresses.
| Kind | Attributes |
|---|---|
postgres |
url, readonly_url, host, port, name |
redis |
url |
mongo |
url, host, port, name |
s3 |
url, host, port, bucket, region, access_key, secret_key |
volume |
name |
readonly_url is possible only with readonly_role: true. For all the forms of a reference, see the references reference.
Volumes
Section titled “Volumes”A volume belongs to one workspace. A service that runs as a container mounts it with mounts:. A service with mounts: must have a Dockerfile. The key is the name of the resource. The value is an absolute path in the container.
services: uploads: repo: uploads-api mounts: files: /var/files
resources: files: kind: volumeA process cannot mount a volume, because it uses the files of the host.
Select the isolation
Section titled “Select the isolation”The isolation tells how a workspace keeps its resource apart from the resources of other workspaces. There are two values:
instance: the workspace has a separate server in a container. This is the default.database: the workspace has a separate database on one server that many workspaces share. This uses less memory.
For database, each kind keeps the workspaces apart in a different way:
| Kind | The workspace gets this on the server |
|---|---|
postgres |
A database, cloned from a seeded database. |
redis |
One of the numbered databases 1 to 15. |
mongo |
A database. |
s3 |
A bucket. |
A volume is always an instance.
Select the default isolation of a kind in the machine settings:
resources: postgres: isolation: databaseTo select the isolation of one resource, use --isolation when you make the workspace:
lo new shop-a --template shop --isolation orders-db=databaseThe isolation is fixed when lo new makes the workspace. If you change the machine setting after that, the workspaces that you made keep their isolation.
Shared servers
Section titled “Shared servers”With database isolation, localspace runs one shared server for each kind and major version. The server has a name such as postgres-16. It starts when a workspace must use it. localspace does not stop it when you stop the workspaces.
If you want to use a server that you run yourself, set server in the machine settings. The value is a URL:
resources: postgres: isolation: database server: postgres://postgres@127.0.0.1:5432localspace then makes the databases on that server. For a postgres server, the major version of the server must be the version of the resource. If you set server, localspace loads seeds with the psql of your machine. lo doctor tells you if psql is not there.
Seed a resource
Section titled “Seed a resource”A seed puts the first data in a resource. It is loaded one time, when the resource is made. Set seed on the resource in one of these forms:
resources: orders-db: kind: postgres seed: seeds/orders.sql.gz booking-db: kind: postgres seed: run: php artisan migrate --seed --force service: bookingA seed has three fields:
| Field | Use |
|---|---|
file |
A file that the resource loads. The path is relative to the template directory, or starts with ~/. A string as a value of seed is a file. |
run |
A command that runs in a service. |
service |
The service in which run runs. It is necessary when you set run. |
You can set file and run together. The file loads first.
Seed from a file
Section titled “Seed from a file”| Kind | File |
|---|---|
postgres |
A .sql or .sql.gz dump. |
volume |
A .tar.gz or .tgz archive. localspace extracts it into the volume. |
redis, mongo, s3 |
No file. Seed these kinds with a command. |
lo template validate tells you if the file is not there, or if it has an incorrect form.
With instance isolation, a postgres container loads the dump when it starts for the first time. With database isolation, localspace loads the dump into a seed database one time for each version of the file. It clones that database for each workspace. A change in the file makes a new seed database.
Seed with a command
Section titled “Seed with a command”Use a command when the application has a seeder, such as php artisan db:seed, bin/rails db:seed or npx prisma db seed. The command runs in the directory of the service, or in its container, with the environment of the service.
- The command runs after the
setupof the service, so the dependencies are installed. It runs before the service starts, so the service does not see an empty database. - The service must refer to the resource in its
envor in itsmounts. Without that reference, the command cannot reach the resource, and the template is not valid. - localspace records that the command filled the resource. It does not run the command again at the next start.
- If the command fails, the service fails. The command runs again at the next
lo up.
Reset a resource
Section titled “Reset a resource”Run lo db reset to delete the data of a resource and load the seed again. Use it when your migrations must start from a clean database, for example after lo set shows a warning about migrations.
-
Run the command:
Terminal window lo db reset shop-a orders-db -
Answer
yto the question. To skip the question, add--yes.
For an instance, localspace removes the container and the data volumes, and starts the resource again from its seed. For a database, it drops the database and clones it again. If the seed has a command, localspace runs it again when the sources of the workspace are on disk. If they are not, the first lo up runs it.
Show the size of the data
Section titled “Show the size of the data”Run lo db size to show how much data each resource of a workspace holds in its volumes:
lo db size shop-aThe command shows only the resources with instance isolation. It shows nothing for a resource if the engine cannot measure its volumes.
Related
Section titled “Related”- Write a template shows where
resources:goes. - Share services tells how workspaces share a service.
lo db resetandlo db sizegive all the flags.- Machine settings gives the
resources:setting.