# Volumes

Source: https://docs.apphaven.eu/volumes

Volumes give your [container services](https://docs.apphaven.eu/services/container.md) storage. You
declare them once at the top level of [your manifest](https://docs.apphaven.eu/manifest.md), then mount
them into services by name. AppHaven creates each volume and manages where it
lives.

```yaml
services:
  web:
    type: container
    build:
      dockerfile: Dockerfile
    volumes:
      - name: uploads
        mount_path: /app/uploads
      - name: cache
        mount_path: /app/cache

volumes:
  uploads:
    type: persistent    # kept across deploys
  cache:
    type: ephemeral     # scratch, safe to lose
```

## Declaring volumes

Each entry under the top-level `volumes` is keyed by the volume name:

| Field  | Type   | Required | Rules                                                       |
| ------ | ------ | -------- | ------------------------------------------------------------ |
| `type` | string | yes      | `ephemeral` or `persistent`. No other values are accepted.   |

- **`ephemeral`**: scratch storage for the deployment. Use it for caches and
  other data your app can rebuild.
- **`persistent`**: durable storage that is kept across every redeploy. Use
  it for data that must survive releases.

A volume draws on the deployment's overall [storage budget](https://docs.apphaven.eu/resources.md),
shared with your managed database and backups; there is no per-volume size to
set.

## Mounting a volume

Each entry in a service's `volumes` list attaches one declared volume at a
path inside the container:

| Field        | Type       | Required | Rules                                                                        |
| ------------ | ---------- | -------- | ----------------------------------------------------------------------------- |
| `name`       | identifier | yes      | Must match a key in the top-level `volumes` map.                              |
| `mount_path` | string     | yes      | The path inside the container. Must be **absolute** and **canonical**: no `..`, no trailing `/`, no `//`, and no `.` segments. |

You reference volumes by name only: where they live is AppHaven's concern.

## Scoping and durability

Volumes are scoped to a deployment:

- **Redeploys keep your data.** Persistent volumes survive every release of
  your app, and stopping a deployment keeps its data too; deploying again
  picks it right back up.
- **Backed up nightly.** A deployment's persistent data is captured in a
  nightly snapshot; see [Backups & recovery](https://docs.apphaven.eu/backups.md).
- **Previews are isolated.** Production and each preview environment get their
  own volumes, so an experiment on a branch can never touch production data.
- **Managed databases bring their own storage.** A
  [managed database](https://docs.apphaven.eu/services/postgres.md) provisions its own persistent
  storage; you don't declare it here.

## Where to go next

- [Container services](https://docs.apphaven.eu/services/container.md): the services that mount volumes.
- [Database](https://docs.apphaven.eu/services/postgres.md): managed database storage, handled for you.
- [The manifest](https://docs.apphaven.eu/manifest.md): the full `apphaven.yaml` schema.
