> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Volumes

> Dotez une sandbox d’un espace de travail temporaire accessible en écriture, qui existe aussi longtemps que la sandbox.

<Note>
  The examples on this page omit `auth`. To authenticate with your W\&B API key, install `cwsandbox[wandb]` and pass `auth=AuthStrategy.WANDB` to `Sandbox.run()`, `Sandbox()`, or `Sandbox.session()`. See [Get started](/products/sandboxes/serverless/get-started).
</Note>

Un volume est un espace de stockage qu’un sandbox monte à l’emplacement de votre choix. Les **volumes temporaires** appartiennent à un seul sandbox et disparaissent avec lui.

Cette page s’adresse aux développeurs dont le sandbox doit écrire plus de données que ne peut en contenir le système de fichiers de son conteneur. À la fin de cette page, vous saurez attacher un volume temporaire.

## Scratch volumes

A scratch volume is created with the sandbox and destroyed with it. Declare one with `ScratchVolumeOptions` in the `volumes` list.

```python theme={"system"}
from cwsandbox import Sandbox, ScratchVolumeOptions

with Sandbox.run(
    container_image="ubuntu:22.04",
    volumes=[ScratchVolumeOptions(name="workspace", mount_path="/workspace", size="10Gi")],
) as sandbox:
    sandbox.exec(["sh", "-c", "echo hello > /workspace/data.txt"]).result()
```

`ScratchVolumeOptions` accepts the following fields:

| Champ | Description |
| - | - |
| `name` | Nom du volume au sein du sandbox. Les montages et les snapshots font référence à ce nom. Requis. |
| `mount_path` | Chemin absolu de montage sur le conteneur principal. Omettez-le pour déclarer le volume sans le monter. |
| `size` | Taille du volume, par exemple `"10Gi"`. Omettez-la pour utiliser la valeur par défaut de la plateforme. |
| `medium` | Support de stockage sous-jacent : `StorageMedium.DISK` (par défaut) ou `StorageMedium.MEMORY`. |
| `sub_path` | Chemin relatif, à l’intérieur du volume, à monter à la place de sa racine. |
| `read_only` | Monte le volume en lecture seule. Valeur par défaut : `False`. |
| `restore_from_snapshot_id` | Restaure un [snapshot du système de fichiers](/products/sandboxes/serverless/file-system-snapshots) dans le volume lors de sa création. |

### Choose a storage medium

`StorageMedium.DISK` backs the volume with local disk. `StorageMedium.MEMORY` backs it with tmpfs, which is faster but counts against the mounting container's memory request and against the policy's memory ceiling.

Because an unbounded RAM-backed volume could exhaust the node, a `MEMORY` volume must declare a `size`. It may do so as an absolute quantity, or as an integer percentage of the sandbox's resolved memory request, such as `"50%"`. The percentage form is valid only for `MEMORY`. A `DISK` volume must use an absolute size.

```python theme={"system"}
from cwsandbox import Sandbox, ScratchVolumeOptions, StorageMedium

with Sandbox.run(
    container_image="ubuntu:22.04",
    volumes=[
        ScratchVolumeOptions(
            name="fast-cache",
            mount_path="/cache",
            size="50%",
            medium=StorageMedium.MEMORY,
        )
    ],
) as sandbox:
    sandbox.exec(["sh", "-c", "df -h /cache"]).result()
```

### Mount a scratch volume on several containers

`mount_path` is a convenience that mounts the volume on the primary container. In a multi-container sandbox, omit `mount_path` and declare the mounts on each container instead, so that more than one container can reach the same volume.

```python theme={"system"}
from cwsandbox import Container, Sandbox, ScratchVolumeOptions, VolumeMount

with Sandbox.run(
    volumes=[ScratchVolumeOptions(name="shared", size="5Gi")],
    containers=[
        Container(
            name="app",
            image="ubuntu:22.04",
            primary=True,
            volume_mounts=[VolumeMount(volume="shared", mount_path="/data")],
        ),
        Container(
            name="sidecar",
            image="ubuntu:22.04",
            volume_mounts=[VolumeMount(volume="shared", mount_path="/data", read_only=True)],
        ),
    ],
) as sandbox:
    sandbox.exec(["sh", "-c", "echo shared > /data/note.txt"]).result()
```

## Related resources

For more on related sandbox features, see the following resources:

* [Snapshots du système de fichiers](/products/sandboxes/serverless/file-system-snapshots) : conserver et restaurer le système de fichiers d’un sandbox.
* [Configuration du sandbox](/fr/products/sandboxes/serverless/client/guides/sandbox-configuration) : les autres options de création du sandbox
