From 08a869f0869aba194bee9f50266b9000c369f1ec Mon Sep 17 00:00:00 2001 From: defangdevs Date: Wed, 22 Jul 2026 08:45:00 -0700 Subject: [PATCH] fix(self-improving-mastra): bind app to 0.0.0.0 so Fargate health check 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=, 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 Claude-Session: https://claude.ai/code/session_01Sw7j9FYnbChZJbYArxbhuo --- samples/self-improving-mastra/todo-app/Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/samples/self-improving-mastra/todo-app/Dockerfile b/samples/self-improving-mastra/todo-app/Dockerfile index aff439b92..2917690cd 100644 --- a/samples/self-improving-mastra/todo-app/Dockerfile +++ b/samples/self-improving-mastra/todo-app/Dockerfile @@ -27,4 +27,8 @@ COPY --from=builder --chown=nextjs:nodejs /app/lib/schema.sql ./lib/schema.sql USER nextjs EXPOSE 3000 -CMD ["node", "server.js"] +# Docker/Fargate inject HOSTNAME= at runtime, overriding the +# ENV above. Next's standalone server binds to $HOSTNAME, so it would listen only +# on the task's ENI IP and the `curl localhost` container health check would fail. +# Re-force 0.0.0.0, then `exec` so node stays PID 1 and receives SIGTERM directly. +CMD ["sh", "-c", "HOSTNAME=0.0.0.0 exec node server.js"]