Files
viettranx 6a1e1b98f6 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.
2026-03-26 12:47:47 +07:00

39 lines
910 B
Go

package config
import (
"os"
"strings"
"sync"
)
var (
dockerOnce sync.Once
dockerCached bool
)
// InDocker returns true when running inside a Docker container.
// Result is cached after the first call.
func InDocker() bool {
dockerOnce.Do(func() {
_, err := os.Stat("/.dockerenv")
dockerCached = err == nil
})
return dockerCached
}
// 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 reference loopback.
func DockerLocalhost(url string) string {
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
}