Skip to content

chore: update Container examples and container docs for enableInternet - #85

Merged
sam-goodwin merged 3 commits into
alchemy-run:mainfrom
yovanoc:fix/container-start-enable-internet
May 25, 2026
Merged

sam-goodwin merged 3 commits into
alchemy-run:mainfrom
yovanoc:fix/container-start-enable-internet

Conversation

@yovanoc

@yovanoc yovanoc commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

CF Containers default to blocked public egress. When StartContainer.ts calls container.start() with no options:

  • cfeth0 only gets private addresses (10.0.0.1/24 + fd00::11) — every dns.resolve / fetch / TCP connect hangs indefinitely (25s hard-timeout).
  • Firecracker VMs are hermetic and don't inherit the Worker's env, so containers can't read Worker secrets via Bun.env.XXX.

Fix

  • Opt in to public egress by setting ALCHEMY_CONTAINER_ENABLE_INTERNET=1 in the Worker env (or as a secret). Off by default to match CF's default.
  • Log a one-line hint at container start when the flag is absent, so users hitting the DNS-hang symptom can discover the fix in logs instead of debugging Firecracker networking blind.
  • Forward string-valued Worker env entries into ContainerStartupOptions.env via Effect.serviceOption(WorkerEnvironment).

Verification

Confirmed via a /debug/connectivity probe inside a bun container:

  • Without flag: every outbound test hung 25s (hard-timeout). Log line emitted.
  • With ALCHEMY_CONTAINER_ENABLE_INTERNET=1: DNS resolves in <30ms, TCP connect to 1.1.1.1 completes in <20ms.

Notes

  • Opt-in keeps parity with CF's platform default (blocked egress) and avoids surprising users with opened network access.
  • Only string values are forwarded (DO/KV/R2 bindings are objects and can't cross the FFI boundary anyway).
  • The discoverability log is Effect.logInfo (not logWarning) since locked-down egress is a valid deployment choice.

@yovanoc
yovanoc force-pushed the fix/container-start-enable-internet branch from c37fffe to 1f3da0f Compare April 22, 2026 07:52
@yovanoc yovanoc changed the title fix(Cloudflare): enable container internet by default and forward Worker env fix(Cloudflare): opt-in container internet access + forward Worker env Apr 22, 2026
CF Containers default to blocked public egress — cfeth0 only gets private
addresses (10.0.0.1/24 + fd00::11), so DNS/fetch/TCP hang indefinitely.
Firecracker VMs are also hermetic and don't inherit the Worker's env.

- Opt in to public egress by setting ALCHEMY_CONTAINER_ENABLE_INTERNET=1
  in the Worker env (or as a secret). Off by default to match CF's default.
- Log a one-line hint at container start when the flag is absent, so users
  hitting the DNS-hang symptom can discover the fix in logs instead of
  debugging Firecracker networking blind.
- Forward string-valued Worker env entries into ContainerStartupOptions.env
  via Effect.serviceOption(WorkerEnvironment) so bun can read secrets via
  Bun.env.XXX at runtime.
@yovanoc
yovanoc force-pushed the fix/container-start-enable-internet branch from 1f3da0f to 7fab0e6 Compare May 4, 2026 10:50
@yovanoc
yovanoc marked this pull request as ready for review May 4, 2026 10:51
"Container starting with public egress DISABLED. Set ALCHEMY_CONTAINER_ENABLE_INTERNET=1 in the Worker env to enable internet access (cfeth0 has only private addresses by default).",
);
}
yield* container.start({ enableInternet, env });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHy do this with an environment variable?

Let's update this function to accept those arguments so the user can just pass an arg

export const start = Effect.fnUntraced(function* <
  Shape extends Container,
  Req = never,
>(containerEff: Effect.Effect<Shape, never, Req | DurableObjectState>) {

@yovanoc

yovanoc commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

@sam-goodwin Good call — much cleaner API. Updated:

Before (implicit env reading):

const container = yield* Cloudflare.start(sandbox);
// secretly reads ALCHEMY_CONTAINER_ENABLE_INTERNET from WorkerEnvironment

After (explicit options):

const container = yield* Cloudflare.start(sandbox, { enableInternet: true });

Changes:

  • Removed WorkerEnvironment service dependency from StartContainer.ts
  • start() now accepts optional second arg { enableInternet?: boolean; env?: Record<string, string> }
  • Updated Agent.ts example + all docs / test types to use the new signature
  • When enableInternet is absent/false, the log hint now says "Pass { enableInternet: true }" instead of referencing the env var

Pushed as a fixup commit on the same branch.

@yovanoc
yovanoc force-pushed the fix/container-start-enable-internet branch from 1a34170 to 4018632 Compare May 4, 2026 18:48
Comment on lines +20 to +23
options?: {
enableInternet?: boolean;
env?: Record<string, string>;
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the same type in the underlying Container .start function?

@yovanoc
yovanoc force-pushed the fix/container-start-enable-internet branch from 4018632 to 7145a19 Compare May 4, 2026 20:14
@yovanoc

yovanoc commented May 4, 2026 •

Copy link
Copy Markdown
Contributor Author

@sam-goodwin Done, didn't saw that type, thanks!

Comment on lines +27 to +36
// CF Containers default to BLOCKED public egress. Containers get only
// private addresses (10.0.0.1/24 + fd00::11) on cfeth0 and every DNS
// lookup / TCP connect / fetch hangs indefinitely unless enableInternet
// is explicitly set.
const enableInternet = options?.enableInternet ?? false;
if (!enableInternet) {
yield* Effect.logInfo(
"Container starting with public egress DISABLED. Pass { enableInternet: true } to enable internet access (cfeth0 has only private addresses by default).",
);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we dying here? This is not an error. It's a choice.

…iew feedback

Removes WorkerEnvironment service dependency from StartContainer.start().
Instead of implicitly reading ALCHEMY_CONTAINER_ENABLE_INTERNET and env
from the Worker environment via Effect.serviceOption, the function now
accepts an optional second argument:

  Cloudflare.start(sandbox, { enableInternet: true, env: { FOO: bar } })

This makes the API explicit and avoids hidden env var coupling. Users
who need internet access or env forwarding pass it directly.

Updates all examples, docs, and test types to use the new signature.
@yovanoc

yovanoc commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

Removed the log, just passes options through now.

@yovanoc
yovanoc force-pushed the fix/container-start-enable-internet branch from 7145a19 to c059b67 Compare May 5, 2026 06:10
@yovanoc

yovanoc commented May 16, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #341 which landed the Cloudflare.start(options) change in a narrower scope. The remaining diff here (doc updates, test fixes, Worker env forwarding) can be rebased and reopened as a follow-up if still needed.

@sam-goodwin sam-goodwin reopened this May 25, 2026
@sam-goodwin sam-goodwin changed the title fix(Cloudflare): opt-in container internet access + forward Worker env chore: update Container examples and container docs for enableInternet May 25, 2026
@sam-goodwin
sam-goodwin merged commit 59044b8 into alchemy-run:main May 25, 2026
1 of 4 checks passed
sam-goodwin added a commit that referenced this pull request Jul 28, 2026
DavidJFelix pushed a commit to DavidJFelix/alchemy-effect that referenced this pull request Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants