Skip to content

Commit e5d0661

Browse files
[changelog] Support for Containers with the Cloudflare Vite plugin
1 parent 890bb7b commit e5d0661

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: Support for Containers with the Cloudflare Vite plugin
3+
description: The Cloudflare Vite plugin now supports configuring Containers in your Worker
4+
products:
5+
- workers
6+
date: 2025-07-22
7+
---
8+
9+
import { WranglerConfig } from "~/components";
10+
11+
You can now configure Cloudflare Containers in your Worker during local development using the Cloudflare Vite plugin and `vite dev`.
12+
13+
#### Prerequisites
14+
15+
To develop Container-enabled Workers locally with `vite dev`, you will need to first ensure that a Docker compatible CLI tool and Engine are installed. For instance, you can use [Docker Desktop](https://docs.docker.com/desktop/) on Mac, Windows, or Linux.
16+
17+
Once installed, make sure that Docker is running locally before you start the local development server using `vite dev`.
18+
19+
#### Configuration
20+
21+
You can simply configure your Worker and your Container(s) in your Wrangler configuration file:
22+
23+
<WranglerConfig>
24+
```jsonc
25+
{
26+
"name": "container-starter",
27+
"main": "src/index.js",
28+
"containers": [
29+
{
30+
"class_name": "MyContainer",
31+
"image": "./Dockerfile",
32+
"instances": 5,
33+
"name": "hello-containers-go"
34+
}
35+
],
36+
"durable_objects": {
37+
"bindings": [
38+
{
39+
"class_name": "MyContainer",
40+
"name": "MY_CONTAINER"
41+
}
42+
]
43+
},
44+
"migrations": [
45+
{
46+
"new_sqlite_classes": [
47+
"MyContainer"
48+
],
49+
"tag": "v1"
50+
}
51+
],
52+
}
53+
```
54+
</WranglerConfig>
55+
56+
#### Worker Code
57+
58+
Once your Worker and Containers are configured, you can access the Container instances from your Worker code:
59+
60+
```ts
61+
import { Container, getContainer } from "@cloudflare/containers";
62+
63+
export class MyContainer extends Container {
64+
defaultPort = 4000; // Port the container is listening on
65+
sleepAfter = "10m"; // Stop the instance if requests not sent for 10 minutes
66+
}
67+
68+
async fetch(request, env) {
69+
const { "session-id": sessionId } = await request.json();
70+
// Get the container instance for the given session ID
71+
const containerInstance = getContainer(env.MY_CONTAINER, sessionId)
72+
// Pass the request to the container instance on its default port
73+
return containerInstance.fetch(request);
74+
}
75+
```
76+
77+
#### Local development
78+
79+
To develop your Worker locally, start a local dev server by running
80+
81+
```sh
82+
vite dev
83+
```
84+
85+
in your terminal.
86+
87+
#### Resources
88+
89+
Learn more about [Cloudflare Containers](https://developers.cloudflare.com/containers/) or the [Cloudflare Vite plugin](https://developers.cloudflare.com/workers/vite-plugin/) in our developer docs.

0 commit comments

Comments
 (0)