Skip to content
AlanVaneAlanpally StudioAI & Software
Back to blog
4 min read

Running OpenCode inside a Cloudflare container

A self-hosted AI coding workspace: why I built it, how I verified it, and two things I got wrong.

Cloud Code is an AI coding workspace that runs in your browser: open a page, and you can read and write files and run commands, with the container running in your own Cloudflare account.

I want to write about how it was built. Whether a tool can be trusted is easier to judge from how it was verified than from a feature list.

Why I built it

Hosted versions mostly give you a workspace that works the moment you open it. What they can’t give you is the other half: whose account it runs on, who holds the keys, where the data lives, and whether you can change it.

Cloud Code keeps that inside your own Cloudflare account: the Worker, the container, object storage and access control are all your resources. The code is public, and changing a line means redeploying.

There was a more personal reason too: I wanted to know when this breaks. When the container sleeps, when the disk runs out, what happens when a connection drops. Without building it once yourself, you can only guess.

What I built

The project came together in stages:

  • Get the path working. A Worker in front, OpenCode in the container, a page in the browser that reads and writes files and runs commands; a password on the door first.
  • Make deployment real. A fixed region and instance size, a deploy button and English docs, so anyone who takes the repo can deploy it themselves.
  • Move to OpenCode v2. Container and Worker both follow the v2 interface, pinned at build time.
  • Make the runtime upgradable. Versions pinned, dependencies fixed, faster startup; a new version doesn’t mean relearning everything.
  • Turn “it runs” into “it’s verified.” The automated test builds the image, checks the runtime, starts a container and sends a request through it, rather than just checking that the code compiles.
  • Write the edges down. A deployment checklist, an access-control guide, local testing, a cost assessment, each with its own document.

None of the six is hard on its own. What’s hard is that after each one you have to answer the same question: how do I know it actually works?

Two things I got wrong

The first was shutdown. When the container stopped, the exit code was 143: it ended the moment it got the stop signal, and the cleanup never finished. The cause wasn’t complicated. The cleanup script waits for OpenCode to exit, and what came back was “terminated by a signal”; the script stops on the first error, so the remaining steps were skipped. The test didn’t catch it either: it only required “not out of memory,” and every other non-zero exit code was printed as a note. The fix was to accept the normal stop states and report the rest as failures, and to require exit code 0 plus a cleanup-complete marker in the test.

The second was the bill. The container is set to sleep after ten minutes idle, and I estimated cost on “two hours a day.” Later, reading the source, I found the container keeps a long-lived connection of its own, which may stop it from ever sleeping. That’s an inference; I never checked it against a bill, so it stayed in the docs and never became a conclusion.

Neither was a hard technical problem. The problem was that nobody was checking my assumptions.

Who it’s for

If you only want to pick up where you left off on your phone or another computer, OpenCode’s built-in remote access is enough. You don’t need a container.

What it really adds is an execution environment that doesn’t depend on your machine being online: after you close the laptop, a task with clear edges keeps running — upgrading dependencies, running tests, analysing a repo you’ve already pushed.

It has costs too: a single instance, a fixed region, around thirty dollars a month, and it is not a security sandbox. All of that is in the docs; I didn’t hide it.

What this means for working together

You can probably see how I work from this:

  • Measure before promising. The bill, sleep behaviour, failure modes: test what can be tested, and label what can’t as an inference.
  • A rollback is part of shipping. Decide how to get back before you go live.
  • A check has to be able to fail. A test that only confirms “it didn’t crash” won’t stop a real problem.
  • Docs are part of delivery. Whoever takes over doesn’t have to ask again.

If you have a process that repeats every day and nobody has automated it yet, we can start by talking about whether it’s worth automating. The first conversation is free, and there’s no obligation.