Docker Model Runner (DMR) Documentation

repository·main·Indexed 20 days ago

https://github.com/docker/model-runner

A toolset for managing, running, and deploying AI models (such as LLMs) using Docker. It provides a unified interface via a Docker CLI plugin or a standalone binary (dmr) to pull and serve models from OCI-compliant registries. The toolset supports deployment on Docker Desktop and Kubernetes clusters via Helm, offers GPU support for NVIDIA and AMD, and includes capabilities for packaging GGUF models into OCI artifacts and integrating with Open WebUI.

Tokens
87.9K
Snippets
349
Records
443
Agent score
69%

What's inside Docker Model Runner

  1. Use the docker model CLI

    main

    The docker model command is the entrypoint for the Docker Model Runner CLI. It allows you to run, manage, and interact with AI models directly from your command line. You can use it to pull models from Docker Hub or HuggingFace, run them in chat mode, benchmark performance, and manage the Model Runner lifecycle (start, stop, restart, install, uninstall).

    docker model <subcommand> [options]
  2. Understand the model-cli gateway

    main

    The model-cli gateway is a lightweight, OpenAI-compatible LLM proxy designed to sit in front of Docker Model Runner (DMR) or other providers. It provides several enterprise-grade features including:

    • Routing: Mapping client-side model names to specific provider models.
    • Load Balancing: Distributing requests across multiple deployments using round-robin.
    • Retries & Fallbacks: Automatically retrying failed requests or switching to a different model if the primary one fails.
    • Authentication: Requiring a bearer token for all requests.
    • OpenAI Compatibility: Supporting standard OpenAI API endpoints like /v1/chat/completions, /v1/models, and /v1/embeddings.
  3. Best practices for Docker Model Runner

    main

    To optimize your development workflow with Docker Model Runner:

    • Performance: Use smaller models (e.g., ai/smollm2) for faster response times during development.
    • Scripting: Use the --detach flag when running models to pre-load them in the background, which is better for automated scripts.
    • Model Lifecycle: Models remain loaded in memory until either a different model is requested or a 5-minute timeout occurs.
    • Integration: Prefer the OpenAI-compatible API for integrating local models with existing tools and libraries.
  4. How the dmr architecture works

    main

    The dmr tool is designed to be engine-independent by bypassing the standard Docker Engine connection logic used by the docker model CLI plugin.

    Key architectural components:

    • Daemon: dmr serve runs the pkg/server in-process. It listens on TCP port 12434 by default, or a Unix socket if specified via the --socket flag.
    • Communication: The dmr CLI communicates with its daemon via the MODEL_RUNNER_HOST environment variable (which defaults to http://localhost:12434). This forces the "manual host" code path, ensuring it does not attempt to probe a Docker Engine or Docker Desktop.
    • Command Parity: Every subcommand in dmr matches the docker model CLI plugin command tree, providing full feature parity for operations like run, pull, push, tag, inspect, logs, bench, and configure.
  5. Configure model fallbacks in Docker Model Gateway

    main

    You can define fallback behavior in the general_settings section of your configuration file. This allows the gateway to automatically route a request to a secondary provider if the primary one fails.

    Example Configuration: In this setup, if fast fails, it tries local. If smart fails, it tries fast, and if that fails, it tries local.

    model_list:
      - model_name: fast
        params:
          model: groq/llama-3.1-8b-instant
          api_key: os.environ/GROQ_API_KEY
      - model_name: smart
        params:
          model: openai/gpt-4o
          api_key: os.environ/OPENAI_API_KEY
      - model_name: local
        params:
          model: docker_model_runner/ai/smollm2
          api_base: http://localhost:12434/engines/llama.cpp/v1
    
    general_settings:
      num_retries: 2
      fallbacks:
        - fast: [local]
        - smart: [fast, local]
    general_settings:
      num_retries: 2
      fallbacks:
        - fast: [local]
        - smart: [fast, local]
  6. Understand the Model Distribution Architecture

    main

    The distribution package uses a layered interface hierarchy to separate low-level storage from high-level inference. This allows the system to support different storage backends (like OCI registries) and different model formats (like Docker or CNCF ModelPack) while providing a consistent interface for inference runtimes.

    The Hierarchy

    1. oci.Image: The foundation. Handles low-level OCI artifact operations (layers, manifests, digests) for registry storage.
    2. types.ModelArtifact: Extends oci.Image. Used for the building and pushing phases of a model's lifecycle.
    3. partial.BaseModel: A common implementation used to bridge artifacts to usable models by managing layer lists and configuration files.
    4. types.Model: Represents a stored model. It is used to resolve file paths (GGUF, Safetensors, etc.) for inference.
    5. types.ModelBundle: The final stage. Represents an unpacked model ready for immediate execution by a runtime.
    ┌─────────────────────────────────────────────────────────────────┐
    │                         oci.Image                                │
    │   (Low-level OCI artifact: Layers, Manifest, Digest, etc.)      │
    └───────────────────────────────┬─────────────────────────────────┘
                                    │ embeds
                                    ▼
    ┌─────────────────────────────────────────────────────────────────┐
    │                      types.ModelArtifact                        │
    │         (Building & pushing: ID, Config, Descriptor)            │
    └───────────────────────────────┬─────────────────────────────────┘
                                    │ implemented by
                                    ▼
    ┌─────────────────────────────────────────────────────────────────┐
    │                      partial.BaseModel                          │
    │        (Common implementation with LayerList, ConfigFile)       │
    └─────────────────────────────────────────────────────────────────┘
  7. Understand the aggregated metrics format and labels

    main

    The /metrics endpoint aggregates metrics from all active llama.cpp runners. While metrics retain their original names and types, the model-runner adds three specific labels to every metric to allow for granular filtering in Prometheus:

    • backend: The inference backend (e.g., llama.cpp).
    • model: The specific model name (e.g., llama3.2:latest).
    • mode: The operation mode, which is either completion or embedding.

    Example Output

    # HELP llama_prompt_tokens_total Total number of prompt tokens processed
    # TYPE llama_prompt_tokens_total counter
    llama_prompt_tokens_total{backend="llama.cpp",model="llama3.2:latest",mode="completion"} 4934
    llama_prompt_tokens_total{backend="llama.cpp",model="ai/mxbai-embed-large:335M-F16",mode="embedding"} 4525
    
    # HELP llama_generation_tokens_total Total number of tokens generated
    # TYPE llama_generation_tokens_total counter
    llama_generation_tokens_total{backend="llama.cpp",model="llama3.2:latest",mode="completion"} 2156
    
    # HELP llama_requests_total Total number of requests processed
    # TYPE llama_requests_total counter
    llama_requests_total{backend="llama.cpp",model="llama3.2:latest",mode="completion"} 127
    llama_requests_total{backend="llama.cpp",model="ai/mxbai-embed-large:335M-F16",mode="embedding"} 89
    # HELP llama_prompt_tokens_total Total number of prompt tokens processed
    # TYPE llama_prompt_tokens_total counter
    llama_prompt_tokens_total{backend="llama.cpp",model="llama3.2:latest",mode="completion"} 4934
  8. Quickstart: Deploy Docker Model Runner on Docker Desktop

    main

    To deploy Docker Model Runner on Docker Desktop, apply the provided static manifest and wait for the deployment to become available. Once ready, you can run a model by setting the MODEL_RUNNER_HOST environment variable to point to your local instance.

    kubectl apply -f static/docker-model-runner-desktop.yaml
    kubectl wait --for=condition=Available deployment/docker-model-runner --timeout=5m
    MODEL_RUNNER_HOST=http://localhost:31245 docker model run ai/smollm2:latest
  9. Quickstart: Deploy Docker Model Runner on any Kubernetes Cluster

    main

    To deploy on a standard Kubernetes cluster, apply the docker-model-runner.yaml manifest and use kubectl port-forward to expose the deployment to your local machine. After forwarding, set MODEL_RUNNER_HOST to access the runner via the CLI.

    kubectl apply -f static/docker-model-runner.yaml
    kubectl wait --for=condition=Available deployment/docker-model-runner --timeout=5m
    kubectl port-forward deployment/docker-model-runner 31245:12434
    
    # Run a model
    MODEL_RUNNER_HOST=http://localhost:31245 docker model run ai/smollm2:latest
  10. Build the Docker Model CLI from source

    main

    If you are developing or building the CLI manually, follow these steps:

    1. Clone the repository.
    2. Run make build to compile the binary.
    3. Install the runner using the newly built binary.
    git clone https://github.com/docker/model-cli.git
    cd model-cli
    make build
    ./model-cli install-runner
  11. Cross-compile dmr binaries locally

    main

    You can build all published target binaries for dmr locally using make. Since dmr has no cgo dependencies, all targets can be cross-compiled from any host. The resulting binaries are placed in dist/dmr/<os>-<arch>/dmr.

    make build-dmr-cross