Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 51 additions & 53 deletions src/content/docs/containers/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,65 +46,63 @@ With Containers you can run:
- Existing applications and tools that have been distributed as container images

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:

<Tabs>
<TabItem label="Worker Code">
```js
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);
}

```
</TabItem>
<TabItem label="Worker Config">
<WranglerConfig>

```json
{
"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"
```js
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
}
],
}
```

</WranglerConfig>
</TabItem>
export default {
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);
},
};
```
</TabItem>
<TabItem label="Worker Config">
<WranglerConfig>
```json
{
"name": "container-starter",
"main": "src/index.js",
"compatibility_date": "$today",
"containers": [
{
"class_name": "MyContainer",
"image": "./Dockerfile",
"max_instances": 5
}
],
"durable_objects": {
"bindings": [
{
"class_name": "MyContainer",
"name": "MY_CONTAINER"
}
]
},
"migrations": [
{
"new_sqlite_classes": ["MyContainer"],
"tag": "v1"
}
]
}
```
</WranglerConfig>
</TabItem>
</Tabs>


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

---

Expand Down