Skip to main content
Containerize your agents when you need custom system dependencies, specific base images, or full control over the runtime environment. Unlike serverless agents that run on agent-workers, containerized agents are packaged as Docker images you build and deploy.

When to Use Containers

Use Containers when:
  • ✅ Need to customize agent code logic (xpander_handler.py)
  • ✅ Integrate MCP servers that run locally (not in cloud)
  • ✅ Need custom system dependencies (apt packages, libraries)
  • ✅ Require specific base images or Python versions
  • ✅ Want full control over runtime environment
  • ✅ Need custom tools, models, or integrations
Use Serverless (Workbench only) when:
  • ❌ Building agents with Workbench UI
  • ❌ Don’t need custom code logic
  • ❌ Standard connectors are sufficient
  • ❌ Don’t want to manage code or Docker builds
See Agent Serverless for the no-code deployment mode.

Real-World Example: MCP Integration

Here’s an example of a containerized agent that integrates Alpha Vantage MCP for financial market analysis:
xpander_handler.py
Why this needs containers:
  1. Custom MCP Integration: Connects to Alpha Vantage MCP server with custom authentication
  2. Custom Model Configuration: Uses OpenAI GPT-5.2 with specific reasoning effort settings
  3. Custom Error Handling: Implements specialized logging and error management
  4. Environment-Specific Logic: Requires ALPHAVANTAGE_API_KEY from environment
This level of customization requires code and containerization - it can’t be built with Workbench UI alone.

Quick Start

1

Install xpander CLI

2

Create New Agent Project

This creates:
  • xpander_handler.py - Agent code
  • requirements.txt - Python dependencies
  • Dockerfile - Container definition
  • .env - Environment variables
  • agent.yaml - Agent configuration
3

Setup Virtual Environment

4

Test Locally

Runs agent locally without containerization for fast iteration.
5

Test Container Locally

Verify your container builds and runs correctly before deploying.
6

Deploy Container

Builds Docker image, pushes to registry, and deploys to xpander platform.
7

View Logs

8

Invoke Agent

Or use the SDK, REST API, Slack, webhooks, or any integration.

Project Structure

After running xpander agent new, your project contains:

xpander_handler.py

xpander_handler.py

Dockerfile

Dockerfile

requirements.txt

requirements.txt

Customizing Your Container

Add System Dependencies

Edit Dockerfile to install OS-level packages:

Use Different Base Image

Add Private Dependencies


Local Development Workflow

1. Develop Without Container

This runs your agent code directly without containerization, making development faster.

2. Test Container Locally

3. Deploy to Production

The CLI:
  1. Builds your Docker image
  2. Pushes to container registry
  3. Deploys to xpander platform
  4. Makes agent available via API/SDK/Slack/webhooks

Environment Variables

Store sensitive configuration in .env file:
.env
These variables are:
  • Used during local development (xpander dev)
  • Loaded into container during local testing (docker run --env-file .env)
  • Securely injected during production deployment (xpander deploy)
Never commit .env to version control. Add it to .gitignore.

Invoking Containerized Agents

Once deployed, invoke your containerized agent the same way as serverless agents:

CLI Commands Reference


Container vs Serverless


Troubleshooting

Build Fails

Container Runs Locally But Fails in Production

  • Verify environment variables are set correctly
  • Check logs: xpander logs
  • Ensure base image architecture matches deployment target
  • Validate network access to external APIs

Large Image Size


Agent Serverless

Default deployment mode without containers

Self-Hosted Deployment

Run on your own infrastructure

CLI Reference

Complete CLI command reference