fix(self-improving-mastra): bind app to 0.0.0.0 so Fargate health check passes#654
Merged
lionello merged 1 commit intoJul 22, 2026
Conversation
…ck passes The `app` service's container health check (`curl -f localhost:3000/api/health`) fails on AWS Fargate, killing the task in a loop and failing `defang up`. Root cause: the runtime (Docker/Fargate) injects HOSTNAME=<container hostname>, which overrides the image's `ENV HOSTNAME=0.0.0.0`. Next.js standalone binds its server to $HOSTNAME, so it listens only on the task's ENI IP. The ALB health check (which hits that IP) passes, but the in-container `curl localhost` check never connects, so ECS marks the task UNHEALTHY. Fix: force HOSTNAME=0.0.0.0 in the launch command and `exec` node so it stays PID 1 and still receives SIGTERM for graceful shutdown. Verified against the exact failing ECR image: reproduced the localhost refusal with an injected HOSTNAME, and confirmed the fix restores the 0.0.0.0 bind, a passing health check, and clean exit-0 on SIGTERM. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sw7j9FYnbChZJbYArxbhuo
lionello
approved these changes
Jul 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Deploying the
self-improving-mastrasample to AWS fails: theappservice's container health check never passes, ECS kills and replaces the task in a loop, anddefang upexits non-zero with:The app logs
✓ Readyand even the ALB target reports healthy — but the ECS container health check (curl -f http://localhost:3000/api/health, run inside the task) never connects.Root cause
Next.js standalone (
output: "standalone"→node server.js) binds its HTTP server toprocess.env.HOSTNAME. The Dockerfile setsENV HOSTNAME=0.0.0.0to bind all interfaces — but on Fargate the container runtime injects its ownHOSTNAME(the task's private-DNS hostname, e.g.ip-10-0-97-61.us-west-2.compute.internal), and that runtime value overrides the imageENV. So Next binds only to the task's ENI IP:curl localhost→ nothing on loopback → fails ❌ → task UNHEALTHY → deploy failsThe startup banner in the failing logs confirms it:
- Local: http://ip-10-0-97-61.us-west-2.compute.internal:3000(a real0.0.0.0bind printshttp://localhost:3000).This is Next-standalone-specific — it's the only Next.js sample in the repo using standalone output. The others use
next start, which binds0.0.0.0regardless ofHOSTNAMEand is unaffected. Thedevservice passes the identical check because it fronts Next with Caddy, which binds all interfaces.Fix
Force
HOSTNAME=0.0.0.0at launch so the runtime-injected value can't win, andexecso node replaces the shell — staying PID 1 and receiving ECS's SIGTERM directly for graceful shutdown (no signal-swallowing shell wrapper).Verification
Reproduced and fixed against the exact failing ECR image:
curl localhost:3000/api/healthHOSTNAME=0.0.0.0(intended)0.0.0.0:3000{"ok":true}HOSTNAME=ip-10-0-…(Fargate)0.0.0.0:3000{"ok":true}Also confirmed node is PID 1 under the fix and exits 0 promptly on SIGTERM.
🤖 Generated with Claude Code