Skip to content

Commit bd6c827

Browse files
committed
fix(sim-setup): keep Compose overlays across down and bind Ollama to the Docker bridge
1 parent d0285ea commit bd6c827

11 files changed

Lines changed: 223 additions & 37 deletions

File tree

‎apps/docs/content/docs/platform/self-hosting/docker.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ For production, keep this Compose stack and point `OLLAMA_URL` at an Ollama serv
147147
OLLAMA_URL=http://host.docker.internal:11434
148148
```
149149

150-
Production Compose maps `host.docker.internal` to the host gateway, including on Linux. You can instead use a reachable host IP. The Ollama server must listen on an interface reachable from the container.
150+
Production Compose maps `host.docker.internal` to the host gateway, including on Linux. You can instead use a reachable host IP. The Ollama server must listen on an interface reachable from the container. On Linux, bind it to the Docker bridge address that `host.docker.internal` resolves to, usually `OLLAMA_HOST=172.17.0.1:11434` (check with `ip -4 addr show docker0`). Ollama's API has no authentication, so listen on `0.0.0.0` only behind a firewall that blocks port 11434 from outside the host.
151151

152152
```bash
153153
docker compose -f docker-compose.prod.yml up -d

‎apps/docs/content/docs/platform/self-hosting/troubleshooting.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ Inside Docker, `localhost` = the container, not your host machine.
193193
OLLAMA_URL=http://host.docker.internal:11434
194194
```
195195

196+
On Linux, Ollama must also listen where the container reaches the host: bind it to the Docker bridge address, usually `OLLAMA_HOST=172.17.0.1:11434`. Its API has no authentication, so do not bind `0.0.0.0` unless a firewall blocks port 11434 from outside the host.
197+
196198
## LM Studio requests route to Ollama
197199

198200
Sim identifies dynamically discovered LM Studio and vLLM models by their `vllm/` prefix. If the endpoint is unavailable and you manually enter the raw LM Studio model identifier, Sim treats that unknown identifier as an Ollama model.

‎apps/docs/scripts/fixtures/content-baseline.json‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172580,14 +172580,14 @@
172580172580
{
172581172581
"lang": "bash",
172582172582
"meta": null,
172583-
"sha256": "1b5a91e0c8e98301de482bfdac88a0740597f0d7b4f197d2278a49bd62252752",
172583+
"sha256": "f6c25d1f99edeaa5029c6bae30235083b238b80b215b13eb7f3d5e34efabc9ee",
172584172584
"bytes": 22,
172585172585
"line": 28
172586172586
},
172587172587
{
172588172588
"lang": "bash",
172589172589
"meta": null,
172590-
"sha256": "8869220eac65502159ded115c61d4806be247fbdc930b07d52d32dde70f4f106",
172590+
"sha256": "c6e7fb800384d7d5ef95ed9a935fca407d1d5fa60a7d1efb17c5df96c1a82090",
172591172591
"bytes": 62,
172592172592
"line": 34
172593172593
},
@@ -172734,7 +172734,7 @@
172734172734
{
172735172735
"lang": "bash",
172736172736
"meta": null,
172737-
"sha256": "a68c0e57699fd1b6173497de2b48e69da088d4f060252545cc6073e05474d543",
172737+
"sha256": "115d390d2a2ce355d6ecfe6a662b9e2f6e73f285ec4186638f676cb9eb9007c6",
172738172738
"bytes": 14,
172739172739
"line": 20
172740172740
},
@@ -172752,6 +172752,13 @@
172752172752
"bytes": 370,
172753172753
"line": 41
172754172754
},
172755+
{
172756+
"lang": "bash",
172757+
"meta": null,
172758+
"sha256": "ac53018b6ccf14d539980fc8206ce97fadc6a32220a60333cf7a8bef91f27f77",
172759+
"bytes": 196,
172760+
"line": 80
172761+
},
172755172762
{
172756172763
"lang": "bash",
172757172764
"meta": null,
@@ -172860,9 +172867,7 @@
172860172867
{
172861172868
"lang": "bash",
172862172869
"meta": null,
172863-
"sha256": "a5f21e5199143694b8a6b0048a1cad17a98186f489694b49deacc108edf1fa7b",
172864-
"bytes": 21,
172865-
"line": 268
172870+
"sha256": "fed433dfd89f812c99e9b905b5040307b08f5f828e78a6f6aa9322b7302b2264"
172866172871
}
172867172872
],
172868172873
"links": [
@@ -173416,7 +173421,7 @@
173416173421
{
173417173422
"lang": "bash",
173418173423
"meta": null,
173419-
"sha256": "a68c0e57699fd1b6173497de2b48e69da088d4f060252545cc6073e05474d543",
173424+
"sha256": "115d390d2a2ce355d6ecfe6a662b9e2f6e73f285ec4186638f676cb9eb9007c6",
173420173425
"bytes": 14,
173421173426
"line": 24
173422173427
},
@@ -176111,7 +176116,7 @@
176111176116
{
176112176117
"lang": "bash",
176113176118
"meta": null,
176114-
"sha256": "95f834e604341dd3a4a7dbe6d3e03ba145a0ad5be37a545e49cb8c2650d38542",
176119+
"sha256": "28b55f7fe8f07a34e08d95fd5517b6d6bbbb21984c824825fdeaf8e008a8158f",
176115176120
"bytes": 140,
176116176121
"line": 12
176117176122
},
@@ -176556,7 +176561,7 @@
176556176561
{
176557176562
"lang": "bash",
176558176563
"meta": null,
176559-
"sha256": "ad878e0b3ad9eca63a1d754f876211eebdd440c656ee0d3587aaa68ade3700bb",
176564+
"sha256": "8b1b7dd0aa41d3378e82618677b50ff44a7b9cbbe71038d2c1d30b5480c070bc",
176560176565
"bytes": 275,
176561176566
"line": 155
176562176567
},
@@ -176708,7 +176713,7 @@
176708176713
{
176709176714
"lang": "bash",
176710176715
"meta": null,
176711-
"sha256": "697d0449cc0b626ecd35be20d8ce55d1f41160fae7ccdad9af48a57a5b69a4e1",
176716+
"sha256": "76422de41fcefba9fc85682b50cf9b41f5990ec1b095efb82638f80ac1d0bdc7",
176712176717
"bytes": 21,
176713176718
"line": 14
176714176719
},

‎packages/sim-setup/src/checks.test.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
22
import path from 'node:path'
3-
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3+
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
44

5-
const { fakeRoot, mockSpawnSync } = vi.hoisted(() => {
5+
const { fakeRoot, mockSpawnSync, mockHttpHealth } = vi.hoisted(() => {
66
const { mkdtempSync } = require('node:fs') as typeof import('node:fs')
77
const { tmpdir } = require('node:os') as typeof import('node:os')
88
const { join } = require('node:path') as typeof import('node:path')
99
return {
1010
fakeRoot: mkdtempSync(join(tmpdir(), 'sim-setup-checks-')),
1111
mockSpawnSync: vi.fn(() => ({ status: 1, stdout: '', stderr: '' })),
12+
mockHttpHealth: vi.fn(async (_url: string) => false),
1213
}
1314
})
1415

@@ -17,6 +18,11 @@ vi.mock('node:child_process', async (importOriginal) => {
1718
return { ...actual, spawnSync: mockSpawnSync }
1819
})
1920

21+
vi.mock('./probes', async (importOriginal) => {
22+
const actual = await importOriginal<typeof import('./probes')>()
23+
return { ...actual, httpHealth: mockHttpHealth }
24+
})
25+
2026
vi.mock('./context', async (importOriginal) => {
2127
const actual = await importOriginal<typeof import('./context')>()
2228
return { ...actual, SETUP_CONTEXT: { kind: 'source', root: fakeRoot } }
@@ -164,6 +170,26 @@ describe('realtime reachability from the browser', () => {
164170
})
165171
})
166172

173+
describe("doctor probing a Compose install's host Ollama", () => {
174+
async function ollamaFinding() {
175+
const ctx = { ...rootContext({ OLLAMA_URL: 'http://host.docker.internal:11434' }), live: true }
176+
const findings = await runChecks(ctx, ['live'])
177+
return findings.find((finding) => finding.message.toLowerCase().includes('ollama'))
178+
}
179+
180+
it('passes when Ollama answers only on the Docker bridge address', async () => {
181+
mockHttpHealth.mockImplementation(async (url: string) => !url.includes('localhost'))
182+
183+
expect(await ollamaFinding()).toMatchObject({ status: 'pass' })
184+
})
185+
186+
it('warns when Ollama answers on neither loopback nor the bridge', async () => {
187+
mockHttpHealth.mockImplementation(async () => false)
188+
189+
expect(await ollamaFinding()).toMatchObject({ status: 'warn' })
190+
})
191+
})
192+
167193
describe('Compose root .env required keys', () => {
168194
const secrets = [
169195
`BETTER_AUTH_SECRET=${'a'.repeat(32)}`,
@@ -195,7 +221,6 @@ describe('Compose root .env required keys', () => {
195221
findings.filter((finding) => finding.message.includes(key))
196222

197223
beforeEach(() => vi.stubEnv('POSTGRES_PASSWORD', undefined))
198-
afterEach(() => vi.unstubAllEnvs())
199224

200225
it('generates a missing CRON_SECRET, without which the cron service never runs', async () => {
201226
const findings = await schemaFindings(secrets, prodFile)

‎packages/sim-setup/src/checks.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { spawnSync } from 'node:child_process'
22
import { existsSync, readFileSync } from 'node:fs'
3+
import { networkInterfaces } from 'node:os'
34
import path from 'node:path'
45
import {
56
ASYNC_JOBS_CAPABILITY,
@@ -826,28 +827,47 @@ async function checkService(label: string, port: number, url: string): Promise<F
826827
]
827828
}
828829

830+
/** The Docker bridge address Compose's `host-gateway` points at when the host does not report one. */
831+
const DEFAULT_DOCKER_BRIDGE = '172.17.0.1'
832+
833+
/** The host's `docker0` IPv4 address, which `host.docker.internal` resolves to on Linux. */
834+
function dockerBridgeAddress(): string {
835+
const bridge = networkInterfaces().docker0?.find((entry) => entry.family === 'IPv4')
836+
return bridge?.address ?? DEFAULT_DOCKER_BRIDGE
837+
}
838+
829839
/**
830840
* Where doctor, running on the host, probes the Ollama a Compose app reaches
831-
* through `host.docker.internal` — a name the host itself usually cannot resolve.
841+
* through `host.docker.internal` — a name the host itself usually cannot
842+
* resolve. Ollama may listen on loopback (Docker Desktop forwards to it) or,
843+
* as the setup advice recommends on Linux, only on the Docker bridge address.
832844
*/
833-
function hostOllamaUrl(ollamaUrl: string, compose: boolean): string {
834-
if (!compose) return ollamaUrl
845+
function hostOllamaUrls(ollamaUrl: string, compose: boolean): string[] {
846+
if (!compose) return [ollamaUrl]
835847
try {
836848
const url = new URL(ollamaUrl)
837-
if (url.hostname !== 'host.docker.internal') return ollamaUrl
838-
url.hostname = 'localhost'
839-
return url.toString()
849+
if (url.hostname !== 'host.docker.internal') return [ollamaUrl]
850+
return ['localhost', dockerBridgeAddress()].map((host) => {
851+
url.hostname = host
852+
return url.toString()
853+
})
840854
} catch {
841-
return ollamaUrl
855+
return [ollamaUrl]
842856
}
843857
}
844858

845859
async function checkOllama(sim: EnvFile, compose: boolean): Promise<Finding[]> {
846860
const ollamaUrl = sim.vars.get('OLLAMA_URL')
847861
if (!ollamaUrl) return []
848-
const probeUrl = hostOllamaUrl(ollamaUrl, compose)
862+
let reachable = false
863+
for (const probeUrl of hostOllamaUrls(ollamaUrl, compose)) {
864+
if (await httpHealth(`${probeUrl.replace(/\/$/, '')}/api/tags`)) {
865+
reachable = true
866+
break
867+
}
868+
}
849869
return [
850-
(await httpHealth(`${probeUrl.replace(/\/$/, '')}/api/tags`))
870+
reachable
851871
? { group: 'live', status: 'pass', message: 'ollama reachable' }
852872
: {
853873
group: 'live',

‎packages/sim-setup/src/compose-project.test.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
1+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
22
import { tmpdir } from 'node:os'
33
import path from 'node:path'
44
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
@@ -50,6 +50,9 @@ describe('composeFileSet', () => {
5050
})
5151

5252
it('reads COMPOSE_FILE from the install .env with its path separator', () => {
53+
writeFileSync(base, '')
54+
mkdirSync(path.join(dir, 'overlays'))
55+
writeFileSync(path.join(dir, 'overlays/cron.yml'), '')
5356
writeFileSync(
5457
path.join(dir, '.env'),
5558
'COMPOSE_PATH_SEPARATOR=;\nCOMPOSE_FILE=docker-compose.prod.yml;overlays/cron.yml\n'
@@ -65,6 +68,8 @@ describe('composeFileSet', () => {
6568
const posixDelimiter = path.delimiter
6669
Object.assign(path, { delimiter: ';' })
6770
try {
71+
writeFileSync(base, '')
72+
writeFileSync(path.join(dir, 'cron.yml'), '')
6873
writeFileSync(path.join(dir, '.env'), 'COMPOSE_FILE=docker-compose.prod.yml;cron.yml\n')
6974

7075
expect(composeFileSet({ baseFile: base, dir }).files).toEqual([
@@ -76,7 +81,19 @@ describe('composeFileSet', () => {
7681
}
7782
})
7883

84+
it('leaves out a COMPOSE_FILE entry that no longer exists and says so', () => {
85+
writeFileSync(base, '')
86+
writeFileSync(path.join(dir, '.env'), 'COMPOSE_FILE=docker-compose.prod.yml:gone.yml\n')
87+
88+
const { files, warnings } = composeFileSet({ baseFile: base, dir })
89+
90+
expect(files).toEqual([base])
91+
expect(warnings).toEqual([expect.stringContaining(path.join(dir, 'gone.yml'))])
92+
})
93+
7994
it('ignores a COMPOSE_FILE that does not include the base file', () => {
95+
writeFileSync(path.join(dir, 'docker-compose.local.yml'), '')
96+
writeFileSync(path.join(dir, 'docker-compose.ollama.yml'), '')
8097
writeFileSync(
8198
path.join(dir, '.env'),
8299
'COMPOSE_FILE=docker-compose.local.yml:docker-compose.ollama.yml\n'
@@ -85,7 +102,9 @@ describe('composeFileSet', () => {
85102
const { files, warnings } = composeFileSet({ baseFile: base, dir })
86103

87104
expect(files).toEqual([base])
88-
expect(warnings).toEqual([expect.stringContaining('does not include docker-compose.prod.yml')])
105+
expect(warnings).toEqual([
106+
expect.stringContaining('does not include an existing docker-compose.prod.yml'),
107+
])
89108
})
90109

91110
it('adds docker-compose.override.yml only when it exists', () => {

‎packages/sim-setup/src/compose-project.ts‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ export interface ComposeFileSet {
4040
warnings: string[]
4141
}
4242

43+
/** Keeps the files still on disk, recording a warning for each one that is gone. */
44+
function existingFiles(files: readonly string[], origin: string, warnings: string[]): string[] {
45+
const present = files.filter((file) => existsSync(file))
46+
for (const file of files) {
47+
if (!present.includes(file))
48+
warnings.push(`${file} ${origin} but no longer exists; leaving it out`)
49+
}
50+
return present
51+
}
52+
4353
/**
4454
* The Compose files every operation on an install must pass, in order. Passing
4555
* `-f` turns off Compose's own `COMPOSE_FILE` and override-file lookup, so a
@@ -66,15 +76,8 @@ export function composeFileSet({
6676
const base = path.resolve(baseFile)
6777
const warnings: string[] = []
6878

69-
const surviving = recordedFiles.filter((file) => existsSync(file))
70-
for (const file of recordedFiles) {
71-
if (!surviving.includes(file)) {
72-
warnings.push(`${file} was recorded for this stack but no longer exists; leaving it out`)
73-
}
74-
}
75-
if (surviving.some((file) => path.resolve(file) === base)) {
76-
return { files: surviving, warnings }
77-
}
79+
const recorded = existingFiles(recordedFiles, 'was recorded for this stack', warnings)
80+
if (recorded.some((file) => path.resolve(file) === base)) return { files: recorded, warnings }
7881

7982
const envPath = path.join(dir, '.env')
8083
const env = existsSync(envPath)
@@ -83,21 +86,36 @@ export function composeFileSet({
8386
const configured = env.get('COMPOSE_FILE')
8487
if (configured) {
8588
const separator = env.get('COMPOSE_PATH_SEPARATOR') || path.delimiter
86-
const files = configured
89+
const listed = configured
8790
.split(separator)
8891
.map((entry) => entry.trim())
8992
.filter(Boolean)
9093
.map((entry) => path.resolve(dir, entry))
94+
const files = existingFiles(listed, `is listed in COMPOSE_FILE in ${envPath}`, warnings)
9195
if (files.includes(base)) return { files, warnings }
9296
warnings.push(
93-
`COMPOSE_FILE in ${envPath} does not include ${path.basename(baseFile)}; ignoring it`
97+
`COMPOSE_FILE in ${envPath} does not include an existing ${path.basename(baseFile)}; ignoring it`
9498
)
9599
}
96100

97101
const override = path.join(dir, COMPOSE_OVERRIDE_FILE)
98102
return { files: existsSync(override) ? [baseFile, override] : [baseFile], warnings }
99103
}
100104

105+
/**
106+
* The `COMPOSE_FILE` value that reproduces `files` for the install in `dir`:
107+
* paths relative to it, joined with `separator` (the install's
108+
* `COMPOSE_PATH_SEPARATOR`, else the OS path-list separator Compose defaults to).
109+
* Saving it keeps overlays once `docker compose down` erases the record of them.
110+
*/
111+
export function composeFileEnvValue(
112+
files: readonly string[],
113+
dir: string,
114+
separator: string = path.delimiter
115+
): string {
116+
return files.map((file) => path.relative(dir, file) || '.').join(separator)
117+
}
118+
101119
/** Expands a Compose file set into its `-f` arguments. */
102120
export function composeFileArgs(files: readonly string[]): string[] {
103121
return files.flatMap((file) => ['-f', file])

0 commit comments

Comments
 (0)