PromptisePromptise
PlatformSoonEnterprise
DocsGitHub
Talk to usGet started
Promptise - AI Framework LogoPromptise

The full-stack framework for agentic systems. Build with the framework. Run it on the platform. Launch it with our engineers.

pip install promptise

[01] Product

  • Framework
  • Platform (coming soon)
  • Enterprise
  • How it works
  • Contact

[02] Framework

  • The Agent
  • The Interface
  • The Harness

[03] Resources

  • Documentation
  • GitHub
  • Why Promptise
  • Use cases
  • Roadmap
  • Guides
  • Learning Paths
  • Questions

[04] Company

  • About
  • Terms of Service
  • Privacy Policy
  • Cookie Policy
  • Subprocessors

© 2026 Promptise by Manser Ventures. All rights reserved.

hello@promptise.com
← Learning paths> Agentic Engineering with Promptise Foundry
Lesson 43 / 100
43%

How to Deploy an AI Agent with Docker (2026 Guide)

Deploy an AI agent with Docker: a Dockerfile, env vars and secrets, health checks, graceful shutdown, and docker compose with the agent's MCP server.

Intermediate18 minDeploymentModel Context Protocol (MCP)
All 100 lessons↓
  1. ✓What Is an AI Agent? Definition, How It Works, Examples
  2. ✓How to Build an AI Agent in Python (2026 Guide)
  3. ✓Build a Personal AI Agent: Memory, Reminders, Chat
  4. ✓What Is an Agent Harness? Parts and How to Build One
  5. ✓System Prompt Best Practices for AI Agents in Python
  6. ✓Best LLM for AI Agents (2026): Picks, Prices, How to Test
  7. ✓AI Agent YAML: Define and Run an Agent From a Config File
  8. ✓Streaming LLM Responses with FastAPI: Tokens and Tool Calls
  9. ✓Best Local LLM for AI Agents: Ollama Models by GPU (2026)
  10. ✓Claude AI Agent with MCP: Use Anthropic Models in Promptise
  11. ✓Gemini AI Agent with MCP: Use Google Models in Promptise
  12. ✓Azure OpenAI Agent with MCP: Use Deployments in Promptise
  13. ✓OpenRouter AI Agent with MCP: Many Models in Promptise
  14. ✓Ollama AI Agent with MCP: Run Local Models in Promptise
  15. ✓LLM Semantic Cache and Model Fallback in Python
  16. ✓What Is MCP (Model Context Protocol)? A Developer's Guide
  17. ✓What Is Tool Calling? LLM Function Calling Explained
  18. ✓MCP Tools vs Resources vs Prompts (With Python Examples)
  19. ✓How to Connect MCP Servers to Your AI Agent in Python
  20. ✓Build a Production MCP Server in Python (Step by Step)
  21. ✓MCP Transports: stdio vs Streamable HTTP vs SSE (Python)
  22. ✓MCP 2026-07-28 Migration: Make Your MCP Server Stateless
  23. ✓OpenAPI to MCP: Turn Any REST API into an MCP Server
  24. ✓MCP Authentication in Python: API Keys, JWT and OAuth
  25. ✓Build an Authenticated MCP Server for Your AI Agent (Python)
  26. ✓MCP Security Best Practices: A Checklist for MCP Servers
  27. ✓Testing MCP Servers in Python: pytest Without a Live Server
  28. ✓Too Many MCP Tools? How to Fix LLM Tool Selection in Python
  29. ✓Idempotent Tool Calls: Make AI Agent Retries Safe
  30. ✓AI Agent Tool Failures: Stop Unsafe Workarounds
  31. ✓MCP Server Caching, Timeouts and Circuit Breakers in Python
  32. ✓MCP Middleware for Servers: Auth, Logging, Rate Limits
  33. ✓MCP vs CLI for AI Agents: Tokens, Security, When to Use Each
  34. ✓LangChain vs MCP: Differences and How They Work Together
  35. ✓FastMCP Alternatives for Production MCP Servers (2026)
  36. ✓AI Agent Audit Logging: Tamper-Evident Logs in Python
  37. ✓AI Agent Human in the Loop: Approve Tool Calls in Python
  38. ✓AI Agent Prompt Injection: How to Defend Against It
  39. ✓Hidden Text Prompt Injection: Sanitize Pages for AI Agents
  40. ✓Build a Multi-Agent System in Python (and When Not To)
  41. ✓Context Engineering for AI Agents: Context Window Management
  42. ✓Context Compaction for AI Agents: Trim vs Summarize
  43. 43How to Deploy an AI Agent with Docker (2026 Guide)
  44. 44LLM Tool Rate Limiting: Per-Client Limits for MCP Servers
  45. 45MCP Long-Running Tasks: Background Jobs in Python
  46. 46MCP Server Monitoring: Prometheus, OpenTelemetry and Logs
  47. 47Best AI Agent Frameworks (2026): Python Options Compared
  48. 48Self-Improving AI Agent: Learn From Tool Failures in Python
  49. 49AI Agent Identity: Authenticate Agents to MCP Servers
  50. 50Code-Action Agents in Python: One Program vs 37 Tool Calls
  51. 51Multi-Tenant AI Agents: Keep Each Customer's Data Apart
  52. 52What Is Agentic RAG? How It Differs from Classic RAG
  53. 53AI Agent Memory in Python: Short-Term and Long-Term
  54. 54Per-User Agent Memory Isolation: Stop Leaks Between Users
  55. 55Agentic RAG in Python: Give Your AI Agent a Search Tool
  56. 56AI Agent Reasoning Patterns in Python: From ReAct to Debate
  57. 57Plan-and-Execute Agents in Python: Beyond the ReAct Loop
  58. 58Self-Modifying AI Agent in Python: Open Mode & Limits
  59. 59CrewAI Alternatives: 7 Options and How to Migrate (2026)
  60. 60Migrate from AutoGen: Agent Framework, AG2 and More
  61. 61What Is an LLM Guardrail? Types, Examples and Limits
  62. 62Best LLM Guardrails Tools (2026): 9 Platforms Compared
  63. 63AI Agent Guardrails in Python: Block Injection, Redact PII
  64. 64NeMo Guardrails vs Guardrails AI: Which to Choose (2026)
  65. 65AI-Based PII Detection and Redaction Tools for LLM Apps
  66. 66Stop Secrets Leaking Into LLM Logs and Traces
  67. 67AI Agent Code Execution Sandbox: Run LLM Code Safely
  68. 68AI Agent Network Egress: Limit Where Agents Can Connect
  69. 69AutoGen Docker Task Isolation: Secure the Code Executor
  70. 70SPIFFE for AI Agents: Workload Identity Without API Keys
  71. 71On-Behalf-Of Auth for AI Agents: Act as Each User
  72. 72AI Agent Production Checklist: What to Check Before Launch
  73. 73Approval Fatigue: Auto-Approve Safe AI Agent Actions
  74. 74What Is Human in the Loop AI? Approvals for AI Agents
  75. 75AI Agent Notifications: Webhook and Slack Alerts in Python
  76. 76AI Agent Observability: Trace Tool Calls, Tokens and Cost
  77. 77Best AI Agent Observability Tools (2026), Compared
  78. 78AI Agent Stuck in a Loop? How to Detect and Stop It
  79. 79How to Run, Debug and Deploy an AI Agent in Python
  80. 80AI Agent Cost Tracking: Tokens and Cost per Run and User
  81. 81How to Evaluate AI Agents: Evals, LLM Judges and pass^k
  82. 82AI Agent Cost Control: Budgets That Stop Runaway Spend
  83. 83How to Test LLM Prompts and AI Agents in Python
  84. 84AI Agent Evals in CI: Run Them on Every Pull Request
  85. 85What Is an Agentic Runtime? AI Agent Runtimes Explained
  86. 86Autonomous AI Agent Runtime: Budgets, Crash Recovery, API
  87. 87Event-Driven AI Agents: Webhook, Cron and File Triggers
  88. 88AI Agent Cron Jobs: Run an Agent on a Schedule
  89. 89AI Agent Webhook Trigger: Run an Agent on HTTP Events
  90. 90AI Agent File Watcher: Process New Files Automatically
  91. 91Custom AI Agent Triggers: Wake an Agent From Any Source
  92. 92AI Agent Event Filters: Skip Runs Before the LLM
  93. 93AI Agent Retries and a Dead Letter Queue for Failed Runs
  94. 94Run Multiple AI Agents: One Runtime, Many Processes
  95. 95AI Agent Manifest: Declare a Long-Running Agent in YAML
  96. 96AI Agent CLI: Start, Stop and Inspect Running Agents
  97. 97AI Agent Monitoring Dashboard in the Terminal
  98. 98Multi-Agent Event Bus: Chain Agents With Events
  99. 99Distributed AI Agents: Run Agents Across Many Machines
  100. 100Pause and Resume an AI Agent: Lifecycle States Explained

Lessons in this path

  1. ✓What Is an AI Agent? Definition, How It Works, Examples
  2. ✓How to Build an AI Agent in Python (2026 Guide)
  3. ✓Build a Personal AI Agent: Memory, Reminders, Chat
  4. ✓What Is an Agent Harness? Parts and How to Build One
  5. ✓System Prompt Best Practices for AI Agents in Python
  6. ✓Best LLM for AI Agents (2026): Picks, Prices, How to Test
  7. ✓AI Agent YAML: Define and Run an Agent From a Config File
  8. ✓Streaming LLM Responses with FastAPI: Tokens and Tool Calls
  9. ✓Best Local LLM for AI Agents: Ollama Models by GPU (2026)
  10. ✓Claude AI Agent with MCP: Use Anthropic Models in Promptise
  11. ✓Gemini AI Agent with MCP: Use Google Models in Promptise
  12. ✓Azure OpenAI Agent with MCP: Use Deployments in Promptise
  13. ✓OpenRouter AI Agent with MCP: Many Models in Promptise
  14. ✓Ollama AI Agent with MCP: Run Local Models in Promptise
  15. ✓LLM Semantic Cache and Model Fallback in Python
  16. ✓What Is MCP (Model Context Protocol)? A Developer's Guide
  17. ✓What Is Tool Calling? LLM Function Calling Explained
  18. ✓MCP Tools vs Resources vs Prompts (With Python Examples)
  19. ✓How to Connect MCP Servers to Your AI Agent in Python
  20. ✓Build a Production MCP Server in Python (Step by Step)
  21. ✓MCP Transports: stdio vs Streamable HTTP vs SSE (Python)
  22. ✓MCP 2026-07-28 Migration: Make Your MCP Server Stateless
  23. ✓OpenAPI to MCP: Turn Any REST API into an MCP Server
  24. ✓MCP Authentication in Python: API Keys, JWT and OAuth
  25. ✓Build an Authenticated MCP Server for Your AI Agent (Python)
  26. ✓MCP Security Best Practices: A Checklist for MCP Servers
  27. ✓Testing MCP Servers in Python: pytest Without a Live Server
  28. ✓Too Many MCP Tools? How to Fix LLM Tool Selection in Python
  29. ✓Idempotent Tool Calls: Make AI Agent Retries Safe
  30. ✓AI Agent Tool Failures: Stop Unsafe Workarounds
  31. ✓MCP Server Caching, Timeouts and Circuit Breakers in Python
  32. ✓MCP Middleware for Servers: Auth, Logging, Rate Limits
  33. ✓MCP vs CLI for AI Agents: Tokens, Security, When to Use Each
  34. ✓LangChain vs MCP: Differences and How They Work Together
  35. ✓FastMCP Alternatives for Production MCP Servers (2026)
  36. ✓AI Agent Audit Logging: Tamper-Evident Logs in Python
  37. ✓AI Agent Human in the Loop: Approve Tool Calls in Python
  38. ✓AI Agent Prompt Injection: How to Defend Against It
  39. ✓Hidden Text Prompt Injection: Sanitize Pages for AI Agents
  40. ✓Build a Multi-Agent System in Python (and When Not To)
  41. ✓Context Engineering for AI Agents: Context Window Management
  42. ✓Context Compaction for AI Agents: Trim vs Summarize
  43. 43How to Deploy an AI Agent with Docker (2026 Guide)
  44. 44LLM Tool Rate Limiting: Per-Client Limits for MCP Servers
  45. 45MCP Long-Running Tasks: Background Jobs in Python
  46. 46MCP Server Monitoring: Prometheus, OpenTelemetry and Logs
  47. 47Best AI Agent Frameworks (2026): Python Options Compared
  48. 48Self-Improving AI Agent: Learn From Tool Failures in Python
  49. 49AI Agent Identity: Authenticate Agents to MCP Servers
  50. 50Code-Action Agents in Python: One Program vs 37 Tool Calls
  51. 51Multi-Tenant AI Agents: Keep Each Customer's Data Apart
  52. 52What Is Agentic RAG? How It Differs from Classic RAG
  53. 53AI Agent Memory in Python: Short-Term and Long-Term
  54. 54Per-User Agent Memory Isolation: Stop Leaks Between Users
  55. 55Agentic RAG in Python: Give Your AI Agent a Search Tool
  56. 56AI Agent Reasoning Patterns in Python: From ReAct to Debate
  57. 57Plan-and-Execute Agents in Python: Beyond the ReAct Loop
  58. 58Self-Modifying AI Agent in Python: Open Mode & Limits
  59. 59CrewAI Alternatives: 7 Options and How to Migrate (2026)
  60. 60Migrate from AutoGen: Agent Framework, AG2 and More
  61. 61What Is an LLM Guardrail? Types, Examples and Limits
  62. 62Best LLM Guardrails Tools (2026): 9 Platforms Compared
  63. 63AI Agent Guardrails in Python: Block Injection, Redact PII
  64. 64NeMo Guardrails vs Guardrails AI: Which to Choose (2026)
  65. 65AI-Based PII Detection and Redaction Tools for LLM Apps
  66. 66Stop Secrets Leaking Into LLM Logs and Traces
  67. 67AI Agent Code Execution Sandbox: Run LLM Code Safely
  68. 68AI Agent Network Egress: Limit Where Agents Can Connect
  69. 69AutoGen Docker Task Isolation: Secure the Code Executor
  70. 70SPIFFE for AI Agents: Workload Identity Without API Keys
  71. 71On-Behalf-Of Auth for AI Agents: Act as Each User
  72. 72AI Agent Production Checklist: What to Check Before Launch
  73. 73Approval Fatigue: Auto-Approve Safe AI Agent Actions
  74. 74What Is Human in the Loop AI? Approvals for AI Agents
  75. 75AI Agent Notifications: Webhook and Slack Alerts in Python
  76. 76AI Agent Observability: Trace Tool Calls, Tokens and Cost
  77. 77Best AI Agent Observability Tools (2026), Compared
  78. 78AI Agent Stuck in a Loop? How to Detect and Stop It
  79. 79How to Run, Debug and Deploy an AI Agent in Python
  80. 80AI Agent Cost Tracking: Tokens and Cost per Run and User
  81. 81How to Evaluate AI Agents: Evals, LLM Judges and pass^k
  82. 82AI Agent Cost Control: Budgets That Stop Runaway Spend
  83. 83How to Test LLM Prompts and AI Agents in Python
  84. 84AI Agent Evals in CI: Run Them on Every Pull Request
  85. 85What Is an Agentic Runtime? AI Agent Runtimes Explained
  86. 86Autonomous AI Agent Runtime: Budgets, Crash Recovery, API
  87. 87Event-Driven AI Agents: Webhook, Cron and File Triggers
  88. 88AI Agent Cron Jobs: Run an Agent on a Schedule
  89. 89AI Agent Webhook Trigger: Run an Agent on HTTP Events
  90. 90AI Agent File Watcher: Process New Files Automatically
  91. 91Custom AI Agent Triggers: Wake an Agent From Any Source
  92. 92AI Agent Event Filters: Skip Runs Before the LLM
  93. 93AI Agent Retries and a Dead Letter Queue for Failed Runs
  94. 94Run Multiple AI Agents: One Runtime, Many Processes
  95. 95AI Agent Manifest: Declare a Long-Running Agent in YAML
  96. 96AI Agent CLI: Start, Stop and Inspect Running Agents
  97. 97AI Agent Monitoring Dashboard in the Terminal
  98. 98Multi-Agent Event Bus: Chain Agents With Events
  99. 99Distributed AI Agents: Run Agents Across Many Machines
  100. 100Pause and Resume an AI Agent: Lifecycle States Explained
← Path overview

This guide shows how to deploy an AI agent with Docker, from a Dockerfile to a docker compose stack with the agent's MCP tool server beside it. It is for developers who have an agent that works on their laptop and now need it to run unattended. You will package the agent as an HTTP service, configure it with environment variables, keep secrets out of the image, add health and readiness checks, and make it finish its running requests when the container stops. Every command below was run, and the outputs are real.

[01]

How do you deploy an AI agent with Docker?

Run the agent as a long-lived HTTP service in a slim Python image, with every setting and secret coming from environment variables. Give it a liveness endpoint and a readiness endpoint, start it in exec form so it receives SIGTERM, and let it drain running requests before it exits. Put each MCP tool server in its own container on a private network, and start the agent only once those servers are healthy. With Promptise Foundry, promptise run --prod --serve provides the HTTP API, the probes, JSON logs and the drain.

  • Build once, configure at run time. The same image runs in staging and production. Only the environment changes.

  • Secrets never enter the image. Exclude .env with .dockerignore, and inject keys from your orchestrator.

  • Liveness and readiness are different questions. /healthz means the process is up. /readyz means the agent is built and its tools are connected.

  • Shutdown is part of the deploy. A rolling update sends SIGTERM. An agent request can take many seconds, so the grace period must be longer than your slowest request.

  • Agents scale out like any stateless web service, as long as conversation state lives in a shared store.


[02]

How a containerized AI agent works

An agent service has the same shape as any API service. The difference is what happens inside a request: several model calls and tool calls that can take from a few seconds to a minute.

A client and probes call the agent container (promptise run --prod --serve with /healthz, /readyz, /v1/invoke and /v1/stream), which calls the shop-mcp container over MCP on HTTP inside a private docker compose network. The agent also calls the model provider API over HTTPS, shop-mcp reaches the orders DB, and the keys come in as environment variables, never baked in.
FigureA containerized agent service: the agent container, its MCP tool server and what they connect to

Four things change when an agent moves from a script to a container:

  • Configuration comes from the environment. A .env file on your laptop must not end up in the image.

  • The process is managed. Docker or Kubernetes starts it, probes it, restarts it when it dies and stops it with SIGTERM.

  • Startup has dependencies. The agent connects to its MCP servers when it is built. If a server isn't up yet, the agent fails to start.

  • More than one copy may run. Anything kept in process memory, such as a conversation, is lost on restart and not shared between replicas.


[03]

When to use Docker for an AI agent (and when not to)

Use a container when the agent is a service other systems call: a support bot behind your web app, an internal API, a worker that runs on a schedule. It gives you the same artifact in every environment, and it is the unit Kubernetes, ECS, Cloud Run and Fly.io deploy.

You don't need a separate container when the agent is a library inside an existing web app. Then you deploy the app as usual and call build_agent() inside it. A one-off batch job can also run with plain python main.py in CI. For agents that run on their own for days with triggers and budgets, see deploying long-running AI agents.


[04]

What you need

  • Docker 24 or later with Docker Compose v2. The commands here ran on Docker 27.4.

  • Python 3.10 or later and Promptise Foundry 1.10.0 for local runs: pip install "promptise==1.10.0".

  • An OpenAI API key in OPENAI_API_KEY. The agent uses gpt-5-mini.

  • About 30 minutes, most of it the first image build.

The example is a shop support agent with two tools, get_order and check_stock, served by an MCP server. The layout:

Text
1
2
3
4
5
6
7
8
9
support-bot/
├── compose.yaml
├── agent/          # the agent service
│   ├── Dockerfile  .dockerignore  requirements.txt
│   ├── promptise.toml
│   └── agent.py
└── shop-mcp/       # the MCP tool server
    ├── Dockerfile  .dockerignore
    └── server.py

[05]

Step 1: Write the agent factory and the manifest

Build the agent in a function, and read every deployment setting from the environment. The URL of the MCP server is configuration, not code:

Pythonagent/agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"""The support agent. It reaches the shop tools over MCP at SHOP_MCP_URL."""
import os

from promptise import PromptiseAgent, build_agent
from promptise.config import HTTPServerSpec


async def create_agent(ctx=None) -> PromptiseAgent:
    return await build_agent(
        model=os.environ.get("AGENT_MODEL", "openai:gpt-5-mini"),
        instructions="You answer shop customers. Use the tools for facts. Keep answers short.",
        servers={"shop": HTTPServerSpec(url=os.environ["SHOP_MCP_URL"])},
        max_invocation_time=60,      # seconds; a stuck request ends instead of hanging
    )

max_invocation_time=60 ends a request that runs longer than a minute, so one stuck request can't hold a worker forever. The manifest tells promptise run which factory to serve, which variables must be set, and how to behave in production:

TOMLagent/promptise.toml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name = "support-bot"
version = "1.0.0"
agent = "agent.py:create_agent"

[env]
required = ["OPENAI_API_KEY", "SHOP_MCP_URL", "PROMPTISE_SERVE_TOKEN"]

[prod]
log_format = "json"
observe = "basic"          # one JSON log line per run, with its token totals
shutdown_timeout = 30      # seconds running requests get after SIGTERM

[serve]
port = 8080
max_concurrency = 8        # requests beyond this get 429

[pricing]
"gpt-5-mini*" = { input = 0.25, output = 2.00 }
  • [env] required lists the variables the container needs. If one is missing, the process stops with exit code 2 before it builds anything.

  • [prod] turns on JSON logs on stderr, records one log line per agent run with its token totals (observe = "basic"), and gives running requests 30 seconds after SIGTERM.

  • [serve] sets the port and how many requests run at the same time. Requests beyond max_concurrency get 429 with Retry-After instead of piling up.

  • [pricing] adds a cost estimate to the final stats log line.

The manifest holds no secrets, so it is safe to commit and to copy into the image.


[06]

Step 2: Write the Dockerfile

Dockerfileagent/Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# syntax=docker/dockerfile:1
FROM python:3.12-slim

ENV PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    # Configuration comes from the container's environment, never from a .env file.
    PROMPTISE_NO_DOTENV=1

# An unprivileged user to run the agent.
RUN useradd --create-home --uid 10001 app
WORKDIR /app

# Dependencies first, so a code change does not reinstall them.
COPY requirements.txt .
RUN pip install -r requirements.txt

# Owned by root: the app user can read the code and the manifest, not change them.
COPY promptise.toml agent.py ./

USER app
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=20s --retries=3 \
  CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/readyz', timeout=2)"]

# Exec form: promptise is PID 1 and receives the SIGTERM that docker stop sends.
CMD ["promptise", "run", "--prod", "--serve", "--host", "0.0.0.0"]

What each part does:

  • python:3.12-slim is a small Debian image with Python. The built agent image is about 525 MB, most of it Python packages.

  • PROMPTISE_NO_DOTENV=1 turns off .env loading, so a stray file can never override the container's environment.

  • Dependencies before code. requirements.txt pins promptise==1.10.0, and Docker caches that layer, so changing agent.py rebuilds in seconds.

  • COPY leaves the code owned by root. The app user can read agent.py and the manifest but can't change them. promptise run accepts a manifest owned by root or by the user that runs it, because the manifest decides what runs. It refuses one owned by another user, for example after --chown to a different uid, or one anyone can write.

  • USER app runs the agent without root, which limits what a compromised process can do: it can't install packages or change the Python installation.

  • HEALTHCHECK uses Python's standard library, because the slim image has no curl. It checks /readyz, so Docker reports the container healthy only once the agent is built.

  • Exec form CMD [...] makes promptise process 1. With the shell form (CMD promptise run ...), /bin/sh can end up as process 1, and it does not pass the SIGTERM from docker stop on to the agent.

The .dockerignore keeps secrets and local state out of the build context:

Textagent/.dockerignore
1
2
3
4
5
6
7
8
.env
.env.*
.venv/
__pycache__/
.promptise/
reports/
*.json
.git/

[07]

Step 3: Configure it with environment variables

Every setting the container needs arrives through the environment, and the process checks them before it starts. Run the image without them to see what happens:

>_Terminal
docker run --rm -e OPENAI_API_KEY support-bot-agent; echo "exit code: $?"
Output
{"ts": "2026-10-23T23:41:25.268+00:00", "level": "error", "logger": "promptise.run", "msg": "preflight failed", "problems": ["missing required environment variable(s): SHOP_MCP_URL, PROMPTISE_SERVE_TOKEN (set them, or add them to .env)"]}
exit code: 2

One JSON log line lists every missing variable at once, and the exit code is 2, so an orchestrator reports a configuration error instead of a crash loop with a stack trace. The serve token is required because the container listens on 0.0.0.0: promptise run refuses to serve on a network address without a bearer token.

In CI, check the image with the same command the container runs, plus --check:

>_Terminal
docker run --rm -e OPENAI_API_KEY -e SHOP_MCP_URL -e PROMPTISE_SERVE_TOKEN \
  support-bot-agent promptise run --prod --serve --check
Output
✓ support-bot: agent.py:create_agent (serve) is ready

--check validates the manifest, the environment and the factory's module, then exits with 0 or 2 without serving anything.

Where secrets come from

Pass secrets as environment variables from the platform's secret store: Kubernetes Secrets, AWS Secrets Manager through ECS task definitions, Docker Swarm secrets, or your CI's protected variables. Never use ENV OPENAI_API_KEY=... or ARG for secrets in a Dockerfile, because both end up in the image metadata. Generate the serve token with openssl rand -hex 32 and rotate it like any API key. AI agent secrets management covers scoped, expiring credentials inside the agent.


[08]

Step 4: Put the MCP server in its own container

The tool server is a separate service with its own image, so you can deploy, scale and restrict it on its own. It needs one change from the local version: it must listen on all interfaces inside its container, and it should only answer the host names it is reached by.

Pythonshop-mcp/server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""A minimal MCP server for a small shop: two tools and one resource.

Run it over stdio (the default) or over Streamable HTTP:
    python server.py            # stdio: the client starts this process
    python server.py http       # HTTP on port 8000, path /mcp
"""
import os
import sys

from promptise.mcp.server import MCPServer

server = MCPServer(name="shop", version="1.0.0")

ORDERS = {
    "A1001": {"status": "delivered", "carrier": "UPS", "delivered_on": "2026-10-12"},
    "A1002": {"status": "in_transit", "carrier": "DHL", "eta": "2026-10-21"},
}
STOCK = {"TENT-2P": 4, "STOVE-MINI": 0}


@server.tool(read_only_hint=True)
async def get_order(order_id: str) -> dict:
    """Look up an order by its id (for example A1002): status, carrier and dates."""
    order = ORDERS.get(order_id.strip().upper())
    if order is None:
        return {"error": f"No order {order_id!r}. Order ids look like A1001."}
    return {"order_id": order_id.upper(), **order}


@server.tool(read_only_hint=True)
async def check_stock(sku: str) -> dict:
    """How many units of a product (by SKU, for example TENT-2P) are in stock."""
    units = STOCK.get(sku.strip().upper())
    if units is None:
        return {"error": f"Unknown SKU {sku!r}."}
    return {"sku": sku.upper(), "units": units}


@server.resource("shop://policies/returns", mime_type="text/markdown")
async def returns_policy() -> str:
    """The shop's return policy."""
    return "Returns are free within 30 days of delivery. Items must be unused."


if __name__ == "__main__":
    transport = sys.argv[1] if len(sys.argv) > 1 else "stdio"
    server.run(
        transport=transport,
        host=os.environ.get("MCP_HOST", "127.0.0.1"),
        port=8000,
        # Host headers this server answers: its compose service name and localhost.
        allowed_hosts=["shop-mcp:*", "localhost:*", "127.0.0.1:*"],
    )
Dockerfileshop-mcp/Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PIP_DISABLE_PIP_VERSION_CHECK=1
WORKDIR /app
RUN pip install "promptise==1.10.0"
COPY server.py .
RUN useradd --create-home --uid 10001 app
USER app
ENV MCP_HOST=0.0.0.0
EXPOSE 8000
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \
  CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=2)"]
CMD ["python", "server.py", "http"]

MCP_HOST=0.0.0.0 is set in the image, so the same server.py still binds 127.0.0.1 when you run it on your laptop. The Promptise MCP server answers GET /health for probes. allowed_hosts turns on Host header validation for a public bind, which protects against DNS rebinding. This server has no authentication because it is only reachable on the compose network. If anything else can reach it, add API keys or OAuth as shown in MCP server authentication.


[09]

Step 5: Run both with docker compose

YAMLcompose.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
services:
  shop-mcp:
    build: ./shop-mcp
    # No ports: the MCP server is reachable only on the compose network.
    restart: unless-stopped

  agent:
    build: ./agent
    ports:
      - "8080:8080"
    environment:
      OPENAI_API_KEY: ${OPENAI_API_KEY:?set OPENAI_API_KEY}
      PROMPTISE_SERVE_TOKEN: ${PROMPTISE_SERVE_TOKEN:?set PROMPTISE_SERVE_TOKEN}
      SHOP_MCP_URL: http://shop-mcp:8000/mcp
    depends_on:
      shop-mcp:
        condition: service_healthy   # the agent connects to MCP when it starts
    stop_grace_period: 40s           # longer than [prod] shutdown_timeout
    restart: unless-stopped
  • No ports on shop-mcp. Only containers on the compose network can reach the tools.

  • ${OPENAI_API_KEY:?...} takes the value from your shell and stops compose with a clear message when it is missing.

  • depends_on with service_healthy waits for the MCP server's health check, because the agent connects to it while it starts. Without it, the agent can start first and fail to connect.

  • stop_grace_period: 40s gives the agent more time than its 30-second drain before Docker sends SIGKILL. Docker's default is 10 seconds, which is shorter than many agent requests.

>_Terminal
export OPENAI_API_KEY=sk-...
export PROMPTISE_SERVE_TOKEN="$(openssl rand -hex 32)"
docker compose up -d --build
docker compose ps
Output
SERVICE    STATUS                    PORTS
agent      Up 5 seconds (healthy)    0.0.0.0:8080->8080/tcp
shop-mcp   Up 11 seconds (healthy)   8000/tcp

[10]

Step 6: Check health, readiness and the API

>_Terminal
curl -s localhost:8080/healthz
curl -s localhost:8080/readyz
curl -s -o /dev/null -w "%{http_code}\n" localhost:8080/v1/info
Output
$ curl -s localhost:8080/healthz
{"status":"ok"}
$ curl -s localhost:8080/readyz
{"status":"ready"}
$ curl -s -o /dev/null -w "%{http_code}\n" localhost:8080/v1/info
401

The probes need no token, so load balancers and Kubernetes can call them. Everything under /v1/ needs the bearer token, so /v1/info without it returns 401. Now ask the agent a question:

>_Terminal
curl -s localhost:8080/v1/invoke \
  -H "Authorization: Bearer $PROMPTISE_SERVE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"input": "Where is my order A1002?"}'
Output
{
    "output": "Your order A1002 is currently in transit with DHL. The estimated delivery was 2026-10-21, so it appears delayed \u2014 sorry about that.\n\nWould you like me to open a support ticket to investigate or try contacting DHL? If you have the DHL tracking number, paste it here and I\u2019ll check further.",
    "duration_ms": 6867.7,
    "usage": {
        "llm_calls": 2,
        "prompt_tokens": 468,
        "completion_tokens": 421,
        "total_tokens": 889
    },
    "request_id": "03353a5728a047dca3bf593f7b851938"
}

The agent called get_order on the MCP container and answered. usage counts the model calls and tokens of this request, which is what per-user cost tracking needs (see serve mode for every field). request_id also appears in the X-Request-ID response header and in every log line of the request, so you can find a bad answer in the logs.


[11]

Step 7: Shut down without dropping requests

Start a request, and stop the container while it runs:

>_Terminal
curl -s localhost:8080/v1/invoke -H "Authorization: Bearer $PROMPTISE_SERVE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"input": "Is TENT-2P in stock, and what about order A1001?"}' &
sleep 1.5; docker compose stop agent; wait
Output
{"output":"TENT-2P: 4 units in stock.\n\nOrder A1001: delivered via UPS on 2026-10-12.\n\nWould you like to reserve a unit or need anything else?","duration_ms":5781.4,"usage":{"llm_calls":2,"prompt_tokens":540,"completion_tokens":366,"total_tokens":906},"request_id":"cf3bd9923ccd4bbe9d9e3cc7af4d9a22"}exit=0

The request that was running when SIGTERM arrived still got its full answer. The JSON logs show the order of events:

>_Terminal
docker compose logs --no-log-prefix agent | jq -c 'select(.msg == "Received SIGTERM: draining"
  or (.msg == "http" and .path == "/v1/invoke") or .msg == "run.exit" or .msg == "stats")
  | {msg, path, status, duration_ms, code, cost_usd: .totals.cost_usd, total_tokens: .totals.total_tokens}
  | del(..|nulls)'
Output
{"msg":"http","path":"/v1/invoke","status":200,"duration_ms":6868.2}
{"msg":"Received SIGTERM: draining"}
{"msg":"http","path":"/v1/invoke","status":200,"duration_ms":5781.7}
{"msg":"run.exit","code":0}
{"msg":"stats","cost_usd":0.001826,"total_tokens":1795}

On SIGTERM the server starts draining: /readyz and new /v1/* requests answer 503, so the load balancer stops sending traffic, and running requests get up to [prod] shutdown_timeout seconds. Then the agent closes its MCP sessions and exits with code 0. The last line carries the process's totals, including the estimated cost from [pricing].


[12]

The complete example

All files are shown above: agent/agent.py, agent/promptise.toml, agent/Dockerfile, agent/.dockerignore, agent/requirements.txt (one line, promptise==1.10.0), shop-mcp/server.py (the two-tool shop server, with the run() call from Step 4), shop-mcp/Dockerfile and compose.yaml. To run the stack:

>_Terminal
cd support-bot
export OPENAI_API_KEY=sk-...
export PROMPTISE_SERVE_TOKEN="$(openssl rand -hex 32)"
docker compose up -d --build
curl -s localhost:8080/v1/invoke -H "Authorization: Bearer $PROMPTISE_SERVE_TOKEN" \
  -H "Content-Type: application/json" -d '{"input": "Where is my order A1002?"}'
docker compose down

[13]

Running it in production

Probes and shutdown on Kubernetes

The same image runs on Kubernetes. Map the endpoints to probes and keep the grace period longer than the drain:

YAML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spec:
  terminationGracePeriodSeconds: 40
  containers:
    - name: agent
      image: registry.example.com/support-bot-agent:1.0.0
      ports: [{containerPort: 8080}]
      env:
        - name: SHOP_MCP_URL
          value: http://shop-mcp:8000/mcp
        - name: OPENAI_API_KEY
          valueFrom: {secretKeyRef: {name: support-bot, key: openai-api-key}}
        - name: PROMPTISE_SERVE_TOKEN
          valueFrom: {secretKeyRef: {name: support-bot, key: serve-token}}
      livenessProbe:
        httpGet: {path: /healthz, port: 8080}
        periodSeconds: 10
      readinessProbe:
        httpGet: {path: /readyz, port: 8080}
        periodSeconds: 5

Don't point the liveness probe at /readyz: a pod that is draining would be killed instead of finishing its requests.

Scaling

One promptise run --serve process serves one agent and runs up to max_concurrency requests at the same time. Most of a request's time is spent waiting on the model API, so a process handles several requests at once with little CPU. Scale out by adding replicas behind a load balancer, and scale on concurrent requests or latency rather than CPU. Two rules keep replicas interchangeable. Keep conversations in a shared store (RedisConversationStore or PostgresConversationStore with a session_id per conversation), not in memory. And remember that your model provider's rate limit is shared by every replica, so more replicas can mean more 429s from the provider.

MCP servers over Streamable HTTP keep a session per client connection. When an MCP container restarts, the server answers old sessions with 404, and Promptise clients open a new session and retry the call once. For MCP servers that run several replicas, use sticky sessions at the load balancer, or check that your server handles requests without per-session state.

Logs, metrics and traces

--prod writes one JSON object per line on stderr, which Docker's logging drivers, Loki, Datadog and CloudWatch ingest as is. Raise [prod] observe to standard to log every model call and tool call, or use the OpenTelemetry transporter to send each request as a trace. AI agent observability covers what to record and what to keep out of logs.

Image hygiene

Pin the base image and your dependencies, and rebuild regularly for security patches. Scan images in CI (docker scout, Trivy or Grype). Run as a non-root user, as here, and set the root filesystem read-only where your platform allows it. Agents that execute generated code need a separate sandbox, not just a container: see AI agent code execution sandbox.

Put a gateway in front

The serve mode has one bearer token for all callers. It is meant for one application calling the agent, not for end users. Put TLS, user authentication and per-user rate limits in your API gateway or web app, and pass the user's identity on to the agent. To give each user their own identity and permissions inside the agent, embed build_agent() in your own web server instead.


[14]

Common problems and fixes

Symptom

Cause

Fix

refusing to use this manifest: owned by uid 1000, not by you (uid 10001) or root

The files belong to a user other than root or the one that runs the agent

Copy them with a plain COPY, or --chown them to the user in USER

preflight failed ... missing required environment variable(s) and exit code 2

A variable in [env] required is not set in the container

Pass it with -e, environment: or a secret reference

refusing to serve on 0.0.0.0 without an auth token

Network bind without PROMPTISE_SERVE_TOKEN

Set the token from your secret store

Failed to connect to server 'shop' at start

The agent started before the MCP server was ready

depends_on with condition: service_healthy, and a health check on the MCP server

The MCP server answers 421

The Host header isn't in allowed_hosts

Add the service name, for example "shop-mcp:*"

docker stop takes 10 seconds and requests fail

Shell-form CMD, so SIGTERM doesn't reach the agent and Docker kills it after the timeout

Use exec form CMD ["promptise", "run", ...]

Long requests are cut off on deploy

The grace period is shorter than the drain or the request

Raise stop_grace_period / terminationGracePeriodSeconds above [prod] shutdown_timeout

The container uses an old or wrong key

A .env file was copied into the image

.dockerignore with .env, and PROMPTISE_NO_DOTENV=1

Many 429 busy responses

More concurrent requests than [serve] max_concurrency

Add replicas, or raise the limit if the provider's rate limit allows it


[15]

Limitations

promptise run --serve runs one agent in one process with one bearer token. It has no per-user accounts, TLS or routing, which stay with your gateway and platform. Conversations are only kept across requests when the agent has a conversation store. The image in this guide is a plain single-stage build. A multi-stage build or a distroless base can make it smaller, at the cost of a less convenient debugging shell. Kubernetes manifests are shown as a starting point, not as a tested chart.


[16]

Frequently asked questions

How do I run an AI agent in a Docker container?

Install your agent framework in a slim Python image, copy the agent code, and start it as an HTTP service in exec form. With Promptise Foundry, the command is promptise run --prod --serve --host 0.0.0.0, which serves the agent factory named in promptise.toml with health checks, an invoke endpoint and JSON logs.

How should I pass the API key to a Dockerized agent?

As an environment variable set when the container starts, from your platform's secret store. Never write it into the Dockerfile with ENV or ARG, and keep .env files out of the image with .dockerignore. Promptise reads provider keys such as OPENAI_API_KEY from the environment.

What health checks does an AI agent container need?

Two. A liveness check that only confirms the process responds (/healthz), and a readiness check that confirms the agent is built and its tool servers are connected (/readyz). Route traffic on readiness and restart on liveness, so a draining or starting container is not killed.

How do I stop an agent container without losing requests?

Make the agent process 1 with an exec-form CMD so it receives SIGTERM, let it stop accepting new requests and finish running ones, and give it a grace period longer than your slowest request. promptise run --prod --serve drains for [prod] shutdown_timeout seconds and exits with code 0.

Should the MCP server run in the same container as the agent?

Usually not. A separate container lets you deploy, restrict and scale the tools on their own and share them between agents. A stdio MCP server, which the agent starts as a child process, can live in the agent's image when only that agent uses it.

How do I scale an AI agent with Docker?

Run more replicas behind a load balancer. Each replica is stateless if conversations live in a shared store such as Redis or Postgres. Scale on concurrent requests or latency, since agents mostly wait on the model API, and watch the provider's rate limit, which all replicas share.


[17]

Next steps

  • Run, debug and serve the same project locally: Run, debug and deploy an AI agent.

  • Agents that run on their own with triggers and budgets: Deploy AI agents in production.

  • Harden the tool server: Build a production MCP server in Python.

  • Survive crashes mid-run: Durable AI agents and crash recovery.

  • Reference: promptise run serve mode and Docker and MCP server deployment.

← PreviousContext Compaction for AI Agents: Trim vs SummarizeNext lesson →LLM Tool Rate Limiting: Per-Client Limits for MCP Servers