DEV Community

Cover image for Agents at Your Fingertips: My Claude Code Setup with Docker Sandboxes
Kirill Byvshev
Kirill Byvshev

Posted on Originally published at byv.sh AI-assisted

Agents at Your Fingertips: My Claude Code Setup with Docker Sandboxes

In this article, I describe how my approach to working with AI agents changed and what setup I finally came to.

The implementation of these ideas is on GitHub: crionuke/sandboxer.

Why this way

Let me start with a short chain of thoughts and conclusions that explain why everything is set up this way and not otherwise.

I definitely do not want to be part of a process where I have to approve commands or tools before they run, because such approvals give no real control anyway, and it all slides into Enter → Enter → Enter... So what I need to control is not each step of the agent, but what is available to it at all. If a tool or an access exists, the agent must be able to use it; otherwise it simply should not exist. So, from a technical point of view, the agent must run in the privileged --dangerously-skip-permissions mode and in no other way.

But an agent with no limits on my machine is a risk to the host, the secrets and other projects. So everything must happen in an isolated environment, where the agent can do everything that is not explicitly forbidden.

Isolation also has a side benefit. If each agent has its own environment, I can work on different projects and tasks truly in parallel and switch between them when I need to, not when I can, because the agents do not compete for the same resources: ports, the Docker daemon and so on.

Since I do not approve the agent's steps, I must at least check the result of its work. The most convenient and familiar way to do this is command-line tools, plus text or graphical editors on my machine. So isolation must not hide the changes from the host: they must stay as easy to review as possible.

There can be many such environments, and they are mostly ephemeral. So the configuration cannot live inside them. Besides, I feel much more comfortable when all artifacts of my work are versioned and, so to speak, kept as Everything-as-Code. So all instructions, settings and so on must be in a repository, from which I can update them centrally and deliver them to the agents with no risk of mixing something up or losing it on the way.

How I came to the current setup

I tried 4 different setups, and all of them were built on technologies I was already used to: scripts, Docker containers and virtual machines. So I could quickly get and debug the configuration I needed, and each next setup fixed the problems of the previous one.

The first attempt, of course, was to run the agent locally. Making it even minimally secure turned out to be hard, and the result was rather fragile. I used the sandbox built into Claude Code itself and ran in auto mode. For this, I had to put together the right settings and permissions, taking into account the specifics and limits of this sandbox. The main one: the built-in sandbox isolates only shell commands, while Claude's file tools, MCP servers and hooks work outside it. Docker commands also ran outside the sandbox, and there were problems with removing worktrees: the agent needed extra instructions on how to do it correctly from inside the sandbox.

The second attempt was to run agents in containers with Docker Compose. In general, the scheme works, except that agents often need to run Docker containers. With DinD, I could not do this without elevated privileges, and running containers on the host caused collisions between agents of the same or different projects: for example, they all wanted to run PostgreSQL on port 5432. There was also the fun part: to reach services on the host, you had to use the magic name host.docker.internal, and this often broke tests with default settings in the projects I worked on.

So I came to a scheme where all work happened in VMs that ran right on my Mac through UTM. It worked: there were real boundaries between the agents, and each one had its own Docker. But it was rather cumbersome: the VMs took a lot of disk space, I had to keep a template VM in shape to clone it for each project and each agent, and I also had to put all the secrets into each VM, where they immediately became available to the agent.

And finally, the current approach: Docker Sandboxes. One command creates and starts an isolated sandbox for the given project, with an agent session and preinstalled development tools. The secrets are managed on the host, and inside, the agent sees only placeholders, which a proxy replaces with the real values in requests to the right services.

How it works now

For convenience, all clones of the projects I work on live in a separate directory, at the same paths as on GitHub: ~/Workspace/<owner>/<repo>. To start work on any of them, I run just one command:

~/Sandboxer/run.sh ~/Workspace/<owner>/<repo> claude
Enter fullscreen mode Exit fullscreen mode

If the project does not have a Docker Sandbox yet, the command creates it. Then it starts the sandbox and opens Claude Code in it in --dangerously-skip-permissions mode. Each project gets its own sandbox named <owner>-<repo>, and a second run opens one more agent session in the same sandbox.

The sandbox is created from a kit. In Docker Sandboxes terms, this is an image with all the tools, the Claude Code settings and the list of allowed hosts. run.sh builds the kit from a separate clone, ~/Sandboxer, which holds the sandbox sources, the settings and the instructions for the agents. This is done for security: the agent cannot change its own configuration, so the next time I start it, it cannot come up with new, unverified behavior.

Inside the sandbox, the agent can do anything, but the sandbox itself is restricted. It is a separate microVM with its own Docker Engine, so two projects can run PostgreSQL on port 5432 at the same time. The network is open only to the hosts on the list, the proxy injects the secrets, and of the host's files, only the project folder is mounted into the sandbox. So while the agent works, I see all changes of the project in my IDE on the host.

Pitfalls

The proxy adds secrets only to headers. But, for example, the Telegram Bot API requires the token in the URL path, where the proxy cannot insert it. So on each start, run.sh passes the real bot token to the session through an environment variable.

Updates are slow. A sandbox that already exists does not get changes: to update the image or the configuration, you have to remove the sandbox and create it again. The image takes tens of minutes to rebuild, and everything that existed only inside the sandbox is lost. I do not have a ready solution yet. As the next step, I will try to move the tool sets into separate kits and build the sandbox only from the ones it needs.

The sandbox has no GPU and no display. A headless browser works there, but for a game engine, or to see the result in a browser, I either run the commands manually on the host in the project folder, as the agent instructs, or additionally set up the sandbox and the agent to talk with the app on the host in both directions.

Bonus: work and personal projects

I run both personal and work projects through agents in sandboxes. For personal ones, I use my Claude subscription, and for work ones, the company's account in AWS Bedrock. Here it is important not to make a mistake: a personal project must not spend the company's quota, and work code must not go directly to Anthropic, even by accident.

So it is not the project that defines the account, but the kit: each account has its own kit. The personal kit connects only my subscription token, and its network policy does not let the agent into Bedrock. The work kit connects only the Bedrock key, and its policy does not let the agent reach api.anthropic.com. So a sandbox simply cannot use the other account: it has neither the key nor network access to it.

Happy Burning!

Top comments (0)