For the complete documentation index, see llms.txt. This page is also available as Markdown.

Containerizing AI Agents

How to secure and run AI agents in containers

Overview

AI agents, local LLMs, harnesses- you name it are typically run on your local system. Some folks have dedicated AI hardware like Nvidia's DGX Spark, and others run them on their computers — Apple Silicon Macs are especially popular right now! These AI services will inherit your level of access, which is usually admin, and as such get full access to your machine, your data, and your credentials. Maybe you're already aware of this and have taken steps to mitigate their access through your CLAUDE.md / Agents.md or configuration files. Great. But what if that doesn't always work? What if there's a bug? What if the AI goes rogue? You're essentially trying to restrict the AI by using itself. That's not a great approach.​

Enter containers. Containers can provide isolation benefits for the underlying OS, data, applications and their dependencies, networking, and more. This makes them an interesting platform to consider for hosting AI agents. If bugs happen, prompt injections occur, or an agent goes rogue, the scope of damage is far less than if it happened on your machine. Let's dive into containerizing our AI workloads.

* Containers can have access to host data and its filesystem depending on how it's deployed

This example shows how to containerize the Claude Code CLI but you can do other tools like Hermes, Cursor, etc.

Containerizing Claude Code

Creating the Dockerfile

When building a container, it's always best to use a secure, minimal image as a base and then build on top of it. Chainguard is a great platform for providing secure base images. While you need a subscription for much of what they offer, they provide a free secure distroless base image called wolfi. We'll use this in the Dockerfile example below.

For the latest example, check my GitHub repo.

Building the container

Running the container

Keeping it simple, this launches an interactive container where we'll be immediately dropped into our Claude Code CLI. You can explore other docker parameters if you want to pass API keys, mount directories, etc.

If instead you want to point Claude towards a model running elsewhere (like in LM Studio), simply set these environment variables during runtime. Alternatively, add them to your Dockerfile for a more permanent setup.

After a couple of Claude Code CLI popups, you'll be ready to go!

Last updated

Was this helpful?