Skip to content
Dashboard

Volumes

Copy page
View as Markdown

Every deploy and restart gives a service a fresh filesystem, so a file your app writes is gone the next time the service starts. A volume keeps one folder across deploys and restarts. Use a volume for a SQLite database, uploaded images or a small Postgres database behind a tool.

To keep a folder from the very first deploy, name the folder on the deploy:

Terminal window
grayspar deploy --volume app=/app/data

The name before the = is the service. A service at the root of your folder is called app, and a service in a subfolder takes that folder’s name. Running the same command on every deploy is safe, because a folder the service already keeps stays as it is.

To keep a folder of a running service, run grayspar volume add with the folder’s path inside the service. The project redeploys by itself:

Terminal window
grayspar volume add /app/data
Terminal window
✓ Added volume /app/data to app
✓ Redeployed <project-name>

When a project has several services, name the service with --service. A service keeps one volume.

Grayspar puts the volume’s path in the GRAYSPAR_VOLUME_PATH variable. Read the folder from there, so your app and the volume always agree:

import os
import sqlite3
data_folder = os.environ.get("GRAYSPAR_VOLUME_PATH", "./data")
database = sqlite3.connect(os.path.join(data_folder, "<database-name>.db"))

To list the volumes of a project and how much each one holds, run grayspar volume:

Terminal window
SERVICE PATH USED
app /app/data 12 MB of 1.0 GB

A volume grows by itself as it fills, as long as your team’s plan has storage left. When the plan’s storage is all in use, the team’s owners get an email saying which volume is full.

A service with a volume restarts in place when it deploys, because two copies writing the same files would corrupt them. The service is down for the moment its new copy takes to start. A service with no volume switches to its new copy with no downtime.

Some images declare a folder as data, such as the official Postgres image. Grayspar keeps that folder on a volume without being asked. An image that declares several folders fails its build with a message naming the folders, and --volume picks the one to keep.

Removing a volume deletes its data, so only an owner of the team can do it, and the CLI asks first:

Terminal window
grayspar volume remove /app/data

Grayspar deletes the data only after the service has redeployed without the volume and is running.