Skip to content

Commit

Permalink
fix(cli): unable to use CLI within the container
Browse files Browse the repository at this point in the history
In the container, `OLLAMA_HOST` is set by default to `0.0.0.0` (ref: [Dockerfile#L137]), which is fine when starting the server. However, as a client, it is necessary to use `127.0.0.1` or `localhost` for requests.

fix: #3521 #1337
maybe fix: #3526

[Dockerfile#L137]: https://github.com/ollama/ollama/blob/7e432cdfac51583459e7bfa8fdd485c74a6597e7/Dockerfile#L137

Signed-off-by: Kevin Cui <bh@bugs.cc>
  • Loading branch information
BlackHole1 committed Apr 29, 2024
1 parent 7e432cd commit 7ea53b4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ func ClientFromEnvironment() (*Client, error) {
}
}

// 0.0.0.0 => 127.0.0.1
if ip := net.ParseIP(host); ip != nil {
if ip.IsUnspecified() {
host = "127.0.0.1"
}
}

return &Client{
base: &url.URL{
Scheme: scheme,
Expand Down
2 changes: 2 additions & 0 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func TestClientFromEnvironment(t *testing.T) {
"scheme, hostname, and port": {value: "https://example.com:1234", expect: "https://example.com:1234"},
"trailing slash": {value: "example.com/", expect: "http://example.com:11434"},
"trailing slash port": {value: "example.com:1234/", expect: "http://example.com:1234"},
"unspecified ipv4 host": {value: "0.0.0.0:1234", expect: "http://127.0.0.1:1234"},
"unspecified ipv6 host": {value: "[::]:1234", expect: "http://127.0.0.1:1234"},
}

for k, v := range testCases {
Expand Down

0 comments on commit 7ea53b4

Please sign in to comment.