You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Containers](/containers/) now support experimental snapshot APIs for saving and restoring point-in-time filesystem state. Create a snapshot first, then pass it back to `start()` to restore files after container sleep, restart, or handoff to another Durable Object.
12
+
13
+
Turn on the `experimental`[compatibility flag](/workers/configuration/compatibility-flags/) before you use snapshot APIs:
14
+
15
+
<WranglerConfig>
16
+
17
+
```toml
18
+
compatibility_flags = ["experimental"]
19
+
```
20
+
21
+
</WranglerConfig>
22
+
23
+
You can snapshot a single directory with `snapshotDirectory()` or the full container with `snapshotContainer()`:
Snapshots are immutable. Directory snapshots can be restored to a different `mountPoint`. `snapshotContainer({ memory: true })` can also capture memory. You can restore multiple directory snapshots in one `start()` call, but only one container snapshot.
43
+
44
+
For more information, refer to [Snapshots](/containers/platform-details/snapshots/) and [Durable Object Container](/durable-objects/api/container/).
Copy file name to clipboardExpand all lines: src/content/docs/containers/faq.mdx
+2-4Lines changed: 2 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,11 +89,9 @@ See [image management documentation](/containers/platform-details/image-manageme
89
89
90
90
## Is disk persistent? What happens to my disk when my container sleeps?
91
91
92
-
All disk is ephemeral. When a Container instance goes to sleep, the next time
93
-
it is started, it will have a fresh disk as defined by its container image.
92
+
All disk is ephemeral by default. When a Container instance goes to sleep, the next time it starts, it uses a fresh disk from the container image.
94
93
95
-
Persistent disk is something the Cloudflare team is exploring in the future, but
96
-
is not slated for the near term.
94
+
If you need point-in-time filesystem state, create and restore an experimental snapshot. Snapshots are immutable, so later file changes require a new snapshot. For more information, refer to [Snapshots](/containers/platform-details/snapshots/).
Copy file name to clipboardExpand all lines: src/content/docs/containers/platform-details/architecture.mdx
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -98,12 +98,11 @@ When a container instance is going to be shut down, it is sent a `SIGTERM` signa
98
98
and then a `SIGKILL` signal after 15 minutes. You should perform any necessary
99
99
cleanup to ensure a graceful shutdown in this time.
100
100
101
-
#### Persistent disk
101
+
#### Use snapshots
102
102
103
-
All disk is ephemeral. When a Container instance goes to sleep, the next time
104
-
it is started, it will have a fresh disk as defined by its container image.
105
-
Persistent disk is something the Cloudflare team is exploring in the future, but
106
-
is not slated for the near term.
103
+
All disk is ephemeral by default. When a Container instance goes to sleep, the next time it starts, it uses a fresh disk from the container image.
104
+
105
+
If you need point-in-time filesystem state, create and restore an experimental snapshot. Snapshots are immutable, so later file changes require a new snapshot. For more information, refer to [Snapshots](/containers/platform-details/snapshots/).
Snapshots let you save point-in-time filesystem state from a running [Container](/containers/). `snapshotContainer({ memory: true })` can also include container memory.
12
+
13
+
:::caution[Experimental]
14
+
Snapshot APIs require the `experimental`[compatibility flag](/workers/configuration/compatibility-flags/). These APIs may change in backward-incompatible ways.
15
+
:::
16
+
17
+
## Turn on the compatibility flag
18
+
19
+
Add `experimental` to your Worker's Wrangler configuration before you use snapshot APIs:
20
+
21
+
<WranglerConfig>
22
+
23
+
```toml
24
+
compatibility_date = "$today"
25
+
compatibility_flags = ["experimental"]
26
+
```
27
+
28
+
</WranglerConfig>
29
+
30
+
## Choose a snapshot type
31
+
32
+
Use `snapshotDirectory()` to capture one directory. Use `snapshotContainer()` to capture the full container filesystem.
33
+
34
+
Directory snapshots only capture filesystem contents. Container snapshots can also capture memory when you set `memory: true`.
35
+
36
+
Both snapshot types are immutable. If you restore a snapshot and then change files, create a new snapshot to persist those changes.
37
+
38
+
- You can restore multiple directory snapshots in one `start()` call.
39
+
- You can restore only one container snapshot in one `start()` call.
40
+
- You can restore directory snapshots on top of a container snapshot.
41
+
- Snapshot handles are plain data objects, so you can store them and restore them later, even from another Durable Object.
42
+
43
+
## Create a directory snapshot
44
+
45
+
Use `snapshotDirectory()` to capture the current contents of a directory in a running container:
46
+
47
+
<TypeScriptExamplefilename="src/index.ts">
48
+
49
+
```ts
50
+
exportclassMyContainerextendsDurableObject {
51
+
async saveWorkspace() {
52
+
returnawaitthis.ctx.container.snapshotDirectory({
53
+
dir: "/app/workspace",
54
+
name: "workspace-checkpoint",
55
+
});
56
+
}
57
+
}
58
+
```
59
+
60
+
</TypeScriptExample>
61
+
62
+
The `dir` value must be an absolute path. The API returns a serializable snapshot handle that you can store and restore later.
63
+
64
+
## Create a container snapshot
65
+
66
+
Use `snapshotContainer()` to capture the full container filesystem:
67
+
68
+
<TypeScriptExamplefilename="src/index.ts">
69
+
70
+
```ts
71
+
exportclassMyContainerextendsDurableObject {
72
+
async saveContainer() {
73
+
returnawaitthis.ctx.container.snapshotContainer({
74
+
name: "before-upgrade",
75
+
memory: true,
76
+
});
77
+
}
78
+
}
79
+
```
80
+
81
+
</TypeScriptExample>
82
+
83
+
Set `memory: true` to include container memory in the snapshot. The API returns a serializable snapshot handle that you can store and restore later.
84
+
85
+
:::note[Local development]
86
+
`snapshotContainer({ memory: true })` is not supported in [local development](/containers/local-dev/) with `wrangler dev` or `vite dev`. Deploy your Worker to use memory snapshots.
87
+
:::
88
+
89
+
## Restore snapshots
90
+
91
+
Pass saved snapshot handles to `this.ctx.container.start()` when you start the container:
Copy file name to clipboardExpand all lines: src/content/docs/durable-objects/api/container.mdx
+91Lines changed: 91 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,13 +13,26 @@ import {
13
13
Type,
14
14
MetaInfo,
15
15
TypeScriptExample,
16
+
WranglerConfig,
16
17
} from"~/components";
17
18
18
19
## Description
19
20
20
21
When using a [Container-enabled Durable Object](/containers), you can access the Durable Object's associated container via
21
22
the `container` object which is on the `ctx` property. This allows you to start, stop, and interact with the container.
22
23
24
+
:::caution[Experimental snapshot APIs]
25
+
`snapshotDirectory()`, `snapshotContainer()`, `directorySnapshots`, and `containerSnapshot` require the `experimental`[compatibility flag](/workers/configuration/compatibility-flags/). These APIs may change in backward-incompatible ways.
26
+
27
+
<WranglerConfig>
28
+
29
+
```toml
30
+
compatibility_flags = ["experimental"]
31
+
```
32
+
33
+
</WranglerConfig>
34
+
:::
35
+
23
36
:::note
24
37
It is likely preferable to use the official `Container` class, which provides helper methods and
25
38
a more idiomatic API for working with containers on top of Durable Objects.
@@ -76,11 +89,86 @@ this.ctx.container.start({
76
89
-`env`: An object containing environment variables to pass to the container. This is useful for passing configuration values or secrets to the container.
77
90
-`entrypoint`: An array of strings representing the command to run in the container.
78
91
-`enableInternet`: A boolean indicating whether to enable internet access for the container.
92
+
-`directorySnapshots`: An array of objects describing directory snapshots to restore before the container starts. You can restore more than one directory snapshot in a single call.
93
+
-`snapshot`: The `ContainerDirectorySnapshot` to restore.
94
+
-`mountPoint` (optional): The absolute path where the snapshot is restored. If omitted, the snapshot is restored to its original `dir` path.
95
+
-`containerSnapshot`: A full container snapshot to restore before the container starts. You can restore only one container snapshot in a single call.
79
96
80
97
#### Return values
81
98
82
99
- None.
83
100
101
+
#### Understand snapshot restore behavior
102
+
103
+
- Directory snapshots are restored before the container starts.
104
+
- If `mountPoint` is not set, the snapshot is restored to the original `dir` path.
105
+
- Directory snapshots cannot be restored to `/`.
106
+
- You can snapshot `/`, but that snapshot must be restored to a subdirectory by setting `mountPoint`.
107
+
- Nested directory snapshots are restored in depth order. For example, `/app` is restored before `/app/data`.
108
+
- Directory snapshots can be restored on top of a container snapshot.
109
+
110
+
### `snapshotDirectory`
111
+
112
+
`snapshotDirectory` creates a point-in-time snapshot of a directory in a running container.
-`options`: An object with the following properties:
159
+
-`memory` (optional boolean): If `true`, include container memory in the snapshot.
160
+
-`name` (optional string): A human-friendly name for the snapshot.
161
+
162
+
#### Return values
163
+
164
+
- A promise that resolves to a `ContainerSnapshot` object with `id`, `size`, optional `name`, and `memory` properties.
165
+
166
+
#### Notes
167
+
168
+
- Container snapshots are immutable.
169
+
-`snapshotContainer({ memory: true })` is not supported in [local development](/containers/local-dev/) with `wrangler dev` or `vite dev`. Deploy your Worker to use memory snapshots.
170
+
- Snapshot handles currently have an implicit 90-day time-to-live that refreshes when you restore them.
171
+
84
172
### `destroy`
85
173
86
174
`destroy` stops the container and optionally returns a custom error message to the `monitor()` error callback.
0 commit comments