Skip to content

Commit ab8a17e

Browse files
authored
Restructure containers docs (#24633)
* restructure * new rollouts page * rewrite architecture page as lifecycle * miscellaneous fixes * pr feedback and fixups * update CLOUDFLARE_DURABLE_OBJECT_ID env var description * update container shutdown * update secret and env vars page
1 parent 53ff2ab commit ab8a17e

27 files changed

+435
-289
lines changed

public/__redirects

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2421,4 +2421,9 @@
24212421
/ai-gateway/guardrails/* /ai-gateway/features/guardrails/:splat 301
24222422
/ai-gateway/websockets-api/* /ai-gateway/usage/websockets-api/:splat 301
24232423

2424-
2424+
# Containers
2425+
/containers/image-management /containers/platform-details/image-management 301
2426+
/containers/scaling-and-routing /containers/platform-details/scaling-and-routing 301
2427+
/containers/architecture /containers/platform-details/architecture 301
2428+
/containers/durable-object-methods /containers/platform-details/durable-object-methods 301
2429+
/containers/platform-details /containers/platform-details/architecture 301

src/content/docs/containers/architecture.mdx

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/content/docs/containers/beta-info.mdx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: reference
33
title: Beta Info & Roadmap
44
sidebar:
5-
order: 2
5+
order: 9
66
---
77

88
Currently, Containers are in beta. There are several changes we plan to make prior to GA:
@@ -24,7 +24,7 @@ by calling `get()` on their binding with a unique ID.
2424
We plan to add official support for utilization-based autoscaling and latency-aware load balancing
2525
in the future.
2626

27-
See the [Autoscaling documentation](/containers/scaling-and-routing) for more information.
27+
See the [Autoscaling documentation](/containers/platform-details/scaling-and-routing) for more information.
2828

2929
### Reduction of log noise
3030

@@ -38,7 +38,6 @@ We plan to automatically reduce log noise in the future.
3838

3939
The dashboard will be updated to show:
4040

41-
- the status of Container rollouts
4241
- links from Workers to their associated Containers
4342

4443
### Co-locating Durable Objects and Containers
@@ -71,6 +70,6 @@ There are several areas where we wish to gather feedback from users:
7170
- Do you want to integrate Containers with any other Cloudflare services? If so, which ones and how?
7271
- Do you want more ways to interact with a Container via Workers? If so, how?
7372
- Do you need different mechanisms for routing requests to containers?
74-
- Do you need different mechanisms for scaling containers? (see [scaling documentation](/containers/scaling-and-routing) for information on autoscaling plans)
73+
- Do you need different mechanisms for scaling containers? (see [scaling documentation](/containers/platform-details/scaling-and-routing) for information on autoscaling plans)
7574

7675
At any point during the Beta, feel free to [give feedback using this form](https://forms.gle/CscdaEGuw5Hb6H2s7).

src/content/docs/containers/container-package.mdx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@
22
pcx_content_type: navigation
33
title: Container Package
44
sidebar:
5-
order: 8
5+
order: 5
66
---
77

8+
import { PackageManagers } from "~/components";
9+
810
When writing code that interacts with a container instance, you can either use a
9-
Durable Object directly or use the [`Container` module](https://github.com/cloudflare/containers)
11+
[Durable Object directly](/containers/platform-details/durable-object-methods) or use the [`Container` class](https://github.com/cloudflare/containers)
1012
importable from [`@cloudflare/containers`](https://www.npmjs.com/package/@cloudflare/containers).
1113

14+
We recommend using the `Container` class for most use cases.
15+
16+
<PackageManagers pkg="@cloudflare/containers" />
17+
18+
Then, you can define a class that extends `Container`, and use it in your Worker:
19+
1220
```javascript
1321
import { Container } from "@cloudflare/containers";
1422

1523
class MyContainer extends Container {
1624
defaultPort = 8080;
1725
sleepAfter = "5m";
1826
}
19-
```
20-
21-
We recommend using the `Container` class for most use cases.
2227

23-
Install it with `npm install @cloudflare/containers`.
28+
export default {
29+
async fetch(request, env) {
30+
// gets default instance and forwards request from outside Worker
31+
return env.MY_CONTAINER.getByName("hello").fetch(request);
32+
},
33+
};
34+
```
2435

25-
The `Container` class extends `DurableObject` so all Durable Object functionality is available.
36+
The `Container` class extends `DurableObject` so all [Durable Object](/durable-objects) functionality is available.
2637
It also provides additional functionality and a nice interface for common container behaviors,
2738
such as:
2839

src/content/docs/containers/examples/container-backend.mdx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
32
summary: A simple frontend app with a containerized backend
43
pcx_content_type: example
54
title: Static Frontend, Container Backend
@@ -33,6 +32,7 @@ For a full example, see the [Static Frontend + Container Backend Template](https
3332
{
3433
"class_name": "Backend",
3534
"image": "./Dockerfile",
35+
"max_instances": 3
3636
}
3737
],
3838
"durable_objects": {
@@ -166,7 +166,7 @@ select of of N instances of a Container to route requests to.
166166
In the future, we will provide improved latency-aware load balancing and autoscaling.
167167

168168
This will make scaling stateless instances simple and routing more efficient. See the
169-
[autoscaling documentation](/containers/scaling-and-routing) for more details.
169+
[autoscaling documentation](/containers/platform-details/scaling-and-routing) for more details.
170170
:::
171171

172172
## Define a backend container
@@ -176,6 +176,7 @@ Your container should be able to handle requests to `/api/widgets`.
176176
In this case, we'll use a simple Golang backend that returns a hard-coded list of widgets.
177177

178178
<Details header="server.go">
179+
179180
```go
180181
package main
181182

@@ -186,22 +187,23 @@ import (
186187
)
187188

188189
func handler(w http.ResponseWriter, r \*http.Request) {
189-
widgets := []map[string]interface{}{
190-
{"id": 1, "name": "Widget A"},
191-
{"id": 2, "name": "Sprocket B"},
192-
{"id": 3, "name": "Gear C"},
193-
}
190+
widgets := []map[string]interface{}{
191+
{"id": 1, "name": "Widget A"},
192+
{"id": 2, "name": "Sprocket B"},
193+
{"id": 3, "name": "Gear C"},
194+
}
194195

195-
w.Header().Set("Content-Type", "application/json")
196-
w.Header().Set("Access-Control-Allow-Origin", "*")
197-
json.NewEncoder(w).Encode(widgets)
196+
w.Header().Set("Content-Type", "application/json")
197+
w.Header().Set("Access-Control-Allow-Origin", "*")
198+
json.NewEncoder(w).Encode(widgets)
198199

199200
}
200201

201202
func main() {
202-
http.HandleFunc("/api/widgets", handler)
203-
log.Fatal(http.ListenAndServe(":8080", nil))
203+
http.HandleFunc("/api/widgets", handler)
204+
log.Fatal(http.ListenAndServe(":8080", nil))
204205
}
205206

206207
```
208+
207209
</Details>

src/content/docs/containers/examples/cron.mdx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
32
summary: Running a container on a schedule using Cron Triggers
43
pcx_content_type: example
54
title: Cron Container
@@ -43,9 +42,7 @@ Use a cron expression in your Wrangler config to specify the schedule:
4342
},
4443
"migrations": [
4544
{
46-
"new_sqlite_classes": [
47-
"CronContainer"
48-
],
45+
"new_sqlite_classes": ["CronContainer"],
4946
"tag": "v1"
5047
}
5148
]
@@ -61,7 +58,6 @@ import { Container, getContainer } from "@cloudflare/containers";
6158

6259
export class CronContainer extends Container {
6360
sleepAfter = "5m";
64-
manualStart = true;
6561
}
6662

6763
export default {
@@ -71,13 +67,16 @@ export default {
7167
);
7268
},
7369

70+
// scheduled is called when a cron trigger fires
7471
async scheduled(
7572
_controller: any,
7673
env: { CRON_CONTAINER: DurableObjectNamespace<CronContainer> },
7774
) {
78-
await getContainer(env.CRON_CONTAINER).startContainer({
79-
envVars: {
80-
MESSAGE: "Start Time: " + new Date().toISOString(),
75+
await getContainer(env.CRON_CONTAINER).startAndWaitForPorts({
76+
startOptions: {
77+
envVars: {
78+
MESSAGE: "Start Time: " + new Date().toISOString(),
79+
},
8180
},
8281
});
8382
},

0 commit comments

Comments
 (0)