How to Run OpenCode AI Agents Safely in Docker Sandbox

Is your AI model executing all kinds of commands on your laptop? It would be better if you ran your AI agent within a sandboxed environment. This way, you control what the AI model may access or change. A solution for that is Docker Sandbox. In this blog, you will learn how to get started with Docker Sandbox. Enjoy!

1. Introduction

You install an AI agent on your laptop, e.g., GitHub Copilot CLI, OpenCode, etc. By default, the AI model running in the agent, can execute all kinds of commands on your laptop with your permissions. That is quite scary if you think closely about it. You are probably admin on your laptop and you let an AI model, which you do not control, use your admin rights. Oh, you need to approve before a command is being executed, so you do control what is executed? But, do you read every command very closely and do you understand each command before you approve? Probably not, after a while you become a meat proxy, you just approve without actually reading what you approve. A solution for this is to run your agent inside a sandbox. Docker has created Docker Sandbox for this purpose.

In the remainder of this blog, you will learn how to setup Docker Sandbox using your own skills, MCP servers, etc. and all kinds of other configuration you need. OpenCode will be used as an agent and a GitHub Copilot Enterprise subscription within a tenant (the latter will be an important detail).

2. Installation

Installation should be quite easy, just follow the official installation instructions.

However, if you are using Ubuntu 22.04, you need to follow a different path. Do note that Docker Sandbox is officially not supported for Ubuntu 22.04, but it works. You can continue to the next paragraph if you could follow the official instructions, otherwise, the remainder of this paragraph will be very useful to you.

Check whether KVM is available. If you see an output like the one below with kvm_intel, you are good to go.

$ lsmod | grep kvm
kvm_intel             491520  0
kvm                  1409024  1 kvm_intel
irqbypass              12288  1 kvm

Add your user to the kvm group.

sudo usermod -aG kvm $USER

Execute the following to make the group change take effect.

newgrp kvm

Now continue with the manual installation. Create an installation directory.

mkdir -p ~/sbx-install
cd ~/sbx-install

The manual installation instructions were at the time of writing often not accessible due to a ‘504 gateway time-out’. Also, the download link was often not accessible for the same reason. This complicated the manual installation quite a bit. However, eventually it succeeded.

curl -fL \
  https://github.com/docker/sbx-releases/releases/latest/download/DockerSandboxes-linux-amd64.tar.gz \
  -o sbx.tar.gz

Untar the downloaded artifact and navigate into the docker-sbx directory.

tar -xzf sbx.tar.gz
cd docker-sbx

Run the install script.

./install.sh 

Add the path to your .bashrc.

echo 'export PATH="$HOME/.docker/sbx/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Docker Sandbox collects telemetry data, if you do not want this, you can disable it. Do note that you have to restart the sbx daemon.

sbx settings set telemetry.disabled true
sbx daemon restart

In order to use Docker Sandbox, you need to log in to DockerHub. In the FAQ is explained why you need to login.

sbx login

After this, you are all set and ready to go.

3. First Startup

Navigate to the directory of one of your repositories and start the sandbox. You give the sandbox a name and, as mentioned earlier, you choose OpenCode as your agent.

sbx run --name my-sandbox opencode

It takes a few seconds to startup, but after that, you see an OpenCode terminal, just like you are used to when starting OpenCode on your host machine. The difference now is that it runs inside a sandbox.

When you open another terminal, you can open an interactive dashboard showing you all your sandboxes.

sbx

If you want to stop the sandbox, you just enter the following command.

sbx stop my-sandbox

With a simple ls, you can also show a list of your sandboxes.

$ sbx ls

  SANDBOX      AGENT      STATUS      PORTS   WORKSPACE                       CREATED
  my-sandbox   opencode   ○ stopped           /.../dockersandbox/my-project   7m

By means of the run command, you can start the sandbox again. This time, it will start faster.

sbx run --name my-sandbox

And finally, if you want to remove the sandbox, you just execute the rm command.

sbx rm my-sandbox

4. Connect to GitHub Copilot

You have started the OpenCode sandbox, but how do you connect to your GitHub Copilot subscription? See the official documentation and the GitHub Copilot section. However, this approach does not work when you have a GitHub Copilot Enterprise subscription (using <company name>.ghe.com) and data residency within the EU. Therefore, you need to follow a different path.

First, you need to allow the sandbox to access the GitHub Copilot URLs where you replace companyname with the name of your company.

sbx policy allow network companyname.ghe.com:443
sbx policy allow network api.companyname.ghe.com:443
sbx policy allow network copilot-api.companyname.ghe.com:443
sbx policy allow network copilot-proxy.companyname.ghe.com:443

Start the sandbox and connect to the provider.

/connect

After connecting, you have access to the GitHub Copilot models as configured for your company. This approach is not ideal if you have many sandboxes, but it works. Later, you will see an alternative approach that avoids the need to connect inside every sandbox.

5. Configuring the Harness

You probably do not use OpenCode as delivered out-of-the-box. You have created custom skills, use MCP servers, and have a custom OpenCode configuration where you limit the number of AI providers (for example, you only want to see the GitHub Copilot models and not the free OpenCode models) and set a default agent. You do not want to configure this for every sandbox separately. Let’s see how you can create a common setup for all your sandboxes.

5.1 Connect to GitHub Copilot Part Two

First let’s solve this GitHub Copilot connection issue. This solution is not the most perfect one, but it works.

You have connected to your GitHub Copilot subscription in OpenCode on your host machine. Now export the credentials.

opencode auth export

Create a directory on your host to store the credentials.

mkdir -p ~/.config/sbx-config/credentials
chmod 600 ~/.config/sbx-config/credentials

Save the output of the opencode auth export command to a file named opencode-credentials.json in this directory.

Create a Docker Build Kit. A kit packages software and configuration for a sandbox. It is a yaml file and you define in this yaml file that you want to import the credentials file in the sandboxed OpenCode.

# syntax=docker/sandbox-kit:3
schemaVersion: "3"
kind: mixin

displayName: OpenCode Enterprise Authentication
description: Import GitHub Copilot Enterprise credentials before OpenCode starts
version: "1.0.0"

capabilities:
  - type: com.docker.sandbox/lifecycle@1
    config:
      install:
        - description: Import GitHub Enterprise credentials into OpenCode
          user: "agent"
          env: [HOME]
          command: |
            set -eu

            CREDENTIALS="/mnt/host-auth/opencode-credentials.json"
            OPENCODE="/usr/local/bin/opencode"
            EXPECTED_TENANT="companyname.ghe.com"

            # Fail closed when credentials are missing
            if [ ! -s "$CREDENTIALS" ]; then
              echo "ERROR: GitHub Copilot credentials are missing"
              exit 1
            fi

            # Verify that this is the expected Enterprise tenant
            if ! grep -Fq "$EXPECTED_TENANT" "$CREDENTIALS"; then
              echo "ERROR: Credentials do not belong to $EXPECTED_TENANT"
              exit 1
            fi

            # Use the absolute binary path (sbx v0.51.0 PATH workaround)
            if [ ! -x "$OPENCODE" ]; then
              echo "ERROR: OpenCode binary not found: $OPENCODE"
              exit 1
            fi

            export HOME=/home/agent

            # Import into OpenCode v2's credential database
            "$OPENCODE" auth import --standalone < "$CREDENTIALS"

            echo "GitHub Copilot Enterprise credentials imported successfully"

You will see in the next paragraphs how the kit is enabled in the sandbox.

5.2 Limit Models

If you connected to GitHub Copilot successfully, you noticed that you were still able to see all kinds of other providers. This is confusing, especially if you want to prevent making mistakes and use one of the free models by accident.

You need a custom OpenCode configuration for this purpose. Create a file opencode-sandbox.jsonc in the directory ~/.config/opencode. In this file, deny all providers and enable only the GitHub Copilot provider. You can also set a default model and agent if desired.

{
  "$schema": "https://opencode.ai/config.json",
  "model": "github-copilot/claude-opus-5.5",
  "default_agent": "plan",
  // Only GitHub Copilot; this also hides the free OpenCode models.
  "policies": [
    { "action": "provider.use", "resource": "*", "effect": "deny" },
    { "action": "provider.use", "resource": "github-copilot", "effect": "allow" }
  ],
  "share": "disabled",
}

5.3 Skills

How do you add skills to the sandbox? Mount the directories inside the sandbox (see the next paragraph). Assume you have installed skills in ~/.agents/skills from Claude Code or other resources, and you have custom skills shared with colleagues in a checked-out repository on your laptop. Both paths will be mounted inside the sandbox to personal-skills and shared-skills directories, respectively. To make OpenCode aware of these skills, add the following to the opencode-sandbox.jsonc file.

  "skills": [
    "/home/agent/.config/opencode/shared-skills",
    "/home/agent/.config/opencode/personal-skills"
  ]

5.4 Startup Script

The best approach now is to create a startup script for the sandbox. In this script, set the following:

  1. bind the Docker Build Kit using --kit;
  2. the current directory will be used by default as a name for the sandbox;
  3. mount the opencode-sandbox.jsonc;
  4. mount your custom agents located in ~/.config/opencode/agents;
  5. mount any plugins located in ~/.config/opencode/plugins;
  6. mount the shared skills;
  7. mount the personal skills;
  8. mount the opencode-credentials.json file;
  9. set the custom OpenCode config.

Create a file opencode-sandbox in ~/.local/bin and make it executable. The script is the following.

#!/usr/bin/env bash
set -euo pipefail

# Project directory: the current directory is the default
PROJECT="${1:-$PWD}"
PROJECT="$(cd "$PROJECT" && pwd)"

# Sandbox name: name of the project directory
NAME="${2:-$(basename "$PROJECT")}"

# Configuration
KIT="$HOME/.config/sbx-config/kits/opencode-enterprise-auth"
CREDENTIALS="$HOME/.config/sbx-config/credentials/opencode-credentials.json"

# Check credentials
if [[ ! -s "$CREDENTIALS" ]]; then
  echo "ERROR: GitHub Copilot Enterprise credentials missing:"
  echo "$CREDENTIALS"
  exit 1
fi

if [[ ! -f "$KIT/opencode-enterprise-auth.yaml" ]]; then
  echo "ERROR: OpenCode Enterprise kit not found: $KIT"
  exit 1
fi

# Check project directory
if [[ ! -d "$PROJECT" ]]; then
  echo "ERROR: Projectdirectory does not exist: $PROJECT"
  exit 1
fi

echo "Starting OpenCode sandbox..."
echo "Project:  $PROJECT"
echo "Sandbox:  $NAME"

sbx run opencode \
  --kit "$KIT" \
  "$PROJECT" \
  "$HOME/.config/opencode/opencode-sandbox.jsonc:/home/agent/.config/opencode/opencode.jsonc:ro" \
  "$HOME/.config/opencode/agents:/home/agent/.config/opencode/agents:ro" \
  "$HOME/.config/opencode/plugins:/home/agent/.config/opencode/plugins:ro" \
  "$HOME/.agents/skills:/home/agent/.config/opencode/personal-skills:ro" \
  "/<local repository directory>/githubcopilot/skills:/home/agent/.config/opencode/shared-skills:ro" \
  "$CREDENTIALS:/mnt/host-auth/opencode-credentials.json:ro" \
  -e OPENCODE_CONFIG=/home/agent/.config/opencode/opencode.jsonc \
  --name "$NAME" 

When you start this script in your repository, you will be connected to your GitHub Copilot subscription, have your skills available and you will be limited to the GitHub Copilot models.

5.5 MCP Servers

How do you add MCP servers to your setup? The recommended approach is to run the MCP servers on your host and register them in the Docker Sandbox. However, exercise caution: if the MCP server can execute commands on your host, you bypass the sandbox entirely. For example, the IntelliJ MCP Server has an execute_terminal_command tool. If enabled, this allows the sandbox to execute commands on your host, which is undesirable.

First, add the javadocs remote MCP server by registering it.

sbx mcp add javadocs --url https://www.javadocs.dev/mcp

In order to use it, you need to add it to the sbx run command.

--static-mcp javadocs

In the OpenCode sandbox, you will only see one MCP server and that is the MCP Gateway. In order to check which MCP servers are actually available, you need to execute the following command.

$ sbx mcp ls

  NAME       TRANSPORT     STATUS   GATEWAY                 DETAIL
  javadocs   remote http   ready    LOCAL, managed by you   

In order to verify whether it is actually usable, you can ask a simple question like find the latest version of org.assertj.assertj-core via javadocs. You can see in the terminal output which tools have been used.

Adding an MCP server that runs as a Docker container is more complex. Create a directory ~/.config/sbx-config/mcp and an executable atlassian.sh file. The example script shows how to start the Atlassian MCP server using only the Jira connection. Secrets are already configured in the host OpenCode configuration.

#!/usr/bin/env bash
set -euo pipefail

S="$HOME/.config/opencode/secrets"
IMAGE="ghcr.io/sooperset/mcp-atlassian:latest"

export JIRA_URL=https://companyname.atlassian.net
export JIRA_USERNAME="$(<"$S/jira-username")"
export JIRA_API_TOKEN="$(<"$S/jira-api-token")"
export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
export TOOLSETS=default,jira_agile
export READ_ONLY_MODE=true

exec docker run --init --pull=always --rm -i --network host \
  --cap-drop all --security-opt no-new-privileges=true \
  -e JIRA_URL -e JIRA_USERNAME -e JIRA_API_TOKEN \
  -e TOOLSETS -e READ_ONLY_MODE \
  "$IMAGE"

Now, you register this bash script.

sbx mcp add atlassian --command "$HOME/.config/sbx-config/mcp/atlassian.sh"

And of course add it to the sandbox in the static mcp list.

5.6 Other Tooling

You added your skills earlier. However, some skills require specific tooling that is not available in the sandbox by default. For example, you need Java and Maven to build your code. Add this tooling using Docker Build Kits, as you did with the credentials. In an install step, install all necessary tooling and add network policies. To install SDKMAN, for example, allow get.sdkman.io and api.sdkman.io during both installation and runtime. The best approach is to ask your AI model to help you set up the build kit for the tooling you need. It works well.

6. Troubleshooting

If you encounter problems and want to look inside the sandbox, use the sbx exec command. It is similar to accessing a regular container.

sbx exec -it my-sandbox -- bash

Sometimes MCP servers contain tools with the same name. For example, the GitLab MCP server and the SonarQube MCP server both have a tool list_branches. The first registered MCP server takes precedence, and the tools of the other MCP server become unavailable. You can prevent this by prefixing the tools with the MCP server name, but you must enable this setting.

sbx settings set mcp.useServerNamePrefixes true

Do note that you need to recreate the sandbox before it takes effect.

7. Conclusion

Sandboxes are an excellent concept. You no longer need to worry whether an AI model will corrupt your system. However, configuring your setup correctly takes time. If you want to get started, take your time and add functionality one at a time rather than rushing. Do not expect to be ready in a couple of hours, but afterwards, you will benefit from all the advantages.


Discover more from My Developer Planet

Subscribe to get the latest posts sent to your email.

Leave a Reply

Powered by WordPress.com.

Up ↑