The OpenHands Docker Runtime is the core component that enables secure and flexible execution of AI agent’s action.
It creates a sandboxed environment using Docker, where arbitrary code can be run safely without risking the host system.
OpenHands needs to execute arbitrary code in a secure, isolated environment for several reasons:
Security: Executing untrusted code can pose significant risks to the host system. A sandboxed environment prevents malicious code from accessing or modifying the host system’s resources
Consistency: A sandboxed environment ensures that code execution is consistent across different machines and setups, eliminating “it works on my machine” issues
Resource Control: Sandboxing allows for better control over resource allocation and usage, preventing runaway processes from affecting the host system
Isolation: Different projects or users can work in isolated environments without interfering with each other or the host system
Reproducibility: Sandboxed environments make it easier to reproduce bugs and issues, as the execution environment is consistent and controllable
The OpenHands Runtime system uses a client-server architecture implemented with Docker containers. Here’s an overview of how it works:
User Input: The user provides a custom base Docker image
Image Building: OpenHands builds a new Docker image (the “OH runtime image”) based on the user-provided image. This new image includes OpenHands-specific code, primarily the “runtime client”
Container Launch: When OpenHands starts, it launches a Docker container using the OH runtime image
Action Execution Server Initialization: The action execution server initializes an ActionExecutor inside the container, setting up necessary components like a bash shell and loading any specified plugins
Communication: The OpenHands backend (client: openhands/runtime/impl/action_execution/action_execution_client.py; runtimes: openhands/runtime/impl/docker/docker_runtime.py, openhands/runtime/impl/local/local_runtime.py) communicates with the action execution server over RESTful API, sending actions and receiving observations
Action Execution: The runtime client receives actions from the backend, executes them in the sandboxed environment, and sends back observations
Observation Return: The action execution server sends execution results back to the OpenHands backend as observations
The role of the client:
It acts as an intermediary between the OpenHands backend and the sandboxed environment
It executes various types of actions (shell commands, file operations, Python code, etc.) safely within the container
It manages the state of the sandboxed environment, including the current working directory and loaded plugins
It formats and returns observations to the backend, ensuring a consistent interface for processing results
How OpenHands builds and maintains OH Runtime images
OpenHands’ approach to building and managing runtime images ensures efficiency, consistency, and flexibility in creating and maintaining Docker images for both production and development environments.Check out the relevant code if you are interested in more details.
No re-build: OpenHands first checks whether an image with the same most specific source tag exists. If there is such an image,
no build is performed - the existing image is used.
Fastest re-build: OpenHands next checks whether an image with the generic lock tag exists. If there is such an image,
OpenHands builds a new image based upon it, bypassing all installation steps (like poetry install and
apt-get) except a final operation to copy the current source code. The new image is tagged with a
source tag only.
Ok-ish re-build: If neither a source nor lock tag exists, an image will be built based upon the versioned tag image.
In versioned tag image, most dependencies should already been installed hence saving time.
Slowest re-build: If all of the three tags don’t exists, a brand new image is built based upon the base
image (Which is a slower operation). This new image is tagged with all the source, lock, and versioned tags.
This tagging approach allows OpenHands to efficiently manage both development and production environments.
Identical source code and Dockerfile always produce the same image (via hash-based tags)
The system can quickly rebuild images when minor changes occur (by leveraging recent compatible images)
The lock tag (e.g., runtime:oh_v0.9.3_1234567890abcdef) always points to the latest build for a particular base image, dependency, and OpenHands version combination
Named volume: “volume:<name>:/container/path[:mode]” or any non-absolute host spec treated as a named volume
Overlay mode (copy-on-write layer) is supported for bind mounts by appending “:overlay” to the mode (e.g., “:ro,overlay”).
To enable overlay COW, set SANDBOX_VOLUME_OVERLAYS to a writable host directory; per-container upper/work dirs are created under it. If SANDBOX_VOLUME_OVERLAYS is unset, overlay mounts are skipped.Implementation references:
openhands/runtime/impl/docker/docker_runtime.py (named volumes in _build_docker_run_args; overlay mounts in _process_overlay_mounts)
The OpenHands Runtime supports a plugin system that allows for extending functionality and customizing the runtime environment. Plugins are initialized when the action execution server starts up inside the runtime.
Plugin Definition: Plugins are defined as Python classes that inherit from a base Plugin class
Plugin Registration: Available plugins are registered in openhands/runtime/plugins/__init__.py via ALL_PLUGINS
Plugin Specification: Plugins are associated with Agent.sandbox_plugins: list[PluginRequirement]. Users can specify which plugins to load when initializing the runtime
Initialization: Plugins are initialized asynchronously when the runtime starts and are accessible to actions
Usage: Plugins extend capabilities (e.g., Jupyter for IPython cells); the server exposes any web endpoints (ports) via host port mapping