Skip to content

Commit 1301cd7

Browse files
committed
miscellaneous fixes
1 parent 88b0cd1 commit 1301cd7

File tree

12 files changed

+146
-109
lines changed

12 files changed

+146
-109
lines changed

public/__redirects

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2398,4 +2398,10 @@
23982398
/ai-gateway/guardrails/* /ai-gateway/features/guardrails/:splat 301
23992399
/ai-gateway/websockets-api/* /ai-gateway/usage/websockets-api/:splat 301
24002400

2401-
2401+
# Containers
2402+
/containers/platform-details/#limits /containers/platform-details/limits 301
2403+
/containers/image-management /containers/platform-details/image-management 301
2404+
/containers/scaling-and-routing /containers/platform-details/scaling-and-routing 301
2405+
/containers/architecture /containers/platform-details/architecture 301
2406+
/containers/durable-object-methods /containers/platform-details/durable-object-methods 301
2407+
/containers/platform-details /containers/platform-details/architecture 301

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar:
66
---
77

88
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)
9+
[Durable Object directly](/containers/platform-details/durable-object-methods) or use the [`Container` module](https://github.com/cloudflare/containers)
1010
importable from [`@cloudflare/containers`](https://www.npmjs.com/package/@cloudflare/containers).
1111

1212
```javascript

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
},

src/content/docs/containers/examples/env-vars-and-secrets.mdx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: Pass in environment variables and secrets to your container
1010
import { WranglerConfig, PackageManagers } from "~/components";
1111

1212
Environment variables can be passed into a Container using the `envVars` field
13-
in the `Container` class, or by setting manually when the Container starts.
13+
in the [`Container`](/containers/container-package) class, or by setting manually when the Container starts.
1414

1515
Secrets can be passed into a Container by using [Worker Secrets](/workers/configuration/secrets/)
1616
or the [Secret Store](/secrets-store/integrations/workers/), then passing them into the Container
@@ -111,14 +111,13 @@ set as environment variables when it launches.
111111

112112
But what if you want to set environment variables on a per-instance basis?
113113

114-
In this case, set `manualStart` then use the `start` method to pass in environment variables for each instance.
114+
In this case, use the `startAndWaitForPorts()` method to pass in environment variables for each instance.
115115
We'll assume that we've set additional secrets in the Secret Store.
116116

117117
```js
118118
export class MyContainer extends Container {
119119
defaultPort = 8080;
120120
sleepAfter = "10s";
121-
manualStart = true;
122121
}
123122

124123
export default {
@@ -129,19 +128,23 @@ export default {
129128

130129
// Each instance gets a different set of environment variables
131130

132-
await instanceOne.start({
133-
envVars: {
134-
ACCOUNT_NAME: env.ACCOUNT_NAME + "-1",
135-
ACCOUNT_API_KEY: env.SECRET_STORE.ACCOUNT_API_KEY_ONE,
136-
CONTAINER_SECRET_KEY: env.CONTAINER_SECRET_KEY_TWO,
131+
await instanceOne.startAndWaitForPorts({
132+
startOptions: {
133+
envVars: {
134+
ACCOUNT_NAME: env.ACCOUNT_NAME + "-1",
135+
ACCOUNT_API_KEY: env.SECRET_STORE.ACCOUNT_API_KEY_ONE,
136+
CONTAINER_SECRET_KEY: env.CONTAINER_SECRET_KEY_TWO,
137+
},
137138
},
138139
});
139140

140-
await instanceTwo.start({
141-
envVars: {
142-
ACCOUNT_NAME: env.ACCOUNT_NAME + "-2",
143-
ACCOUNT_API_KEY: env.SECRET_STORE.ACCOUNT_API_KEY_TWO,
144-
CONTAINER_SECRET_KEY: env.CONTAINER_SECRET_KEY_TWO,
141+
await instanceTwo.startAndWaitForPorts({
142+
startOptions: {
143+
envVars: {
144+
ACCOUNT_NAME: env.ACCOUNT_NAME + "-2",
145+
ACCOUNT_API_KEY: env.SECRET_STORE.ACCOUNT_API_KEY_TWO,
146+
CONTAINER_SECRET_KEY: env.CONTAINER_SECRET_KEY_TWO,
147+
},
145148
},
146149
});
147150
return new Response("Container instances launched");
@@ -151,3 +154,7 @@ export default {
151154
},
152155
};
153156
```
157+
158+
## Build-time environment variables
159+
160+
Finally, you can also set build-time environment variables that are only available when building the container image via the `image_vars` field in the Wrangler configuration.

src/content/docs/containers/faq.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ on image size and code execution time, among other factors.
8585

8686
## How do I use an existing container image?
8787

88-
See [image management documentation](/containers/image-management/#using-existing-images) for details.
88+
See [image management documentation](/containers/platform-details/image-management/#using-existing-images) for details.
8989

9090
## Is disk persistent? What happens to my disk when my container sleeps?
9191

src/content/docs/containers/get-started.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This example Worker should give you a sense for simple Container use, and provid
1717
### Ensure Docker is running locally
1818

1919
In this guide, we will build and push a container image alongside your Worker code. By default, this process uses
20-
[Docker](https://www.docker.com/) to do so. You must have Docker running locally when you run `wrangler deploy`. For most people, the best way to install Docker is to follow the [docs for installing Docker Desktop](https://docs.docker.com/desktop/).
20+
[Docker](https://www.docker.com/) to do so. You must have Docker running locally when you run `wrangler deploy`. For most people, the best way to install Docker is to follow the [docs for installing Docker Desktop](https://docs.docker.com/desktop/). Other tools like [Colima](https://github.com/abiosoft/colima) may also work.
2121

2222
You can check that Docker is running properly by running the `docker info` command in your terminal. If Docker is running, the command will succeed. If Docker is not running,
2323
the `docker info` command will hang or return an error including the message "Cannot connect to the Docker daemon".
@@ -29,9 +29,11 @@ the `docker info` command will hang or return an error including the message "Ca
2929

3030
Run the following command to create and deploy a new Worker with a container, from the starter template:
3131

32-
```sh
33-
npm create cloudflare@latest -- --template=cloudflare/templates/containers-template
34-
```
32+
<PackageManagers
33+
type="create"
34+
pkg="cloudflare@latest"
35+
args={"--template=cloudflare/templates/containers-template"}
36+
/>
3537

3638
When you want to deploy a code change to either the Worker or Container code, you can run the following command using [Wrangler CLI](/workers/wrangler/):
3739

@@ -40,7 +42,7 @@ When you want to deploy a code change to either the Worker or Container code, yo
4042
When you run `wrangler deploy`, the following things happen:
4143

4244
- Wrangler builds your container image using Docker.
43-
- Wrangler pushes your image to a [Container Image Registry](/containers/image-management/) that is automatically
45+
- Wrangler pushes your image to a [Container Image Registry](/containers/platform-details/image-management/) that is automatically
4446
integrated with your Cloudflare account.
4547
- Wrangler deploys your Worker, and configures Cloudflare's network to be ready to spawn instances of your container
4648

@@ -67,7 +69,7 @@ And see images deployed to the Cloudflare Registry with the following command:
6769

6870
Now, open the URL for your Worker. It should look something like `https://hello-containers.YOUR_ACCOUNT_NAME.workers.dev`.
6971

70-
If you make requests to the paths `/container/1` or `/container/2`, these requests are routed to specific containers.
72+
If you make requests to the paths `/container/1` or `/container/2`, your Worker routes requests to specific containers.
7173
Each different path after "/container/" routes to a unique container.
7274

7375
If you make requests to `/lb`, you will load balanace requests to one of 3 containers chosen at random.

src/content/docs/containers/index.mdx

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,66 +45,72 @@ With Containers you can run:
4545
- Applications and libraries that require a full filesystem, specific runtime, or Linux-like environment
4646
- Existing applications and tools that have been distributed as container images
4747

48-
Container instances are spun up on-demand and controlled by code you write in your [Worker](/workers). Instead of chaining together API calls or writing Kubernetes operators, you just write JavaScript:
48+
Container instances are spun up on-demand and controlled by code you write in your [Worker](/workers). Instead of chaining together API calls or writing Kubernetes operators, you just write JavaScript:
49+
4950
<Tabs>
5051
<TabItem label="Worker Code">
51-
```js
52-
import { Container, getContainer } from "@cloudflare/containers";
53-
54-
export class MyContainer extends Container {
55-
defaultPort = 4000; // Port the container is listening on
56-
sleepAfter = "10m"; // Stop the instance if requests not sent for 10 minutes
57-
}
58-
59-
async fetch(request, env) {
60-
const { "session-id": sessionId } = await request.json();
61-
// Get the container instance for the given session ID
62-
const containerInstance = getContainer(env.MY_CONTAINER, sessionId)
63-
// Pass the request to the container instance on its default port
64-
return containerInstance.fetch(request);
65-
}
52+
```js
53+
import { Container, getContainer } from "@cloudflare/containers";
54+
55+
export class MyContainer extends Container {
56+
defaultPort = 4000; // Port the container is listening on
57+
sleepAfter = "10m"; // Stop the instance if requests not sent for 10 minutes
58+
}
59+
60+
async fetch(request, env) {
61+
const { "session-id": sessionId } = await request.json();
62+
// Get the container instance for the given session ID
63+
const containerInstance = getContainer(env.MY_CONTAINER, sessionId)
64+
// Pass the request to the container instance on its default port
65+
return containerInstance.fetch(request);
66+
}
67+
```
6668

67-
```
6869
</TabItem>
6970
<TabItem label="Worker Config">
7071
<WranglerConfig>
7172

7273
```json
7374
{
74-
"name": "container-starter",
75-
"main": "src/index.js",
76-
"containers": [
77-
{
78-
"class_name": "MyContainer",
79-
"image": "./Dockerfile",
80-
"instances": 5,
81-
}
82-
],
83-
"durable_objects": {
75+
"name": "container-starter",
76+
"main": "src/index.js",
77+
"containers": [
78+
{
79+
"class_name": "MyContainer",
80+
"image": "./Dockerfile",
81+
"max_instances": 5
82+
}
83+
],
84+
"durable_objects": {
8485
"bindings": [
8586
{
8687
"class_name": "MyContainer",
87-
"name": "MY_CONTAINER"
88-
}
89-
]
90-
},
88+
"name": "MY_CONTAINER"
89+
}
90+
]
91+
},
9192
"migrations": [
9293
{
93-
"new_sqlite_classes": [
94-
"MyContainer"
95-
],
94+
"new_sqlite_classes": ["MyContainer"],
9695
"tag": "v1"
9796
}
98-
],
97+
]
9998
}
10099
```
101100

102101
</WranglerConfig>
103102
</TabItem>
104103
</Tabs>
105104

106-
107-
<LinkButton variant="primary" href="/containers/get-started/">Get started</LinkButton> <LinkButton variant="secondary" href="https://dash.cloudflare.com/?to=/:account/workers/containers">Containers dashboard</LinkButton>
105+
<LinkButton variant="primary" href="/containers/get-started/">
106+
Get started
107+
</LinkButton>
108+
<LinkButton
109+
variant="secondary"
110+
href="https://dash.cloudflare.com/?to=/:account/workers/containers"
111+
>
112+
Containers dashboard
113+
</LinkButton>
108114

109115
---
110116

@@ -146,7 +152,11 @@ regional placement, Workflow and Queue integrations, AI-generated code execution
146152
containers with Wrangler.
147153
</LinkTitleCard>
148154

149-
<LinkTitleCard title="Limits" href="/containers/platform-details/#limits" icon="seti:lock">
155+
<LinkTitleCard
156+
title="Limits"
157+
href="/containers/platform-details/#limits"
158+
icon="seti:lock"
159+
>
150160
Learn about what limits Containers have and how to work within them.
151161
</LinkTitleCard>
152162

@@ -160,4 +170,3 @@ regional placement, Workflow and Queue integrations, AI-generated code execution
160170
</LinkTitleCard>
161171

162172
</CardGrid>
163-

src/content/docs/containers/local-dev.mdx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,35 @@ sidebar:
55
order: 6
66
---
77

8-
You can run both your container and your Worker locally, without additional configuration, by running [`npx wrangler dev`](/workers/wrangler/commands/#dev) (or `vite dev` for Vite projects using the [Cloudflare Vite plugin](/workers/vite-plugin/)) in your project's directory.
8+
You can run both your container and your Worker locally by simply running [`npx wrangler dev`](/workers/wrangler/commands/#dev) (or `vite dev` for Vite projects using the [Cloudflare Vite plugin](/workers/vite-plugin/)) in your project's directory.
99

1010
To develop Container-enabled Workers locally, you will need to first ensure that a
1111
Docker compatible CLI tool and Engine are installed. For instance, you could use [Docker Desktop](https://docs.docker.com/desktop/) or [Colima](https://github.com/abiosoft/colima).
1212

1313
When you start a dev session, your container image will be built or downloaded. If your
1414
[Wrangler configuration](/workers/wrangler/configuration/#containers) sets
1515
the `image` attribute to a local path, the image will be built using the local Dockerfile.
16-
If the `image` attribute is set to a URL, the image will be pulled from the associated registry.
16+
If the `image` attribute is set to a URL, the image will be pulled from the Cloudflare registry.
17+
18+
:::note
19+
Currently, the Cloudflare Vite-plugin does not support registry links in local development, unlike `wrangler dev`.
20+
As a workaround, you can create a minimal Dockerfile that uses `FROM <registry-link>`. Make sure to `EXPOSE` a port for local dev as well.
21+
:::
1722

1823
Container instances will be launched locally when your Worker code calls to create
19-
a new container. This may happen when calling `.get()` on a `Container` instance or
20-
by calling `start()` if `manualStart` is set to `true`. Requests will then automatically be routed to the correct locally-running container.
24+
a new container. Requests will then automatically be routed to the correct locally-running container.
2125

2226
When the dev session ends, all associated container instances should be stopped, but
2327
local images are not removed, so that they can be reused in subsequent builds.
2428

2529
:::note
2630
If your Worker app creates many container instances, your local machine may not be able to run as many containers concurrently as is possible when you deploy to Cloudflare.
2731

32+
Also, `max_instances` configuration option does not apply during local development.
33+
2834
Additionally, if you regularly rebuild containers locally, you may want to clear
2935
out old container images (using `docker image prune` or similar) to reduce disk used.
3036

31-
Also note that the `max_instances` configuration option is only enforced when running in production on Cloudflare's network. This limit does not apply during local development, so you may run more instances than specified.
3237
:::
3338

3439
## Iterating on Container code

src/content/docs/containers/platform-details/environment-variables.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ The container runtime automatically sets the following variables:
1919
- `CLOUDFLARE_REGION` - a region name
2020
- `CLOUDFLARE_DURABLE_OBJECT_ID` - the ID of the Durable Object that the container is bound to
2121

22-
## Custom environment variables
22+
## User-defined environment variables
2323

24-
Custom environment variables can be set when defining a Container in your Worker:
24+
You can set environment variables when defining a Container in your Worker, or when starting a container instance.
25+
26+
For example:
2527

2628
```javascript
2729
class MyContainer extends Container {
@@ -33,6 +35,4 @@ class MyContainer extends Container {
3335
}
3436
```
3537

36-
:::note
37-
If you supply environment variables with the same names, supplied values will override predefined values.
38-
:::
38+
More details about defining environment variables and secrets can be found in [this example](/containers/examples/env-vars-and-secrets).

0 commit comments

Comments
 (0)