Securing Docker containers running AI tools (such as LiteLLM, Ollama, LocalAI, or custom inference runtimes) requires a defense-in-depth approach spanning container hardening, network isolation, credential management, and resource governance.
1. Enforce Container Hardening & Least Privilege
- Run as a Non-Root User: Never allow AI containers to execute as root inside the container. Specify a non-root UID/GID (e.g.,
user: "1000:1000") in your Docker Compose or Dockerfile. - Make the Root Filesystem Read-Only: Use
read_only: trueto prevent attackers or compromised AI applications from modifying system binaries or installing malware. Mount a dedicated, isolated/tmpvolume for transient model caching or scratch space. - Drop Linux Kernel Capabilities: Strip unnecessary privileges by adding
cap_drop: [ALL]. AI containers typically only need user-space execution. - Prevent Privilege Escalation: Set
security_opt: ["no-new-privileges:true"]to stop processes inside the container from gaining additional privileges viasetuidbinaries.
2. Isolate Network Access & API Interfaces
- Bind to Localhost (
127.0.0.1): Unless the AI tool must be directly reachable across the local network, explicitly bind container ports to127.0.0.1:PORTinstead of exposing0.0.0.0:PORT. - Put Services Behind an Authenticated Reverse Proxy: Route public or team access through Nginx, Traefik, or Caddy configured with TLS/SSL, robust authentication (OAuth2/OIDC), and rate limiting.
- Restrict Outbound Egress: Use Docker custom networks or host firewall rules (UFW/iptables) to limit outbound internet access. If your AI tool processes sensitive data locally using local models (e.g., Ollama), block all outbound internet traffic for that container.
3. Implement Strict Secret Management & Access Controls
- Never Hardcode API Keys: Store external model keys (OpenAI, Anthropic, HuggingFace tokens) in environment variables loaded via
.envfiles (secured withchmod 600) or dedicated secret managers (Vault, Docker Secrets). - Enforce Virtual Keys and Spend Caps: If using AI proxies like LiteLLM, generate individual virtual API keys for each developer or team rather than sharing the master key. Assign daily/monthly financial budgets and rate limits per key.
- Redact Sensitive Data (PII/Credentials): Deploy guardrail middleware (such as Microsoft Presidio or custom regex filters) to automatically scrub PII, API tokens, and internal database connection strings before requests reach LLM providers.
4. Apply Resource Limits & Host Protection
- Set CPU and RAM Constraints: Heavy LLM workloads can quickly starve host system resources. Set explicit limits in Docker Compose to prevent Denial of Service (DoS):
YAMLdeploy: resources: limits: cpus: '2.0' memory: 4096M - Restrict GPU Access (If Applicable): When exposing NVIDIA GPUs to containers (
nvidia-docker), pass only specific GPU IDs to dedicated containers rather than granting full access across all hardware. - Monitor Metrics: Scrape container resource utilization (via cAdvisor + Prometheus) to detect abnormal compute spikes or unauthorized long-running batch processes.
5. Secure Image Supply Chain & Updates
- Use Official & Minimal Base Images: Use minimal base images (e.g.,
Alpine,Distroless, or official vendor images likeghcr.io/berriai/litellm). Avoid unverified community images from public registries. - Pin Specific Version Tags: Never use the
:latestimage tag in production or staging environments. Pin explicit version tags (e.g.,v1.59.0) or image digest hashes (@sha256:...) to prevent untested upstream changes. - Vulnerability Scanning: Scan container images for CVEs using tools like Trivy, Grype, or Docker Scout before deploying them.