OpenCode Custom Provider in Docker Sandbox
A simple guide/writeup to myself and others when setting up OpenCode inside a Docker sandbox. Get Started Install OpenCode Get your provider key Create your opencode.jsonc config: { "$schema" : "https://opencode.ai/config.json" , "lsp" : true , // see note below "enabled_providers" : [ "custom_provider" ], "provider" : { "custom_provider" : { "npm" : "@ai-sdk/openai-compatible" , "name" : "My…
A comprehensive guide to setting up OpenCode within a Docker sandbox is available to help users get started. To begin, obtain the OpenCode provider key and then create the opencode.jsonc configuration file.
The configuration file should include the following elements:
1. The $schema attribute should reference the OpenCode configuration file: https://opencode.ai/config.json
2. The lsp (language server protocol) option should be set to true. This allows OpenCode to function as a language server.
3. The enabled_providers array should contain the custom_provider entry.
Next, add the custom provider settings within the provider object:
1. The custom_provider object should be defined, specifying the npm package, custom_provider.
2. The name of the custom provider should be set to "My Custom Provider".
3. The options object should include the baseURL, pointing to custom_provider.example.com/v1 (note that this URL should be modified to match your specific provider).
4. The apiKey should be set as an environment variable, using the env:CUSTOM_PROVIDER_API_KEY notation. This allows for secure management of the API key within the Docker sandbox environment.
5. The models object should list the specific models that the custom provider should support, including their names and API keys.
Some additional notes to keep in mind:
* OpenCode will use the first encountered config file in the directory tree, which can be helpful when running within a sandbox that only has access to a subset of the disk's directory structure.
* When running OpenCode in a container, such as a Docker sandbox, you'll need to pass in the API key as an environment variable to ensure that agents cannot access the entire machine. Here's an example command for setting up OpenCode in a Docker sandbox:
export CUSTOM_PROVIDER_API_KEY = "****************" && \
sbx secret set-custom --host custom_provider.example.com --env CUSTOM_PROVIDER_API_KEY --value $CUSTOM_PROVIDER_API_KEY
OpenCode is capable of supporting a variety of providers, which can be enabled or disabled based on your specific needs. Enable or disable providers using the disabled_providers or enabled_providers arrays.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.