Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/containers/gotchas.md
1## Critical Gotchas23### ⚠️ WebSocket: fetch() vs containerFetch()45**Problem:** WebSocket connections fail silently67**Cause:** `containerFetch()` doesn't support WebSocket upgrades89**Fix:** Always use `fetch()` for WebSocket1011```typescript12// ❌ WRONG13return container.containerFetch(request);1415// ✅ CORRECT16return container.fetch(request);17```1819### ⚠️ startAndWaitForPorts() vs start()2021**Problem:** "connection refused" after `start()`2223**Cause:** `start()` returns when process starts, NOT when ports ready2425**Fix:** Use `startAndWaitForPorts()` before requests2627```typescript28// ❌ WRONG29await container.start();30return container.fetch(request);3132// ✅ CORRECT33await container.startAndWaitForPorts();34return container.fetch(request);35```3637### ⚠️ Activity Timeout on Long Operations3839**Problem:** Container stops during long work4041**Cause:** `sleepAfter` based on request activity, not internal work4243**Fix:** Renew timeout by touching storage4445```typescript46const interval = setInterval(() => {47this.ctx.storage.put("keepalive", Date.now());48}, 60000);4950try {51await this.doLongWork(data);52} finally {53clearInterval(interval);54}55```5657### ⚠️ blockConcurrencyWhile for Startup5859**Problem:** Race conditions during initialization6061**Fix:** Use `blockConcurrencyWhile` for atomic initialization6263```typescript64await this.ctx.blockConcurrencyWhile(async () => {65if (!this.initialized) {66await this.startAndWaitForPorts();67this.initialized = true;68}69});70```7172### ⚠️ Lifecycle Hooks Block Requests7374**Problem:** Container unresponsive during `onStart()`7576**Cause:** Hooks run in `blockConcurrencyWhile` - no concurrent requests7778**Fix:** Keep hooks fast, avoid long operations7980### ⚠️ Don't Override alarm() When Using schedule()8182**Problem:** Scheduled tasks don't execute8384**Cause:** `schedule()` uses `alarm()` internally8586**Fix:** Implement `alarm()` to handle scheduled tasks8788## Common Errors8990### "Container start timeout"9192**Cause:** Container took >8s (`start()`) or >20s (`startAndWaitForPorts()`)9394**Solutions:**95- Optimize image (smaller base, fewer layers)96- Check `entrypoint` correct97- Verify app listens on correct ports98- Increase timeout if needed99100### "Port not available"101102**Cause:** Calling `fetch()` before port ready103104**Solution:** Use `startAndWaitForPorts()`105106### "Container memory exceeded"107108**Cause:** Using more memory than instance type allows109110**Solutions:**111- Use larger instance type (standard-2, standard-3, standard-4)112- Optimize app memory usage113- Use custom instance type114115```jsonc116"instance_type_custom": {117"vcpu": 2,118"memory_mib": 8192119}120```121122### "Max instances reached"123124**Cause:** All `max_instances` slots in use125126**Solutions:**127- Increase `max_instances`128- Implement proper `sleepAfter`129- Use `getRandom()` for distribution130- Check for instance leaks131132### "No container instance available"133134**Cause:** Account capacity limits reached135136**Solutions:**137- Check account limits138- Review instance types across containers139- Contact Cloudflare support140141## Limits142143| Resource | Limit | Notes |144|----------|-------|-------|145| Cold start | 2-3s | Image pre-fetched globally |146| Graceful shutdown | 15 min | SIGTERM → SIGKILL |147| `start()` timeout | 8s | Process start |148| `startAndWaitForPorts()` timeout | 20s | Port ready |149| Max vCPU per container | 4 | standard-4 or custom |150| Max memory per container | 12 GiB | standard-4 or custom |151| Max disk per container | 20 GB | Ephemeral, resets |152| Account total memory | 400 GiB | All containers |153| Account total vCPU | 100 | All containers |154| Account total disk | 2 TB | All containers |155| Image storage | 50 GB | Per account |156| Disk persistence | None | Use DO storage |157158## Best Practices1591601. **Use `startAndWaitForPorts()` by default** - Prevents port errors1612. **Set appropriate `sleepAfter`** - Balance resources vs cold starts1623. **Use `fetch()` for WebSocket** - Not `containerFetch()`1634. **Design for restarts** - Ephemeral disk, implement graceful shutdown1645. **Monitor resources** - Stay within account limits1656. **Keep hooks fast** - Run in `blockConcurrencyWhile`1667. **Renew activity for long ops** - Touch storage to prevent timeout167168## Beta Caveats169170⚠️ Containers in **beta**:171172- **API may change** without notice173- **No SLA** guarantees174- **Limited regions** initially175- **No autoscaling** - manual via `getRandom()`176- **Rolling deploys** only (not instant like Workers)177178Plan for API changes, test thoroughly before production.179