How to Deploy an AI Agent with Docker
Updated 2026-11-10
Containerizing an agent packages Python, dependencies, and your entry script so the same image runs on a laptop, a DigitalOcean droplet, or any host with Docker. This guide targets long-running bots (Discord, Slack Socket Mode, scheduled jobs) built from Build your first AI agent in Python or Connect an AI agent to Discord. Last tested 2026-11-10.
Prerequisites
- Docker Engine 24+ installed locally (Install Docker)
- An agent script that runs with
python your_agent.pyand reads secrets from environment variables - API keys and bot tokens available as env vars (never bake them into the image)
Docs used:
Steps
1. Pin dependencies
pip install pip-tools
pip-compile requirements.in -o requirements.txt
Example requirements.in:
openai>=1.40
discord.py>=2.4
requests>=2.32
Commit requirements.txt. Do not commit .env.
2. Write a multi-stage Dockerfile
# Dockerfile
FROM python:3.12-slim AS base
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
FROM base AS deps
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
FROM base AS runtime
COPY --from=deps /usr/local /usr/local
COPY agent.py discord_agent.py ./
# optional: non-root user
RUN useradd -m appuser && chown -R appuser:appuser /app
USER appuser
CMD ["python", "discord_agent.py"]
Official guidance recommends slim base images and a single WORKDIR per the Dockerfile reference.
3. Provide secrets with an env file
# .env (local only, gitignored)
DISCORD_TOKEN=...
OPENAI_API_KEY=sk-...
MODEL=gpt-4.1-mini
docker build -t my-agent .
docker run --env-file .env --restart unless-stopped my-agent
--restart unless-stopped keeps the bot running across host reboots, matching the always-on pattern in Build an AI agent that runs on a schedule.
4. Add compose for one-command deploy
# compose.yaml
services:
agent:
build: .
env_file: .env
restart: unless-stopped
docker compose up -d --build
docker compose logs -f agent
The compose file reference documents env_file and restart.
5. Optional health check
Long-running bots may not expose HTTP. A lightweight check can verify the process is alive:
HEALTHCHECK --interval=60s --timeout=5s --retries=3 \
CMD pgrep -f discord_agent.py || exit 1
For HTTP agents, point HEALTHCHECK at your /health route instead.
6. Ship to a VPS
- Install Docker on the server.
git cloneyour repo or copy the image from a registry.- Place
.envon the server withchmod 600. - Run
docker compose up -d.
Combine with Connect an AI agent to Discord or Connect an AI agent to Slack for always-on chat bots.
Common errors
ModuleNotFoundError in the container. The dependency is missing from requirements.txt. Rebuild after pip-compile.
Bot exits immediately. Missing env vars. Docker does not load shell profiles; pass --env-file or compose environment.
Image is huge. Use python:3.12-slim, multi-stage copy, and avoid pip install as root without cleaning apt caches.
Permission errors on volumes. Match USER in the Dockerfile with volume ownership, or run without a bind mount for simple bots.
Secrets leaked in image layers. Never ENV OPENAI_API_KEY=... in the Dockerfile. Use runtime env files or your host’s secret manager.
FAQ
How do I deploy an AI agent with Docker?
Build a slim image that installs pinned requirements, copies your agent script, and starts it with CMD. Inject API keys with --env-file or compose env_file, and set restart: unless-stopped.
Do MCP servers belong in the same container?
Keep MCP stdio servers separate unless you fully control startup order. Remote HTTP MCP (Gmail, GitHub, Slack) needs no sidecar. Local stdio MCP fits a second compose service or the pattern in Connect an AI agent to an MCP server.
Docker vs cron on the host?
Cron suits batch agents once per hour. Docker suits always-on bots and gives you identical dev/prod dependencies.
How does this relate to Railway or Vercel?
Docker is portable across VPS providers. Managed platforms often accept a Dockerfile directly; this guide stays provider-agnostic.