The best way to set up OpenHands to access compilers in a different Docker container, both running locally, is to leverage Docker's built-in networking capabilities. The most straightforward and recommended approach is to use a user-defined bridge network with .
Here's a breakdown of why this is the best approach and how to implement it:
Why User-Defined Bridge Networks and ?
Service Discovery by Name: When containers are on the same user-defined bridge network, they can communicate with each other using their service names (as defined in docker-compose.yml) as hostnames. This eliminates the need to hardcode IP addresses, which can change when containers are restarted.
Isolation: User-defined networks provide better isolation than the default bridge network.
This means your OpenHands application and your compiler container won't interfere with other Docker containers you might be running.
Simplified Configuration: Docker Compose allows you to define and manage multi-container Docker applications in a single docker-compose.yml file.
This makes it easy to declare your OpenHands service, your compiler service, and the shared network they'll use.
Reproducibility: A docker-compose.yml file ensures that your setup is consistent and reproducible across different environments or for other developers.
Steps to Set Up:
Create a docker-compose.yml file:
This file will define both your OpenHands service and your compiler service, along with the shared network.
YAML
version: '3.8' # Use a recent Compose file format
services:
openhands:
image: ghcr.io/all-hands-ai/openhands:latest # Or your specific OpenHands image
container_name: openhands_app
environment:
# OpenHands specific environment variables (e.g., for LLM if applicable)
- LLM_BASE_URL=ernal:11434 # Example for if running on host
# Other OpenHands env vars as needed
ports:
- "3000:3000" # Expose OpenHands UI if needed
volumes:
- ./workspace:/opt/workspace_base # Mount a local workspace for OpenHands
# If OpenHands needs to interact with the daemon to spawn its own sandbox:
- /var/run/docker.sock:/var/run/docker.sock # CAUTION: Grants container full Docker access
networks:
- my_shared_network # Connect to the shared network
compiler_service:
image: your_compiler_image:latest # Replace with your compiler's image (e.g., gcc, clang, make)
container_name: dev_compilers
# If your compiler needs to listen on a port (less common for direct compiler access, more for a compiler server)
# ports:
# - "8080:8080"
volumes:
# Mount any necessary directories for your compiler to access source code, etc.
# For example, if OpenHands outputs files to its workspace and the compiler needs to read them:
- ./workspace:/app/shared_code # Example: Mount the OpenHands workspace here
networks:
- my_shared_network # Connect to the shared network
networks:
my_shared_network:
driver: bridge
Explanation of the docker-compose.yml:
version: '3.8': Specifies the Docker Compose file format version.
services Defines your individual Docker containers.
openhands
image The image for OpenHands. Use the official image or your custom-built one.
container_name A user-friendly name for your OpenHands container.
environment Set environment variables for OpenHands.
LLM_BASE_URL: If OpenHands needs to connect to an LLM running on your host machine (e.g., ), you can use ernal:PORT to access it. If your LLM is another Docker container, you'd use its service name and internal port (e.g., http://ollama_service:11434).
ports Maps ports from the container to your host. 3000:3000 means port 3000 in the container is accessible on port 3000 of your localhost.
volumes Mounts directories from your host machine into the container.
./workspace:/opt/workspace_base: This is crucial. It mounts a local directory named workspace (relative to your docker-compose.yml file) into the OpenHands container. This is where OpenHands will create and manage files, including source code that needs compilation.
/var/run/docker.sock:/var/run/docker.sock: Use with extreme caution. OpenHands sometimes needs to spawn its own sandboxed containers for code execution.
This volume mount allows the OpenHands container to interact with the daemon on your host, essentially giving it control over your environment. Only use this in trusted development environments.
networks Connects the OpenHands service to the my_shared_network.
compiler_service
image The image for your compilers. This could be an image pre-packaged with GCC, Clang, Make, etc., or one you build yourself.
container_name A user-friendly name for your compiler container.
volumes
./workspace:/app/shared_code: This is key. By mounting the same workspace directory (or a relevant subdirectory) from your host into the compiler container, both OpenHands and the compiler container will have access to the same files. OpenHands writes to this directory, and the compiler reads from it.
networks Connects the compiler service to the my_shared_network.
networks Defines the user-defined network.
my_shared_network The name of your custom network.
driver: bridge: Specifies that it's a bridge network.
Build your Compiler Docker Image (if not using an existing one):
If you need specific compilers or tools not available in a public image, create a Dockerfile for your compiler service:
Dockerfile
Dockerfile for your compiler_service
FROM ubuntu:latest # Or a base image with your preferred OS
RUN apt-get update && apt-get install -y build-essential curl git # Install GCC, G++, make, etc.
Add any other compilers or tools you need
WORKDIR /app/shared_code # Set a working directory inside the container
Then, in your docker-compose.yml, change image: your_compiler_image:latest to build: ./path/to/your/compiler/dockerfile/context.
Run Docker Compose:
to the directory where your docker-compose.yml file is located and run:
Bash
docker compose up -d
The -d flag runs the containers in detached mode (in the background).
How OpenHands Accesses Compilers:
Once both containers are running on my_shared_network, OpenHands can access the compilers in the compiler_service container in a few ways:
Shared Volume: This is the most common and recommended method for compilation.
OpenHands will write source code files to the mounted workspace directory.
The compiler_service container, also having this volume mounted (e.g., at /app/shared_code), can then access these files.
From within OpenHands (when it executes a shell command in its own sandbox), it can run commands like:
Bash
docker exec dev_compilers gcc /opt/workspace_base/my_program.c -o /opt/workspace_base/my_program
Or if your compiler service is set up to receive commands, you might configure OpenHands to interact with it via an API or a custom script.
Note on docker exec: OpenHands' runtime architecture likely involves it executing commands within its own sandbox.
To docker exec into another container (dev_compilers in this case) from within the OpenHands container, the OpenHands container needs access to the daemon. This is why the /var/run/docker.sock volume mount is often necessary for OpenHands if it needs to orchestrate other containers or docker exec into them.
Network-based Compiler Service (Less Common for Simple Compilers):
If your "compilers" were actually a compilation service (e.g., a custom API that receives code and compiles it), then you would:
Expose a port on the compiler_service (e.g., ports: - "8080:8080").
From OpenHands, you would make an HTTP request (or similar) to http://compiler_service:8080 (where compiler_service is the service name) to send code for compilation.
Important Considerations:
Security of /var/run/docker.sock: Mounting /var/run/docker.sock into a container effectively gives that container root access to your host. If the OpenHands container were compromised, an attacker could potentially control all other containers and even the host machine. For production environments or untrusted code, this is a significant security risk. For local development and experimentation, it's a common practice but be aware of the implications.
Workspace Management: Ensure that OpenHands and your compiler container have consistent access to the necessary files. Using a shared volume is the cleanest way to achieve this.
Compiler Image: Choose or create a compiler image that includes all the necessary compilers, build tools, and libraries that OpenHands might require for its tasks.
Error Handling: When OpenHands executes compilation commands, it needs to be able to parse the output (stdout/stderr) to understand if the compilation was successful and to extract any error messages.
By using Compose with a user-defined bridge network and carefully configured volumes, you can create a robust and manageable environment for OpenHands to interact with separate compiler containers.