[Docker] Cannot connect from LemonAI container to Ollama running on host machine
环境:lemon ai通过docker环境部署,ollma是部署在本地电脑,非docker环境,试了http://localhost:11434/v1,或者那个http://host.docker.internal:11434/v1,http://localhost:11434/v1/chat/completions,都不行请问应该使用哪个网址?
Hello! This is a classic Docker networking challenge.
The core of the problem is that localhost (or 127.0.0.1) inside a Docker container refers to the container itself, not your host machine (your local PC).
You were correct to try http://host.docker.internal:11434 (which is the special DNS name for the host machine from within a Docker container).
The reason this likely failed is that, by default, Ollama only binds to 127.0.0.1 for security. This means it only accepts connections originating from your host machine, and it will reject the connection coming from the Docker container (even via host.docker.internal).
The Solution
To fix this, you need to configure Ollama to listen on all network interfaces (0.0.0.0).
-
Stop your local Ollama service.
-
Restart Ollama with the
OLLAMA_HOSTenvironment variable set. If you are running it from the terminal, use this command:OLLAMA_HOST=0.0.0.0 ollama serve(If you are using the Ollama desktop application, you may need to set this environment variable for your system and restart the application.)
-
Configure LemonAI's URL. Now that Ollama is listening for external connections, you should set the URL in your LemonAI configuration to:
http://host.docker.internal:11434/v1- Note: This is typically the Base URL. The application (LemonAI) will append specific endpoints like
/chat/completionsitself. The URLhttp://host.docker.internal:11434/v1/chat/completionsis an endpoint, not the base URL.
- Note: This is typically the Base URL. The application (LemonAI) will append specific endpoints like
This configuration allows the Docker container (LemonAI) to correctly find and connect to the Ollama service running on your host machine.
Hope this helps!