|
| 1 | +--- |
| 2 | +title: Build and run agentic AI applications with Docker |
| 3 | +linktitle: Agentic AI applications |
| 4 | +keywords: AI, Docker, Model Runner, MCP Toolkit, Docker Offload, AI agents, application development |
| 5 | +summary: | |
| 6 | + Learn how to create AI agent applications using Docker Model Runner, MCP Toolkit, and Docker Offload. |
| 7 | +params: |
| 8 | + tags: [AI] |
| 9 | + time: 30 minutes |
| 10 | +--- |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +Agentic applications are transforming how software gets built. These apps don't |
| 15 | +just respond, they decide, plan, and act. They're powered by models, |
| 16 | +orchestrated by agents, and integrated with APIs, tools, and services in real |
| 17 | +time. |
| 18 | + |
| 19 | +All these new agentic applications, no matter what they do, share a common |
| 20 | +architecture. It's a new kind of stack, built from three core components: |
| 21 | + |
| 22 | +- Models: These are your GPTs, CodeLlamas, Mistrals. They're doing the |
| 23 | + reasoning, writing, and planning. They're the engine behind the intelligence. |
| 24 | + |
| 25 | +- Agent: This is where the logic lives. Agents take a goal, break it down, and |
| 26 | + figure out how to get it done. They orchestrate everything. They talk to the |
| 27 | + UI, the tools, the model, and the gateway. |
| 28 | + |
| 29 | +- MCP gateway: This is what links your agents to the outside world, including |
| 30 | + APIs, tools, and services. It provides a standard way for agents to call |
| 31 | + capabilities via the Model Context Protocol (MCP). |
| 32 | + |
| 33 | +Docker makes this AI-powered stack simpler, faster, and more secure by unifying |
| 34 | +models, tool gateways, and cloud infrastructure into a developer-friendly |
| 35 | +workflow that uses Docker Compose. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +This guide walks you through the core components of agentic development and |
| 40 | +shows how Docker ties them all together with the following tools: |
| 41 | + |
| 42 | +- [Docker Model Runner](../manuals/ai/model-runner/_index.md) lets you run LLMs |
| 43 | + locally with simple command and OpenAI-compatible APIs. |
| 44 | +- [Docker MCP Catalog and |
| 45 | + Toolkit](../manuals/ai/mcp-catalog-and-toolkit/_index.md) helps you discover |
| 46 | + and securely run external tools, like APIs and databases, using the Model |
| 47 | + Context Protocol (MCP). |
| 48 | +- [Docker MCP Gateway](/ai/mcp-gateway/) lets you orchestrate and manage MCP servers. |
| 49 | +- [Docker Offload](/offload/) provides a powerful, GPU-accelerated |
| 50 | + environment to run your AI applications with the same Compose-based |
| 51 | + workflow you use locally. |
| 52 | +- [Docker Compose](/manuals/ai/compose/models-and-compose.md) is the tool that ties it all |
| 53 | + together, letting you define and run multi-container applications with a |
| 54 | + single file. |
| 55 | + |
| 56 | +For this guide, you'll start by running the app in Docker Offload, using the |
| 57 | +same Compose workflow you're already familiar with. Then, if your machine |
| 58 | +hardware supports it, you'll run the same app locally using the same workflow. |
| 59 | +Finally, you'll dig into the Compose file, Dockerfile, and app to see how it all |
| 60 | +works together. |
| 61 | + |
| 62 | +## Prerequisites |
| 63 | + |
| 64 | +To follow this guide, you need to: |
| 65 | + |
| 66 | + - [Install Docker Desktop 4.43 or later](../get-started/get-docker.md) |
| 67 | + - [Enable Docker Model Runner](/manuals/ai/model-runner.md#enable-dmr-in-docker-desktop) |
| 68 | + - [Join Docker Offload Beta](/offload/quickstart/) |
| 69 | + |
| 70 | +## Step 1: Clone the sample application |
| 71 | + |
| 72 | +You'll use an existing sample application that demonstrates how to connect a |
| 73 | +model to an external tool using Docker's AI features. |
| 74 | + |
| 75 | +```console |
| 76 | +$ git clone https://github.com/docker/compose-for-agents.git |
| 77 | +$ cd compose-for-agents/adk/ |
| 78 | +``` |
| 79 | + |
| 80 | +## Step 2: Run the application with Docker Offload |
| 81 | + |
| 82 | +You'll start by running the application in Docker Offload, which provides a |
| 83 | +managed environment for running AI workloads. This is ideal if you want to |
| 84 | +leverage cloud resources or if your local machine doesn't meet the hardware |
| 85 | +requirements to run the model locally. Docker Offload includes support for |
| 86 | +GPU-accelerated instances, making it ideal for compute-intensive workloads like |
| 87 | +AI model inference. |
| 88 | + |
| 89 | +To run the application with Docker Offload, follow these steps: |
| 90 | + |
| 91 | +1. Sign in to the Docker Desktop Dashboard. |
| 92 | +2. In a terminal, start Docker Offload by running the following command: |
| 93 | + |
| 94 | + ```console |
| 95 | + $ docker offload start |
| 96 | + ``` |
| 97 | + |
| 98 | + When prompted, choose the account you want to use for Docker Offload and select |
| 99 | + **Yes** when prompted **Do you need GPU support?**. |
| 100 | + |
| 101 | +3. In the `adk/` directory of the cloned repository, run the following command |
| 102 | + in a terminal to build and run the application: |
| 103 | + |
| 104 | + ```console |
| 105 | + $ docker compose up |
| 106 | + ``` |
| 107 | + |
| 108 | + The first time you run this command, Docker pulls the model from Docker Hub, |
| 109 | + which may take some time. |
| 110 | + |
| 111 | + The application is now running with Docker Offload. Note that the Compose workflow |
| 112 | + is the same when using Docker Offload as it is locally. You define your |
| 113 | + application in a `compose.yaml` file, and then use `docker compose up` to build |
| 114 | + and run it. |
| 115 | + |
| 116 | +4. Visit [http://localhost:8080](http://localhost:8080). Enter a correct or |
| 117 | + incorrect fact in the prompt and hit enter. An agent searches DuckDuckGo to |
| 118 | + verify it and another agent revises the output. |
| 119 | + |
| 120 | +  |
| 121 | + |
| 122 | +5. Press ctrl-c in the terminal to stop the application when you're done. |
| 123 | + |
| 124 | +6. Run the following command to stop Docker Offload: |
| 125 | + |
| 126 | + ```console |
| 127 | + $ docker offload stop |
| 128 | + ``` |
| 129 | + |
| 130 | +## Step 3: Optional. Run the application locally |
| 131 | + |
| 132 | +If your machine meets the necessary hardware requirements, you can run the |
| 133 | +entire application stack locally using Docker Compose. This lets you test the |
| 134 | +application end-to-end, including the model and MCP gateway, without needing to |
| 135 | +run in the cloud. This particular example uses the [Gemma 3 4B |
| 136 | +model](https://hub.docker.com/r/ai/gemma3) with a context size of `10000`. |
| 137 | + |
| 138 | +Hardware requirements: |
| 139 | + - VRAM: 3.5 GB |
| 140 | + - Storage: 2.31 GB |
| 141 | + |
| 142 | +If your machine exceeds those requirements, consider running the application with a larger |
| 143 | +context size or a larger model to improve the agents performance. You can easily |
| 144 | +update model and context size in the `compose.yaml` file. |
| 145 | + |
| 146 | +To run the application locally, follow these steps: |
| 147 | + |
| 148 | +1. In the `adk/` directory of the cloned repository, run the following command in a |
| 149 | + terminal to build and run the application: |
| 150 | + |
| 151 | + ```console |
| 152 | + $ docker compose up |
| 153 | + ``` |
| 154 | + |
| 155 | + The first time you run this command, Docker pulls the |
| 156 | + model from Docker Hub, which may take some time. |
| 157 | + |
| 158 | +2. Visit [http://localhost:8080](http://localhost:8080). Enter a correct or |
| 159 | + incorrect fact in the prompt and hit enter. An agent searches DuckDuckGo to |
| 160 | + verify it and another agent revises the output. |
| 161 | + |
| 162 | +3. Press ctrl-c in the terminal to stop the application when you're done. |
| 163 | + |
| 164 | +## Step 4: Review the application environment |
| 165 | + |
| 166 | +You can find the `compose.yaml` file in the `adk/` directory. Open it in a text |
| 167 | +editor to see how the services are defined. |
| 168 | + |
| 169 | +```yaml {collapse=true,title=compose.yaml} |
| 170 | +services: |
| 171 | + adk: |
| 172 | + build: |
| 173 | + context: . |
| 174 | + ports: |
| 175 | + # expose port for web interface |
| 176 | + - "8080:8080" |
| 177 | + environment: |
| 178 | + # point adk at the MCP gateway |
| 179 | + - MCPGATEWAY_ENDPOINT=http://mcp-gateway:8811/sse |
| 180 | + depends_on: |
| 181 | + - mcp-gateway |
| 182 | + models: |
| 183 | + gemma3 : |
| 184 | + endpoint_var: MODEL_RUNNER_URL |
| 185 | + model_var: MODEL_RUNNER_MODEL |
| 186 | + |
| 187 | + mcp-gateway: |
| 188 | + # mcp-gateway secures your MCP servers |
| 189 | + image: docker/mcp-gateway:latest |
| 190 | + use_api_socket: true |
| 191 | + command: |
| 192 | + - --transport=sse |
| 193 | + # add any MCP servers you want to use |
| 194 | + - --servers=duckduckgo |
| 195 | + |
| 196 | +models: |
| 197 | + gemma3: |
| 198 | + # pre-pull the model when starting Docker Model Runner |
| 199 | + model: ai/gemma3:4B-Q4_0 |
| 200 | + context_size: 10000 # 3.5 GB VRAM |
| 201 | + # increase context size to handle search results |
| 202 | + # context_size: 131000 # 7.6 GB VRAM |
| 203 | +``` |
| 204 | + |
| 205 | +The app consists of three main components: |
| 206 | + |
| 207 | + - The `adk` service, which is the web application that runs the agentic AI |
| 208 | + application. This service talks to the MCP gateway and model. |
| 209 | + - The `mcp-gateway` service, which is the MCP gateway that connects the app |
| 210 | + to external tools and services. |
| 211 | + - The `models` block, which defines the model to use with the application. |
| 212 | + |
| 213 | +When you examine the `compose.yaml` file, you'll notice two notable elements for the model: |
| 214 | + |
| 215 | + - A service‑level `models` block in the `adk` service |
| 216 | + - A top-level `models` block |
| 217 | + |
| 218 | +These two blocks together let Docker Compose automatically start and connect |
| 219 | +your ADK web app to the specified LLM. |
| 220 | + |
| 221 | +> [!TIP] |
| 222 | +> |
| 223 | +> Looking for more models to use? Check out the [Docker AI Model |
| 224 | +> Catalog](https://hub.docker.com/catalogs/models/). |
| 225 | +
|
| 226 | +When examining the `compose.yaml` file, you'll notice the gateway service is a |
| 227 | +Docker-maintained image, |
| 228 | +[`docker/mcp-gateway:latest`](https://hub.docker.com/r/docker/agents_gateway). |
| 229 | +This image is Docker's open source [MCP |
| 230 | +Gateway](https://github.com/docker/docker-mcp/) that enables your application to |
| 231 | +connect to MCP servers, which expose tools that models can call. In this |
| 232 | +example, it uses the [`duckduckgo` MCP |
| 233 | +server](https://hub.docker.com/mcp/server/duckduckgo/overview) to perform web |
| 234 | +searches. |
| 235 | + |
| 236 | +> [!TIP] |
| 237 | +> |
| 238 | +> Looking for more MCP servers to use? Check out the [Docker MCP |
| 239 | +> Catalog](https://hub.docker.com/catalogs/mcp/). |
| 240 | +
|
| 241 | +With only a few lines of instructions in a Compose file, you're able to run and |
| 242 | +connect all the necessary services of an agentic AI application. |
| 243 | + |
| 244 | +In addition to the Compose file, the Dockerfile and the |
| 245 | +`entrypoint.sh` script it creates, play a role in wiring up the AI stack at build and |
| 246 | +runtime. You can find the `Dockerfile` in the `adk/` directory. Open it in a |
| 247 | +text editor. |
| 248 | + |
| 249 | +```dockerfile {collapse=true,title=Dockerfile} |
| 250 | +# Use Python 3.11 slim image as base |
| 251 | +FROM python:3.13-slim |
| 252 | +ENV PYTHONUNBUFFERED=1 |
| 253 | + |
| 254 | +RUN pip install uv |
| 255 | + |
| 256 | +WORKDIR /app |
| 257 | +# Install system dependencies |
| 258 | +COPY pyproject.toml uv.lock ./ |
| 259 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 260 | + UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy \ |
| 261 | + uv pip install --system . |
| 262 | +# Copy application code |
| 263 | +COPY agents/ ./agents/ |
| 264 | +RUN python -m compileall -q . |
| 265 | + |
| 266 | +COPY <<EOF /entrypoint.sh |
| 267 | +#!/bin/sh |
| 268 | +set -e |
| 269 | + |
| 270 | +if test -f /run/secrets/openai-api-key; then |
| 271 | + export OPENAI_API_KEY=$(cat /run/secrets/openai-api-key) |
| 272 | +fi |
| 273 | + |
| 274 | +if test -n "\${OPENAI_API_KEY}"; then |
| 275 | + echo "Using OpenAI with \${OPENAI_MODEL_NAME}" |
| 276 | +else |
| 277 | + echo "Using Docker Model Runner with \${MODEL_RUNNER_MODEL}" |
| 278 | + export OPENAI_BASE_URL=\${MODEL_RUNNER_URL} |
| 279 | + export OPENAI_MODEL_NAME=openai/\${MODEL_RUNNER_MODEL} |
| 280 | + export OPENAI_API_KEY=cannot_be_empty |
| 281 | +fi |
| 282 | +exec adk web --host 0.0.0.0 --port 8080 --log_level DEBUG |
| 283 | +EOF |
| 284 | +RUN chmod +x /entrypoint.sh |
| 285 | + |
| 286 | +# Create non-root user |
| 287 | +RUN useradd --create-home --shell /bin/bash app \ |
| 288 | + && chown -R app:app /app |
| 289 | +USER app |
| 290 | + |
| 291 | +ENTRYPOINT [ "/entrypoint.sh" ] |
| 292 | +``` |
| 293 | + |
| 294 | +The `entrypoint.sh` has five key environment variables: |
| 295 | + |
| 296 | +- `MODEL_RUNNER_URL`: Injected by Compose (via the service-level `models:` |
| 297 | + block) to point at your Docker Model Runner HTTP endpoint. |
| 298 | + |
| 299 | +- `MODEL_RUNNER_MODEL`: Injected by Compose to select which model to launch in |
| 300 | + Model Runner. |
| 301 | + |
| 302 | +- `OPENAI_API_KEY`: If you define an `openai-api-key` secret in your Compose |
| 303 | + file, Compose will mount it at `/run/secrets/openai-api-key`. The entrypoint |
| 304 | + script reads that file and exports it as `OPENAI_API_KEY`, causing the app to |
| 305 | + use hosted OpenAI instead of Model Runner. |
| 306 | + |
| 307 | +- `OPENAI_BASE_URL`: When no real key is present, this is set to |
| 308 | + `MODEL_RUNNER_URL` so the ADK's OpenAI-compatible client sends requests to |
| 309 | + Docker Model Runner. |
| 310 | + |
| 311 | +- `OPENAI_MODEL_NAME`: When falling back to Model Runner, the model is prefixed |
| 312 | + with `openai/` so the client picks up the right model alias. |
| 313 | + |
| 314 | +Together, these variables let the same ADK web server code seamlessly target either: |
| 315 | + |
| 316 | +- Hosted OpenAI: if you supply `OPENAI_API_KEY` (and optionally `OPENAI_MODEL_NAME`) |
| 317 | +- Model Runner: by remapping `MODEL_RUNNER_URL` and `MODEL_RUNNER_MODEL` into the OpenAI client’s expected variables |
| 318 | + |
| 319 | +## Step 5: Review the application |
| 320 | + |
| 321 | +The `adk` web application is an agent implementation that connects to the MCP |
| 322 | +gateway and a model through environment variables and API calls. It uses the |
| 323 | +[ADK (Agent Development Kit)](https://github.com/google/adk-python) to define a |
| 324 | +root agent named Auditor, which coordinates two sub-agents, Critic and Reviser, |
| 325 | +to verify and refine model-generated answers. |
| 326 | + |
| 327 | +The three agents are: |
| 328 | + |
| 329 | +- Critic: Verifies factual claims using the toolset, such as DuckDuckGo. |
| 330 | +- Reviser: Edits answers based on the verification verdicts provided by the Critic. |
| 331 | +- Auditor: A higher-level agent that sequences the |
| 332 | + Critic and Reviser. It acts as the entry point, evaluating LLM-generated |
| 333 | + answers, verifying them, and refining the final output. |
| 334 | + |
| 335 | +All of the application's behavior is defined in Python under the `agents/` |
| 336 | +directory. Here's a breakdown of the notable files: |
| 337 | + |
| 338 | +- `agents/agent.py`: Defines the Auditor, a SequentialAgent that chains together |
| 339 | + the Critic and Reviser agents. This agent is the main entry point of the |
| 340 | + application and is responsible for auditing LLM-generated content using |
| 341 | + real-world verification tools. |
| 342 | + |
| 343 | +- `agents/sub_agents/critic/agent.py`: Defines the Critic agent. It loads the |
| 344 | + language model (via Docker Model Runner), sets the agent’s name and behavior, |
| 345 | + and connects to MCP tools (like DuckDuckGo). |
| 346 | + |
| 347 | +- `agents/sub_agents/critic/prompt.py`: Contains the Critic prompt, which |
| 348 | + instructs the agent to extract and verify claims using external tools. |
| 349 | + |
| 350 | +- `agents/sub_agents/critic/tools.py`: Defines the MCP toolset configuration, |
| 351 | + including parsing `mcp/` strings, creating tool connections, and handling MCP |
| 352 | + gateway communication. |
| 353 | + |
| 354 | +- `agents/sub_agents/reviser/agent.py`: Defines the Reviser agent, which takes |
| 355 | + the Critic’s findings and minimally rewrites the original answer. It also |
| 356 | + includes callbacks to clean up the LLM output and ensure it's in the right |
| 357 | + format. |
| 358 | + |
| 359 | +- `agents/sub_agents/reviser/prompt.py`: Contains the Reviser prompt, which |
| 360 | + instructs the agent to revise the answer text based on the verified claim |
| 361 | + verdicts. |
| 362 | + |
| 363 | +The MCP gateway is configured via the `MCPGATEWAY_ENDPOINT` environment |
| 364 | +variable. In this case, `http://mcp-gateway:8811/sse`. This allows the app to |
| 365 | +use Server-Sent Events (SSE) to communicate with the MCP gateway container, |
| 366 | +which itself brokers access to external tool services like DuckDuckGo. |
| 367 | + |
| 368 | +## Summary |
| 369 | + |
| 370 | +Agent-based AI applications are emerging as a powerful new software |
| 371 | +architecture. In this guide, you explored a modular, chain-of-thought system |
| 372 | +where an Auditor agent coordinates the work of a Critic and a Reviser to |
| 373 | +fact-check and refine model-generated answers. This architecture shows how to |
| 374 | +combine local model inference with external tool integrations in a structured, |
| 375 | +modular way. |
| 376 | + |
| 377 | +You also saw how Docker simplifies this process by providing a suite of tools |
| 378 | +that support local and cloud-based agentic AI development: |
| 379 | + |
| 380 | +- [Docker Model Runner](../manuals/ai/model-runner/_index.md): Run and serve |
| 381 | + open-source models locally via OpenAI-compatible APIs. |
| 382 | +- [Docker MCP Catalog and |
| 383 | + Toolkit](../manuals/ai/mcp-catalog-and-toolkit/_index.md): Launch and manage |
| 384 | + tool integrations that follow the Model Context Protocol (MCP) standard. |
| 385 | +- [Docker MCP Gateway](/ai/mcp-gateway/): Orchestrate and manage |
| 386 | + MCP servers to connect agents to external tools and services. |
| 387 | +- [Docker Compose](/manuals/ai/compose/models-and-compose.md): Define and run |
| 388 | + multi-container agentic AI applications with a single file, using the same |
| 389 | + workflow locally and in the cloud. |
| 390 | +- [Docker Offload](/offload/): Run GPU-intensive AI workloads in a secure, managed |
| 391 | + cloud environment using the same Docker Compose workflow you use locally. |
| 392 | + |
| 393 | +With these tools, you can develop and test agentic AI applications efficiently, |
| 394 | +locally or in the cloud, using the same consistent workflow throughout. |
0 commit comments