|
| 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 | +You can now configure Cloudflare Containers in your Worker during local development using the Cloudflare Vite plugin and `vite dev`. |
| 10 | + |
| 11 | +#### Prerequisites |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +Once installed, make sure that Docker is running locally before you start the local development server using `vite dev`. |
| 16 | + |
| 17 | +#### Configuration |
| 18 | + |
| 19 | +You can simply configure your Worker and your Container(s) in your Wrangler configuration file: |
| 20 | + |
| 21 | +<WranglerConfig> |
| 22 | +```jsonc |
| 23 | +{ |
| 24 | + "name": "container-starter", |
| 25 | + "main": "src/index.js", |
| 26 | + "containers": [ |
| 27 | + { |
| 28 | + "class_name": "MyContainer", |
| 29 | + "image": "./Dockerfile", |
| 30 | + "instances": 5, |
| 31 | + "name": "hello-containers-go" |
| 32 | + } |
| 33 | + ], |
| 34 | + "durable_objects": { |
| 35 | + "bindings": [ |
| 36 | + { |
| 37 | + "class_name": "MyContainer", |
| 38 | + "name": "MY_CONTAINER" |
| 39 | + } |
| 40 | + ] |
| 41 | + }, |
| 42 | + "migrations": [ |
| 43 | + { |
| 44 | + "new_sqlite_classes": [ |
| 45 | + "MyContainer" |
| 46 | + ], |
| 47 | + "tag": "v1" |
| 48 | + } |
| 49 | + ], |
| 50 | +} |
| 51 | +``` |
| 52 | +</WranglerConfig> |
| 53 | + |
| 54 | +#### Worker Code |
| 55 | + |
| 56 | +Once your Worker and Containers are configured, you can access the Container instances from your Worker code: |
| 57 | + |
| 58 | +```ts |
| 59 | +import { Container, getContainer } from "@cloudflare/containers"; |
| 60 | + |
| 61 | +export class MyContainer extends Container { |
| 62 | + defaultPort = 4000; // Port the container is listening on |
| 63 | + sleepAfter = "10m"; // Stop the instance if requests not sent for 10 minutes |
| 64 | +} |
| 65 | + |
| 66 | +async fetch(request, env) { |
| 67 | + const { "session-id": sessionId } = await request.json(); |
| 68 | + // Get the container instance for the given session ID |
| 69 | + const containerInstance = getContainer(env.MY_CONTAINER, sessionId) |
| 70 | + // Pass the request to the container instance on its default port |
| 71 | + return containerInstance.fetch(request); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +#### Local development |
| 76 | + |
| 77 | +To develop your Worker locally, start a local dev server by running |
| 78 | + |
| 79 | +```sh |
| 80 | +vite dev |
| 81 | +``` |
| 82 | + |
| 83 | +in your terminal. |
| 84 | + |
| 85 | +#### Resources |
| 86 | + |
| 87 | +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