keepcalculator

06 Numbers and Powers

Set Mules Ollama API URL in Docker

Card No. 06·68514 · Updated · Standard formula, shown below

This guide shows you how to point MuleSoft’s Mule runtime (running in a Docker container) at a local or remote Ollama API endpoint. You’ll learn the exact environment variables and Docker flags to use, so your Mule flows can call Ollama’s LLM endpoints (like /api/generate or /api/chat) without hardcoding URLs. You’ll need Docker installed and a basic understanding of Mule applications.

What You’ll Need

  • Docker installed on your machine (Docker Desktop or a Linux server).
  • A Mule application that uses the HTTP Connector or a REST client to call Ollama.
  • Ollama running somewhere accessible (e.g., http://localhost:11434 on the host, or a remote server).
  • The Mule runtime version you’re using (e.g., 4.4.x or 4.5.x).

Step-by-Step

  1. Identify the Ollama API URL
    Decide where Ollama is reachable. If it’s on the same host as Docker, use http://host.docker.internal:11434 (on Windows/macOS) or the host’s IP (Linux). If it’s remote, use its public or private IP, e.g., http://192.168.1.50:11434.
  2. Decide how to pass the URL
    You can set it as an environment variable in the container, or as a Mule application property. The cleanest way is to use an environment variable that your Mule config reads via ${env.OLLAMA_API_URL}.
  3. Create or edit your Mule configuration
    In your mule-artifact.properties or global.xml, define a property placeholder. For example, in global.xml:
    <context:property-placeholder location="mule-artifact.properties" />
    Then in your HTTP Request connector, set the URL to ${ollama.api.url}.
  4. Add the property to your application
    In mule-artifact.properties, add:
    ollama.api.url=http://localhost:11434 (this is just a default; you’ll override it at runtime).
  5. Build your Mule application
    Package it as a deployable JAR (e.g., using Maven: mvn clean package). You’ll get a file like mule-app.jar.
  6. Run the Docker container with the environment variable
    Use the -e flag to set the variable. For example:
    docker run -d -p 8081:8081 -e OLLAMA_API_URL=http://host.docker.internal:11434 -v /path/to/app.jar:/opt/mule/apps/app.jar mulesoft/mule:4.4.0
    Replace mulesoft/mule:4.4.0 with your actual image tag.
  7. Reference the environment variable in your Mule config
    In your global.xml or the HTTP Request connector, use ${env.OLLAMA_API_URL} instead of the hardcoded property. For example:
    <http:request-config name="Ollama_HTTP_Request">
      <http:request-connection host="${env.OLLAMA_API_URL}" port="11434" />
    </http:request-config>

    Note: If the URL includes a port, you may need to split it into host and port, or use the full URL in the url attribute of a http:request.
  8. Test the connection
    Once the container is running, call a Mule flow that hits Ollama. Check the logs for errors. If it fails, proceed to the Troubleshooting section.

Common Mistakes

  • Using localhost inside the container – Inside the container, localhost refers to the container itself, not your host. Use host.docker.internal (Windows/macOS) or the host’s IP (Linux).
  • Forgetting to pass the environment variable – If you don’t use -e, the variable won’t exist, and Mule will fail to resolve the placeholder.
  • Mixing up host and port – If your URL is http://192.168.1.50:11434, you can’t put the whole thing in the host attribute of the HTTP Request connector. Split it into host and port, or use the full URL in a http:request operation.
  • Not rebuilding the Mule app – If you change the property placeholder in your Mule config, you must rebuild the JAR and restart the container.

Tips

  • Use a .env file with Docker Compose to manage environment variables neatly.
  • If you’re using Docker Compose, define the environment variable under environment: in your service definition.
  • For security, don’t hardcode API keys or URLs in your Mule app; always use environment variables or a secure property provider.
  • If Ollama is on a different network, ensure firewalls allow traffic on port 11434.
  • Test the URL from inside the container with a simple curl command (if available) before debugging Mule.

Troubleshooting

1. Connection refused
Check if Ollama is actually listening on the expected port. On the host, run curl http://localhost:11434. If it works, check that host.docker.internal resolves correctly. On Linux, you may need to add --add-host=host.docker.internal:host-gateway to your docker run command.

2. Placeholder cannot be resolved
If you see an error like Could not resolve placeholder 'env.OLLAMA_API_URL', it means the environment variable isn’t set. Double-check your docker run command for the -e flag.

3. 404 Not Found
If you get a 404, the URL path is wrong. Ollama’s endpoints are /api/generate and /api/chat. Make sure your HTTP Request connector uses the correct path.

4. Timeout
If the request times out, Ollama might be slow to generate a response. Increase the response timeout in your HTTP Request configuration (e.g., responseTimeout="60000").

5. DNS issues inside container
If host.docker.internal doesn’t work, use the host’s IP address. Find it with ipconfig (Windows) or ifconfig/ip addr (Linux).

Frequently Asked Questions

Q: Can I use http://localhost:11434 if Ollama runs in a separate container?
A: No, unless you use Docker’s host network mode (--network host). Otherwise, use the container’s name or IP.

Q: How do I pass the URL if I’m using Docker Compose?
A: Add an environment: section to your service definition, like:
environment:
- OLLAMA_API_URL=http://host.docker.internal:11434

Q: Do I need to expose port 11434 on the Mule container?
A: No, the Mule container doesn’t need to expose that port; it only needs to connect to Ollama’s port.

Q: What if I want to use a different port for Ollama?
A: Just change the port in the URL. Ollama defaults to 11434, but you can override it with OLLAMA_HOST environment variable on the Ollama side.

Q: Can I set the URL via a Mule property file instead of environment variable?
A: Yes, but then you’d have to bake the URL into the JAR, which is less flexible. Environment variables are recommended for deployment-specific values.

Q: Is there a way to test the connection without a full Mule flow?
A: Yes, you can run a simple curl from inside the container: docker exec <container_id> curl http://host.docker.internal:11434.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →