Skip to content

fix(self-improving-mastra): bind app to 0.0.0.0 so Fargate health check passes#654

Merged
lionello merged 1 commit into
DefangLabs:mainfrom
defangdevs:fix/mastra-app-fargate-hostname-healthcheck
Jul 22, 2026
Merged

fix(self-improving-mastra): bind app to 0.0.0.0 so Fargate health check passes#654
lionello merged 1 commit into
DefangLabs:mainfrom
defangdevs:fix/mastra-app-fargate-hostname-healthcheck

Conversation

@defangdevs

Copy link
Copy Markdown
Contributor

Problem

Deploying the self-improving-mastra sample to AWS fails: the app service's container health check never passes, ECS kills and replaces the task in a loop, and defang up exits non-zero with:

deployment failed for service "app": TASK_DEACTIVATING Task failed container health checks

The app logs ✓ Ready and 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 to process.env.HOSTNAME. The Dockerfile sets ENV HOSTNAME=0.0.0.0 to bind all interfaces — but on Fargate the container runtime injects its own HOSTNAME (the task's private-DNS hostname, e.g. ip-10-0-97-61.us-west-2.compute.internal), and that runtime value overrides the image ENV. So Next binds only to the task's ENI IP:

  • ALB health check → hits the ENI IP → healthy
  • ECS container health check → curl localhost → nothing on loopback → fails ❌ → task UNHEALTHY → deploy fails

The startup banner in the failing logs confirms it: - Local: http://ip-10-0-97-61.us-west-2.compute.internal:3000 (a real 0.0.0.0 bind prints http://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 binds 0.0.0.0 regardless of HOSTNAME and is unaffected. The dev service passes the identical check because it fronts Next with Caddy, which binds all interfaces.

Fix

CMD ["sh", "-c", "HOSTNAME=0.0.0.0 exec node server.js"]

Force HOSTNAME=0.0.0.0 at launch so the runtime-injected value can't win, and exec so 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:

Condition Bind curl localhost:3000/api/health
HOSTNAME=0.0.0.0 (intended) 0.0.0.0:3000 {"ok":true}
HOSTNAME=ip-10-0-… (Fargate) ENI IP only connection refused
This fix 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

…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
@defangdevs
defangdevs deployed to deploy-changed-samples July 22, 2026 15:45 — with GitHub Actions Active
@lionello
lionello merged commit 51c7e6b into DefangLabs:main Jul 22, 2026
1 of 2 checks passed
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