ENF News Bot
Automated News Bot
- Joined
- Apr 15, 2022
- Messages
- 2,591
You can now configure and run Containers alongside your Worker during local development when using the Cloudflare Vite plugin. Previously, you could only develop locally when using Wrangler as your local development server.
You can simply configure your Worker and your Container(s) in your Wrangler configuration file:
Once your Worker and Containers are configured, you can access the Container instances from your Worker code:
To develop your Worker locally, start a local dev server by running
in your terminal.
Learn more about Cloudflare Containers ↗︎ or the Cloudflare Vite plugin ↗︎ in our developer docs.
Continue reading...
Configuration
https://developers.cloudflare.com/changelog/rss/#configurationYou can simply configure your Worker and your Container(s) in your Wrangler configuration file:
Code:
{
"name": "container-starter",
"main": "src/index.js",
"containers": [
{
"class_name": "MyContainer",
"image": "./Dockerfile",
"instances": 5
}
],
"durable_objects": {
"bindings": [
{
"class_name": "MyContainer",
"name": "MY_CONTAINER"
}
]
},
"migrations": [
{
"new_sqlite_classes": [
"MyContainer"
],
"tag": "v1"
}
],
}
Code:
name = "container-starter"
main = "src/index.js"
[[containers]]
class_name = "MyContainer"
image = "./Dockerfile"
instances = 5
[[durable_objects.bindings]]
class_name = "MyContainer"
name = "MY_CONTAINER"
[[migrations]]
new_sqlite_classes = [ "MyContainer" ]
tag = "v1"
Worker Code
https://developers.cloudflare.com/changelog/rss/#worker-codeOnce your Worker and Containers are configured, you can access the Container instances from your Worker code:
Code:
import { Container, getContainer } from "@cloudflare/containers";
export class MyContainer extends Container {
defaultPort = 4000; // Port the container is listening on
sleepAfter = "10m"; // Stop the instance if requests not sent for 10 minutes
}
async fetch(request, env) {
const { "session-id": sessionId } = await request.json();
// Get the container instance for the given session ID
const containerInstance = getContainer(env.MY_CONTAINER, sessionId)
// Pass the request to the container instance on its default port
return containerInstance.fetch(request);
}
Local development
https://developers.cloudflare.com/changelog/rss/#local-developmentTo develop your Worker locally, start a local dev server by running
vite devin your terminal.
Resources
https://developers.cloudflare.com/changelog/rss/#resourcesLearn more about Cloudflare Containers ↗︎ or the Cloudflare Vite plugin ↗︎ in our developer docs.
Continue reading...