Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions cmd/cli/commands/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ measuring the tokens per second (TPS) that the model can generate.`,
return handleClientError(err, "Failed to inspect model")
}

// Ensure model runner is available
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}

if !jsonOutput {
fmt.Printf("Prompt: %s\n", prompt)
fmt.Printf("Duration: %v per concurrency level\n", duration)
Expand Down
12 changes: 6 additions & 6 deletions cmd/cli/commands/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func newUpCommand() *cobra.Command {

sendInfo("Initializing model runner...")
kind := modelRunner.EngineKind()
standalone, err := ensureStandaloneRunnerAvailable(cmd.Context(), nil, false)
runner, err := getStandaloneRunner(cmd.Context())
if err != nil {
_ = sendErrorf("Failed to initialize standalone model runner: %v", err)
return fmt.Errorf("Failed to initialize standalone model runner: %w", err)
_ = sendErrorf("Failed to get standalone model runner info: %v", err)
return fmt.Errorf("Failed to get standalone model runner info: %w", err)
} else if ((kind == types.ModelRunnerEngineKindMoby || kind == types.ModelRunnerEngineKindCloud) &&
standalone == nil) ||
(standalone != nil && (standalone.gatewayIP == "" || standalone.gatewayPort == 0)) {
runner == nil) ||
(runner != nil && (runner.gatewayIP == "" || runner.gatewayPort == 0)) {
return errors.New("unable to determine standalone runner endpoint")
}

Expand Down Expand Up @@ -110,7 +110,7 @@ func newUpCommand() *cobra.Command {
case types.ModelRunnerEngineKindCloud:
fallthrough
case types.ModelRunnerEngineKindMoby:
_ = setenv("URL", "http://"+net.JoinHostPort(standalone.gatewayIP, strconv.Itoa(int(standalone.gatewayPort)))+"/engines/v1/")
_ = setenv("URL", "http://"+net.JoinHostPort(runner.gatewayIP, strconv.Itoa(int(runner.gatewayPort)))+"/engines/v1/")
default:
return fmt.Errorf("unhandled engine kind: %v", kind)
}
Expand Down
3 changes: 0 additions & 3 deletions cmd/cli/commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ func newInspectCmd() *cobra.Command {
Short: "Display detailed information on one model",
Args: requireExactArgs(1, "inspect", "MODEL"),
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}
if openai && remote {
return fmt.Errorf("--remote flag cannot be used with --openai flag")
}
Expand Down
47 changes: 47 additions & 0 deletions cmd/cli/commands/install-runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,53 @@ func ensureStandaloneRunnerAvailable(ctx context.Context, printer standalone.Sta
return inspectStandaloneRunner(container), nil
}

// withStandaloneRunner wraps a command's RunE to ensure the standalone runner
// is available before executing the command. This is a no-op in unsupported
// contexts (e.g., Docker Desktop) or if automatic installations have been disabled.
func withStandaloneRunner(cmd *cobra.Command) *cobra.Command {
if cmd.RunE == nil {
return cmd
}
originalRunE := cmd.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}
return originalRunE(cmd, args)
}
return cmd
}

// getStandaloneRunner returns the standalone runner info by finding the controller container.
// This is useful for commands that need runner details after withStandaloneRunner has run.
// Returns nil for non-standalone contexts (e.g., Docker Desktop).
func getStandaloneRunner(ctx context.Context) (*standaloneRunner, error) {
// Only standalone contexts have a runner container to inspect.
engineKind := modelRunner.EngineKind()
standaloneSupported := engineKind == types.ModelRunnerEngineKindMoby ||
engineKind == types.ModelRunnerEngineKindCloud
if !standaloneSupported {
return nil, nil
}

if dockerCLI == nil {
return nil, nil
}

dockerClient, err := desktop.DockerClientForContext(dockerCLI, dockerCLI.CurrentContext())
if err != nil {
return nil, fmt.Errorf("failed to create Docker client: %w", err)
}
containerID, _, ctr, err := standalone.FindControllerContainer(ctx, dockerClient)
if err != nil {
return nil, fmt.Errorf("unable to find standalone model runner: %w", err)
}
if containerID == "" {
return nil, nil
}
return inspectStandaloneRunner(ctr), nil
}

// runnerOptions holds common configuration for install/start/reinstall commands
type runnerOptions struct {
port uint16
Expand Down
6 changes: 0 additions & 6 deletions cmd/cli/commands/pull.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package commands

import (
"fmt"

"github.com/docker/model-runner/cmd/cli/commands/completion"
"github.com/docker/model-runner/cmd/cli/desktop"

"github.com/spf13/cobra"
)

Expand All @@ -15,9 +12,6 @@ func newPullCmd() *cobra.Command {
Short: "Pull a model from Docker Hub or HuggingFace to your local environment",
Args: requireExactArgs(1, "pull", "MODEL"),
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}
return pullModel(cmd, desktopClient, args[0])
},
ValidArgsFunction: completion.NoComplete,
Expand Down
6 changes: 0 additions & 6 deletions cmd/cli/commands/push.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package commands

import (
"fmt"

"github.com/docker/model-runner/cmd/cli/commands/completion"
"github.com/docker/model-runner/cmd/cli/desktop"

"github.com/spf13/cobra"
)

Expand All @@ -15,9 +12,6 @@ func newPushCmd() *cobra.Command {
Short: "Push a model to Docker Hub",
Args: requireExactArgs(1, "push", "MODEL"),
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}
return pushModel(cmd, desktopClient, args[0])
},
ValidArgsFunction: completion.NoComplete,
Expand Down
4 changes: 0 additions & 4 deletions cmd/cli/commands/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ func newRequestsCmd() *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}

responseBody, cancel, err := desktopClient.Requests(model, follow, includeExisting)
if err != nil {
errMsg := "Failed to get requests"
Expand Down
6 changes: 0 additions & 6 deletions cmd/cli/commands/rm.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package commands

import (
"fmt"

"github.com/docker/model-runner/cmd/cli/commands/completion"

"github.com/spf13/cobra"
)

Expand All @@ -16,9 +13,6 @@ func newRemoveCmd() *cobra.Command {
Short: "Remove local models downloaded from Docker Hub",
Args: requireMinArgs(1, "rm", "[MODEL...]"),
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}
response, err := desktopClient.Remove(args, force)
if response != "" {
cmd.Print(response)
Expand Down
27 changes: 18 additions & 9 deletions cmd/cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,42 @@ func NewRootCmd(cli *command.DockerCli) *cobra.Command {
globalOptions.InstallFlags(rootCmd.Flags())
}

// Add subcommands.
// Runner management commands - these manage the runner itself and don't need automatic runner initialization.
rootCmd.AddCommand(
newVersionCmd(),
newInstallRunner(),
newUninstallRunner(),
newStartRunner(),
newStopRunner(),
newRestartRunner(),
newReinstallRunner(),
)

// Commands that require a running model runner. These are wrapped to ensure the standalone runner is available.
for _, cmd := range []*cobra.Command{
newStatusCmd(),
newPullCmd(),
newPushCmd(),
newPackagedCmd(),
newListCmd(),
newLogsCmd(),
newRunCmd(),
newRemoveCmd(),
newInspectCmd(),
newComposeCmd(),
newTagCmd(),
newInstallRunner(),
newUninstallRunner(),
newStartRunner(),
newStopRunner(),
newRestartRunner(),
newReinstallRunner(),
newConfigureCmd(),
newPSCmd(),
newDFCmd(),
newUnloadCmd(),
newRequestsCmd(),
newPurgeCmd(),
newBenchCmd(),
)
} {
rootCmd.AddCommand(withStandaloneRunner(cmd))
}

// run command handles standalone runner initialization itself (needs debug flag)
rootCmd.AddCommand(newRunCmd())

return rootCmd
}
8 changes: 4 additions & 4 deletions cmd/cli/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ func newRunCmd() *cobra.Command {
}
},
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), debug); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}

model := args[0]
prompt := ""
argsLen := len(args)
Expand Down Expand Up @@ -675,10 +679,6 @@ func newRunCmd() *cobra.Command {
return nil
}

if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), debug); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}

_, err := desktopClient.Inspect(model, false)
if err != nil {
if !errors.Is(err, desktop.ErrNotFound) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/commands/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func newStatusCmd() *cobra.Command {
Use: "status",
Short: "Check if the Docker Model Runner is running",
RunE: func(cmd *cobra.Command, args []string) error {
runner, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false)
runner, err := getStandaloneRunner(cmd.Context())
if err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
return fmt.Errorf("unable to get standalone model runner info: %w", err)
}
status := desktopClient.Status()
if status.Error != nil {
Expand Down
3 changes: 0 additions & 3 deletions cmd/cli/commands/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ func newTagCmd() *cobra.Command {
Short: "Tag a model",
Args: requireExactArgs(2, "tag", "SOURCE TARGET"),
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false); err != nil {
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
}
return tagModel(cmd, desktopClient, args[0], args[1])
},
ValidArgsFunction: completion.ModelNames(getDesktopClient, 1),
Expand Down
Loading