-
Notifications
You must be signed in to change notification settings - Fork 0
cloudflare containers
This guide explains how to deploy the Adblock Compiler to Cloudflare Containers.
Cloudflare Containers allows you to deploy Docker containers globally alongside your Workers. The container configuration is already set up in wrangler.toml.
The wrangler.toml file includes:
[[containers]]
class_name = "AdblockCompiler"
image = "./Dockerfile"
max_instances = 5
instance_type = "basic"
[[durable_objects.bindings]]
class_name = "AdblockCompiler"
name = "ADBLOCK_COMPILER"
[[migrations]]
new_sqlite_classes = ["AdblockCompiler"]
tag = "v1"-
Docker must be running - Wrangler uses Docker to build and push images
docker info
If this fails, start Docker Desktop.
-
Wrangler authentication - Already authenticated as jayson.knight@jaysonknight.com
-
Container support in your Cloudflare plan - Containers are currently in public beta
npx wrangler deployThis command will:
- Build your Docker container image using the Dockerfile
- Push the image to Cloudflare's Container Registry (backed by R2)
- Deploy your Worker
- Configure Cloudflare's network to spawn container instances
After first deployment, wait 2-3 minutes before making requests. Unlike Workers, Containers take time to be provisioned across the edge network.
npx wrangler containers listThis shows all containers in your account and their deployment status.
Containers are not supported for local development on Windows. You have two options:
-
Use WSL (Windows Subsystem for Linux)
wsl cd /mnt/d/source/adblock-compiler npx wrangler dev
-
Disable containers for local dev (current configuration) The
wrangler.tomlhasenable_containers = falsein the[dev]section, which allows you to develop the Worker functionality locally without containers.
You can still test the Worker API locally:
npx wrangler dev --localVisit http://localhost:8787 to access:
-
/api- API documentation -
/compile- JSON compilation endpoint -
/compile/stream- Streaming compilation with SSE -
/metrics- Request metrics
Note: Container-specific endpoints will return 501 Not Implemented in local development.
The AdblockCompiler class in worker/worker.ts is currently a stub for local development. When you deploy to Cloudflare with containers enabled, you can update it to extend the Container class:
import { Container } from 'cloudflare:workers';
export class AdblockCompiler extends Container {
defaultPort = 8787;
}-
Build and test locally (without containers)
npx wrangler dev --local -
Test Docker image (optional)
docker build -t adblock-compiler:test . docker run -p 8787:8787 adblock-compiler:test
-
Deploy to Cloudflare
npx wrangler deploy
-
Check deployment status
npx wrangler containers list
-
Monitor logs
npx wrangler tail
Available instance types in wrangler.toml:
-
basic- Default, suitable for most workloads -
standard- More CPU/memory for intensive tasks -
premium- Maximum resources
[[containers]]
class_name = "AdblockCompiler"
image = "./Dockerfile"
max_instances = 5 # Maximum concurrent container instances
instance_type = "basic"Future autoscaling support:
autoscaling = true # Coming soon
cpu_target = 75 # Target CPU utilizationYour container/worker has access to:
-
env.COMPILATION_CACHE- KV Namespace for caching compiled results -
env.RATE_LIMIT- KV Namespace for rate limiting -
env.METRICS- KV Namespace for metrics storage -
env.FILTER_STORAGE- R2 Bucket for filter list storage -
env.ASSETS- Static assets (HTML, CSS, JS) -
env.COMPILER_VERSION- Version string -
env.ADBLOCK_COMPILER- Durable Object binding to container
- Containers are billed per millisecond of runtime (10ms granularity)
- Automatically scale to zero when not in use
- No charges when idle
- Container registry storage is free (backed by R2)
Error: Docker is not running
Solution: Start Docker Desktop and run docker info to verify.
Error: Container failed to start
Solution:
- Check
npx wrangler containers listfor status - Check container logs with
npx wrangler tail - Verify Dockerfile builds locally:
docker build -t test .
If you see Cannot find module 'cloudflare:workers':
Solution: This is expected in local dev. The module is only available in production. Make sure enable_containers = false is set in [dev] section of wrangler.toml.
-
Deploy to production:
npx wrangler deploy
-
Set up custom domain (optional)
npx wrangler deployments domains add <your-domain>
-
Monitor performance
npx wrangler tail
-
Update container configuration as needed in
wrangler.toml
For issues or questions:
- GitHub Issues: https://github.com/jaypatrick/adblock-compiler/issues
- Cloudflare Discord: https://discord.gg/cloudflaredev