Skip to content

Commit 58487a9

Browse files
CarmenPopoviciuthomasgauvin
authored andcommitted
[changelog] Support for Containers with the Cloudflare Vite plugin (#23855)
* [changelog] Support for Containers with the Cloudflare Vite plugin * feedback fixe + add gif of local dev experience * test
1 parent 65e4527 commit 58487a9

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
1.48 MB
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: Develop locally with Containers and the Cloudflare Vite plugin
3+
description: The Cloudflare Vite plugin now supports configuring Containers in your Worker
4+
products:
5+
- workers
6+
date: 2025-08-01
7+
---
8+
9+
import { WranglerConfig } from "~/components";
10+
11+
You can now configure and run [Containers](/containers) alongside your [Worker](/workers) during local development when using the [Cloudflare Vite plugin](/workers/vite-plugin/). Previously, you could only develop locally when using [Wrangler](/workers/wrangler/) as your local development server.
12+
13+
#### Configuration
14+
15+
You can simply configure your Worker and your Container(s) in your Wrangler configuration file:
16+
17+
<WranglerConfig>
18+
```jsonc
19+
{
20+
"name": "container-starter",
21+
"main": "src/index.js",
22+
"containers": [
23+
{
24+
"class_name": "MyContainer",
25+
"image": "./Dockerfile",
26+
"instances": 5
27+
}
28+
],
29+
"durable_objects": {
30+
"bindings": [
31+
{
32+
"class_name": "MyContainer",
33+
"name": "MY_CONTAINER"
34+
}
35+
]
36+
},
37+
"migrations": [
38+
{
39+
"new_sqlite_classes": [
40+
"MyContainer"
41+
],
42+
"tag": "v1"
43+
}
44+
],
45+
}
46+
```
47+
</WranglerConfig>
48+
49+
#### Worker Code
50+
51+
Once your Worker and Containers are configured, you can access the Container instances from your Worker code:
52+
53+
```ts
54+
import { Container, getContainer } from "@cloudflare/containers";
55+
56+
export class MyContainer extends Container {
57+
defaultPort = 4000; // Port the container is listening on
58+
sleepAfter = "10m"; // Stop the instance if requests not sent for 10 minutes
59+
}
60+
61+
async fetch(request, env) {
62+
const { "session-id": sessionId } = await request.json();
63+
// Get the container instance for the given session ID
64+
const containerInstance = getContainer(env.MY_CONTAINER, sessionId)
65+
// Pass the request to the container instance on its default port
66+
return containerInstance.fetch(request);
67+
}
68+
```
69+
70+
#### Local development
71+
72+
To develop your Worker locally, start a local dev server by running
73+
74+
```sh
75+
vite dev
76+
```
77+
78+
in your terminal.
79+
80+
![Local Dev video](~/assets/images/workers/changelog/worker-with-containers-in-vite.gif)
81+
82+
#### Resources
83+
84+
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)