fix(providers): handle 127.0.0.1 in Docker rewrite and add Ollama host fallback

DockerLocalhost() now rewrites both localhost and 127.0.0.1 to
host.docker.internal. Ollama registerInMemory() defaults to
http://localhost:11434/v1 when APIBase is empty, matching startup code.
This commit is contained in:
viettranx committed 2026-03-26 12:47:47 +07:00
1 parent 19498bff79
commit 6a1e1b98f6
2 files changed
+15 -5

No files matched your search

+9 -3
View File
@@ -21,12 +21,18 @@ func InDocker() bool {
return dockerCached
}
// DockerLocalhost rewrites localhost in url to host.docker.internal
// DockerLocalhost rewrites localhost or 127.0.0.1 in url to host.docker.internal
// when running inside Docker, so the container can reach host services.
// Returns the url unchanged when not in Docker or when it doesn't contain localhost.
// Returns the url unchanged when not in Docker or when it doesn't reference loopback.
func DockerLocalhost(url string) string {
if InDocker() && strings.Contains(url, "localhost") {
if !InDocker() {
return url
}
if strings.Contains(url, "localhost") {
return strings.Replace(url, "localhost", "host.docker.internal", 1)
}
if strings.Contains(url, "127.0.0.1") {
return strings.Replace(url, "127.0.0.1", "host.docker.internal", 1)
}
return url
}
+6 -2
View File
@@ -142,10 +142,14 @@ func (h *ProvidersHandler) registerInMemory(p *store.LLMProviderData) {
h.providerReg.RegisterForTenant(p.TenantID, providers.NewClaudeCLIProvider(cliPath, cliOpts...))
return
}
// Ollama doesn't need an API key — inject a dummy value like startup does.
// Ollama doesn't need an API key — handle before the key guard (same as startup).
// In Docker, swap localhost → host.docker.internal so the container can reach the host.
if p.ProviderType == store.ProviderOllama {
h.providerReg.RegisterForTenant(p.TenantID, providers.NewOpenAIProvider(p.Name, "ollama", config.DockerLocalhost(p.APIBase), "llama3.3"))
host := p.APIBase
if host == "" {
host = "http://localhost:11434"
}
h.providerReg.RegisterForTenant(p.TenantID, providers.NewOpenAIProvider(p.Name, "ollama", config.DockerLocalhost(host+"/v1"), "llama3.3"))
return
}
if p.APIKey == "" {