|
| 1 | +--- |
| 2 | +title: Use OpenCode with Docker Model Runner |
| 3 | +description: Configure OpenCode to use Docker Model Runner so you can code with local models. |
| 4 | +summary: | |
| 5 | + Connect OpenCode to Docker Model Runner with an OpenAI-compatible endpoint, |
| 6 | + choose coding models, and package `gpt-oss` with a larger context window. |
| 7 | +keywords: ai, opencode, docker model runner, local models, coding assistant |
| 8 | +tags: [ai] |
| 9 | +params: |
| 10 | + time: 10 minutes |
| 11 | +--- |
| 12 | + |
| 13 | +This guide shows how to connect OpenCode to Docker Model Runner so OpenCode can |
| 14 | +use local models for coding tasks. You'll configure an `opencode.json` file, |
| 15 | +verify the API endpoint, and run OpenCode against models served from your local |
| 16 | +Docker environment. |
| 17 | + |
| 18 | +Unlike the [OpenCode sandbox guide](../manuals/ai/sandboxes/agents/opencode.md), |
| 19 | +this guide focuses on using OpenCode as a local coding tool backed by Docker |
| 20 | +Model Runner rather than running OpenCode in a containerized sandbox. |
| 21 | + |
| 22 | +In this guide, you'll learn how to: |
| 23 | + |
| 24 | +- Pull coding models for OpenCode |
| 25 | +- Configure OpenCode to use Docker Model Runner |
| 26 | +- Verify the local API endpoint and start OpenCode |
| 27 | +- Package `gpt-oss` with a larger context window when you need it |
| 28 | + |
| 29 | +## Prerequisites |
| 30 | + |
| 31 | +Before you start, make sure you have: |
| 32 | + |
| 33 | +- [Docker Desktop](../get-started/get-docker.md) or Docker Engine installed |
| 34 | +- [Docker Model Runner enabled](../manuals/ai/model-runner/get-started.md#enable-docker-model-runner) |
| 35 | +- [OpenCode installed](https://opencode.ai/docs) |
| 36 | + |
| 37 | +If you use Docker Desktop, turn on TCP access in **Settings** > **AI**, or run: |
| 38 | + |
| 39 | +```console |
| 40 | +$ docker desktop enable model-runner --tcp 12434 |
| 41 | +``` |
| 42 | + |
| 43 | +## Step 1: Pull a coding model |
| 44 | + |
| 45 | +Pull one or more models before you configure OpenCode: |
| 46 | + |
| 47 | +```console |
| 48 | +$ docker model pull ai/qwen3-coder |
| 49 | +$ docker model pull ai/devstral-small-2 |
| 50 | +``` |
| 51 | + |
| 52 | +These models are a good fit for coding workflows because they support large |
| 53 | +context windows. |
| 54 | + |
| 55 | +## Step 2: Create an OpenCode configuration |
| 56 | + |
| 57 | +OpenCode reads configuration from either of these locations: |
| 58 | + |
| 59 | +- `~/.config/opencode/opencode.json` for a global setup |
| 60 | +- `opencode.json` in your project root for a project-specific setup |
| 61 | + |
| 62 | +Project-level configuration overrides the global file. |
| 63 | + |
| 64 | +Add a provider that points to Docker Model Runner: |
| 65 | + |
| 66 | +```json {title="opencode.json"} |
| 67 | +{ |
| 68 | + "$schema": "https://opencode.ai/config.json", |
| 69 | + "provider": { |
| 70 | + "dmr": { |
| 71 | + "npm": "@ai-sdk/openai-compatible", |
| 72 | + "name": "Docker Model Runner", |
| 73 | + "options": { |
| 74 | + "baseURL": "http://localhost:12434/v1" |
| 75 | + }, |
| 76 | + "models": { |
| 77 | + "qwen3-coder": { |
| 78 | + "name": "ai/qwen3-coder" |
| 79 | + }, |
| 80 | + "devstral-small-2": { |
| 81 | + "name": "ai/devstral-small-2" |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +This configuration adds Docker Model Runner as an OpenCode provider and exposes |
| 90 | +two local models. |
| 91 | + |
| 92 | +> [!NOTE] |
| 93 | +> |
| 94 | +> If your setup expects the older OpenAI-compatible path, use |
| 95 | +> `http://localhost:12434/engines/v1` instead. |
| 96 | +
|
| 97 | +## Step 3: Verify the endpoint |
| 98 | + |
| 99 | +Check that Docker Model Runner is reachable before you open OpenCode: |
| 100 | + |
| 101 | +```console |
| 102 | +$ curl http://localhost:12434/v1/models |
| 103 | +``` |
| 104 | + |
| 105 | +If you use the older path, run: |
| 106 | + |
| 107 | +```console |
| 108 | +$ curl http://localhost:12434/engines/v1/models |
| 109 | +``` |
| 110 | + |
| 111 | +The response should list the models available through Docker Model Runner. |
| 112 | + |
| 113 | +## Step 4: Start OpenCode |
| 114 | + |
| 115 | +From your project directory, run: |
| 116 | + |
| 117 | +```console |
| 118 | +$ opencode |
| 119 | +``` |
| 120 | + |
| 121 | +To switch models from the TUI, run: |
| 122 | + |
| 123 | +```text |
| 124 | +/models |
| 125 | +``` |
| 126 | + |
| 127 | +Then select the model from the `dmr` provider. |
| 128 | + |
| 129 | +## Step 5: Package `gpt-oss` with a larger context window |
| 130 | + |
| 131 | +This step is optional. Use it if you need a larger context window for |
| 132 | +repository-scale tasks. |
| 133 | + |
| 134 | +`gpt-oss` defaults to a smaller context window than coding-focused models. If |
| 135 | +you want to use it for repository-scale tasks, package a larger variant: |
| 136 | + |
| 137 | +```console |
| 138 | +$ docker model pull ai/gpt-oss |
| 139 | +$ docker model package --from ai/gpt-oss --context-size 128000 gpt-oss:128k |
| 140 | +``` |
| 141 | + |
| 142 | +Then add it to your OpenCode configuration: |
| 143 | + |
| 144 | +```json {title="opencode.json"} |
| 145 | +{ |
| 146 | + "$schema": "https://opencode.ai/config.json", |
| 147 | + "provider": { |
| 148 | + "dmr": { |
| 149 | + "npm": "@ai-sdk/openai-compatible", |
| 150 | + "name": "Docker Model Runner", |
| 151 | + "options": { |
| 152 | + "baseURL": "http://localhost:12434/v1" |
| 153 | + }, |
| 154 | + "models": { |
| 155 | + "gpt-oss:128k": { |
| 156 | + "name": "gpt-oss:128k" |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## Troubleshooting |
| 165 | + |
| 166 | +If OpenCode can't connect, check Docker Model Runner status: |
| 167 | + |
| 168 | +```console |
| 169 | +$ docker model status |
| 170 | +``` |
| 171 | + |
| 172 | +If OpenCode does not show your model, list local models: |
| 173 | + |
| 174 | +```console |
| 175 | +$ docker model ls |
| 176 | +``` |
| 177 | + |
| 178 | +If the model is missing, pull it first and confirm the model name in |
| 179 | +`opencode.json` matches the local model you want to use. |
| 180 | + |
| 181 | +## Learn more |
| 182 | + |
| 183 | +- [Docker Model Runner overview](../manuals/ai/model-runner/_index.md) |
| 184 | +- [Docker Model Runner API reference](../manuals/ai/model-runner/api-reference.md) |
| 185 | +- [IDE and tool integrations](../manuals/ai/model-runner/ide-integrations.md) |
0 commit comments