Self-Host OpenHands: Run Your Own AI Coding Agent Using Docker

AI tutorial - IT technology blog
AI tutorial - IT technology blog

Beyond Chatbots: Let the AI Actually Write the Code

We’ve all been there: asking an AI for help, then manually copy-pasting snippets, fixing indentation, and chasing down bugs the LLM introduced. OpenHands (formerly OpenDevin) breaks this cycle. It isn’t just another window to chat with; it is an autonomous agent that uses a terminal, manages files, and executes commands. Think of it as a junior developer who works inside a controlled environment to finish the tasks you’re too busy to handle.

I’ve used this setup for complex refactoring and the stability is impressive. By moving the execution into a Docker container, your host machine stays clean while the agent has a playground to install dependencies and run tests. If you’re tired of jumping between your browser and your IDE, self-hosting OpenHands is the best way to reclaim your focus.

Why Docker is Non-Negotiable

Giving an AI agent the power to run shell commands is powerful but risky. You don’t want a hallucination turning a simple cleanup into a rm -rf / disaster on your primary OS. Docker provides the isolation layer needed to keep your files safe.

Using Docker solves three specific problems:

  • Security: The agent lives in a restricted sandbox with no direct access to your host system unless you explicitly allow it.
  • Consistency: The environment is identical every time. No more “it worked on my machine” issues.
  • Zero Clutter: You won’t need to manage specific Python 3.11 or Node.js versions on your local path.

Setting Up the Foundation

Ensure Docker is active before you start. You’ll also need an API key. While OpenAI’s GPT-4o is a solid choice, Claude 3.5 Sonnet is currently the gold standard for OpenHands due to its superior reasoning in coding loops. If you prefer privacy, you can also point this toward a local Ollama instance.

1. Prepare Your Workspace

OpenHands needs a specific place to work. Create a dedicated directory so the agent doesn’t get lost in your root folder:

mkdir -p ~/openhands-projects
cd ~/openhands-projects

2. Pull the OpenHands Image

The image is substantial—roughly 3GB—because it bundles a full Linux execution environment. Grab the latest stable version with this command:

docker pull ghcr.io/all-hands-ai/openhands:0.9

3. Launch the Container

The following command starts the engine. It maps the Docker socket, allowing OpenHands to spin up its own “sibling” containers for task execution.

docker run -it \
    --pull=always \
    -e SANDBOX_USER_ID=$UID \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v ~/.openhands-state:/.openhands-state \
    -v $(pwd):/opt/workspace_base \
    -p 3000:3000 \
    --add-host host.docker.internal:host-gateway \
    --name openhands-app \
    ghcr.io/all-hands-ai/openhands:0.9

Configuring for Peak Performance

The heavy lifting happens in the web UI, but the initial environment variables dictate how stable the experience will be. Let’s break down the vital parts of that startup command.

Smart Workspace Mapping

The -v $(pwd):/opt/workspace_base flag is your bridge. It mounts your current folder into the agent’s view. When the AI writes a file in /opt/workspace_base, it appears on your laptop instantly. This is real-time collaboration.

The Docker Socket Explained

Why mount /var/run/docker.sock? It allows OpenHands to be an orchestrator. When the agent needs to run your code, it doesn’t do it inside the main app container. It creates a fresh, isolated container for that specific task. If the code crashes, only that temporary container dies.

Fine-Tuning the LLM

Head to http://localhost:3000 to finish the setup. For the best results, use these settings:

  • Language Model Provider: Anthropic
  • Model: claude-3-5-sonnet-20240620
  • API Key: [Your Key]
  • Base URL: Leave blank for official APIs

Verification and Troubleshooting

Once you hit save, the chat interface appears. Test the waters with a concrete task. Ask it to: “Initialize a Vite project with React and TypeScript, then create a basic login component.”

Watch the Logs

If the UI feels sluggish, the terminal is your best friend. I often keep a separate tab open to monitor the container’s pulse. This helps catch API rate limits or permission snags early.

docker logs -f openhands-app

Fixing Permission Denied Errors

Linux users often run into file ownership issues because the agent operates as a specific user inside Docker. While -e SANDBOX_USER_ID=$UID fixes most cases, you might occasionally need to reclaim ownership of your project folder:

sudo chown -R $USER:$USER ~/openhands-projects

Resource Management

OpenHands is hungry. If your fans start spinning too fast, cap the resources. You can limit the container to 4GB of RAM and 2 CPU cores by adding --memory="4g" --cpus="2.0" to your run command. I usually keep these limits off during major refactoring sessions to ensure the agent finishes the job quickly.

You’ve now moved past basic AI autocomplete. You have a functional teammate that handles the tedious boilerplate and unit tests while you focus on high-level architecture. It’s a massive upgrade to any developer’s workflow.

Share: