Skip to content
Agent loop · Build & CI

Container Until Healthy

An agent loop that builds your Docker image, starts the container, probes its health endpoint, and fixes the Dockerfile or config until it serves traffic.

Up to 8 iterationsStop rules and safety limits
The prompt

Copy the Container Until Healthy loop

Stops when the image builds and the container answers its health check within the timeout, or after 8 iterations. Edit the settings block, then paste the whole prompt into your agent.

container-until-healthy.prompt.md
You are running the "Container Until Healthy" loop.

Settings (edit before running):
- Build command: docker build -t app-loop-test .
- Run command: docker run -d --name app-loop-test -p 8080:8080 --env-file .env.example app-loop-test
- Health check: GET http://localhost:8080/health returns 200
- Start-up timeout: 60 seconds
- Max iterations: 8

Goal: the image builds and the container passes the health check within the timeout.

Each iteration:
1. Run the build command. If it fails, read the failing step and fix it.
2. Run the container. Poll the health check every 5 seconds until it passes or the timeout ends.
3. If it fails, collect "docker ps -a" status for the container and the last 100 lines of "docker logs".
4. Identify the first error in the logs (missing file, wrong port, missing env var, permission denied, crash on boot) and make one fix.
5. Stop and remove the test container before the next iteration.

Stop when:
- The health check passes. Success. Leave the container stopped and removed.
- You reach the max iteration count.

Rules:
- Only use example or dummy values for environment variables. Never copy real secrets into the Dockerfile, image layers, or logs.
- Only stop and remove containers and images tagged app-loop-test. Do not prune or touch other containers, images, or volumes.
- Do not run the container with --privileged or mount the Docker socket.
- Keep the image non-root if it already is.

Final report: the Dockerfile and config changes with reasons, final image size, and the start-up time you measured.
When to use it

When this loop fits

  • You are containerizing an app for the first time and the image builds but the container exits on start.
  • A base image or runtime upgrade broke the container and the logs are not obvious.
  • You want a slimmer or non-root image and need to confirm it still boots after each change.
How it works

What happens on each pass

The agent repeats these steps until the stop condition holds or it runs out of iterations.

  1. Build the image. Build with a throwaway tag and capture the build output.
  2. Start the container. Run it detached with the expected port mapping and environment, then wait for the start-up window.
  3. Probe health. Request the health endpoint and collect the container status and its recent logs.
  4. Fix one cause. Change the Dockerfile, entrypoint, or app config to address the first error in the logs.
  5. Clean up and repeat. Stop and remove the test container, then rebuild and probe again.
Recommended servers

MCP servers for this loop

The 6 strongest of 763 matching servers, ranked by how closely they fit and then by trust score.

More in Cloud
  • kubernetes-mcp-server logo
    Trust score 76 of 100, Good

    A Model Context Protocol (MCP) server for Kubernetes and OpenShift

    Registry
    LicenseQualityMaintenanceLicense: A. Apache-2.0 is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 15 days ago.
    2.2K stars—/wk weekly downloads— tools
  • Trust score 74 of 100, Good

    Kubernetes MCP server for diagnosis, resource management, GitOps, and RBAC-enforced operations.

    Registry
    LicenseQualityMaintenanceLicense: A. Apache-2.0 is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release yesterday.
    3.9K stars—/wk weekly downloads— tools
  • Trust score 72 of 100, Good

    kubeview

    by mikhae1 · TypeScript

    Read-only Model Context Protocol MCP server enabling code-driven AI analysis of Kubernetes clusters.

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 28 days ago.
    5 stars379/wk weekly downloads— tools
  • Trust score 68 of 100, Good

    Manage Docker (containers, images, Compose, Swarm, registries) via the Docker SDK and CLI.

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 12 days ago.
    9 stars—/wk weekly downloads— tools
  • Trust score 68 of 100, Good

    Dockerhub

    by pipeworx-io · JavaScript

    Docker Hub MCP — wraps the Docker Hub v2 API (free, no auth required for public data)

    RegistryRemote
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 10 days ago.
    0 stars47/wk weekly downloads— tools
  • Trust score 68 of 100, Good

    Hot-reload for MCP servers + mount any community server mid-session, Docker optional. No restarts.

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 20 days ago.
    0 stars87/wk weekly downloads— tools
Run it

How to run this loop

  1. Connect the tools. Install a server the loop can use, for example kubernetes-mcp-server, or rely on your agent's built-in terminal and file access.
  2. Edit the settings block. Replace the example commands, targets, and limits with your project's own. Keep the iteration cap low on the first run.
  3. Paste it into an agent session. Start a fresh session in Claude Code, Cursor, or VS Code agent mode, in the repository root, and paste the prompt.
  4. Review before you merge. Read the final report and the diff. The loop leaves its work for you to review rather than shipping it.
FAQ

Container Until Healthy questions

When does the Container Until Healthy loop stop?

It stops as soon as the image builds and the container answers its health check within the timeout. It also stops after 8 iterations by default, or earlier when the prompt's own stop rules say it is stuck, and then reports what changed and what is left. Change the cap in the settings block at the top of the prompt.

Do I need a Docker MCP server for this loop?

No. An agent with shell access can run docker commands directly. A Docker MCP server helps when your client has no terminal, or when you want the agent limited to container operations instead of a full shell.

Does this work with Docker Compose?

Yes. Swap the build and run commands for docker compose build and docker compose up -d, point the health check at the service you care about, and restrict cleanup to that compose project with docker compose down.

Keep exploring
All loops