diff --git a/commands/compose.go b/commands/compose.go index 81e33222..ede66c47 100644 --- a/commands/compose.go +++ b/commands/compose.go @@ -51,9 +51,12 @@ func newUpCommand() *cobra.Command { _ = setenv("URL", "http://model-runner.docker.internal/engines/v1/") } else if kind == desktop.ModelRunnerEngineKindMobyManual { _ = setenv("URL", modelRunner.URL("/engines/v1/")) - } else if kind == desktop.ModelRunnerEngineKindMoby || kind == desktop.ModelRunnerEngineKindCloud { - // TODO: Find a more robust solution in Moby-like environments. - _ = setenv("URL", "http://host.docker.internal:"+strconv.Itoa(standalone.DefaultControllerPort)+"/engines/v1/") + } else if kind == desktop.ModelRunnerEngineKindMoby { + // TODO: Use more robust detection in Moby-like environments. + _ = setenv("URL", "http://host.docker.internal:"+strconv.Itoa(standalone.DefaultControllerPortMoby)+"/engines/v1/") + } else if kind == desktop.ModelRunnerEngineKindCloud { + // TODO: Use more robust detection in Cloud environments. + _ = setenv("URL", "http://host.docker.internal:"+strconv.Itoa(standalone.DefaultControllerPortCloud)+"/engines/v1/") } return nil }, diff --git a/commands/install-runner.go b/commands/install-runner.go index 767a000a..eef5fba8 100644 --- a/commands/install-runner.go +++ b/commands/install-runner.go @@ -96,7 +96,11 @@ func ensureStandaloneRunnerAvailable(ctx context.Context, printer standalone.Sta } // Create the model runner container. - if err := standalone.CreateControllerContainer(ctx, dockerClient, standalone.DefaultControllerPort, gpu, modelStorageVolume, printer); err != nil { + port := uint16(standalone.DefaultControllerPortMoby) + if engineKind == desktop.ModelRunnerEngineKindCloud { + port = standalone.DefaultControllerPortCloud + } + if err := standalone.CreateControllerContainer(ctx, dockerClient, port, gpu, modelStorageVolume, printer); err != nil { return fmt.Errorf("unable to initialize standalone model runner container: %w", err) } @@ -112,18 +116,31 @@ func newInstallRunner() *cobra.Command { Short: "Install Docker Model Runner", RunE: func(cmd *cobra.Command, args []string) error { // Ensure that we're running in a supported model runner context. - if kind := modelRunner.EngineKind(); kind == desktop.ModelRunnerEngineKindDesktop { + engineKind := modelRunner.EngineKind() + if engineKind == desktop.ModelRunnerEngineKindDesktop { // TODO: We may eventually want to auto-forward this to // docker desktop enable model-runner, but we should first make // sure the CLI flags match. cmd.Println("Standalone installation not supported with Docker Desktop") cmd.Println("Use `docker desktop enable model-runner` instead") return nil - } else if kind == desktop.ModelRunnerEngineKindMobyManual { + } else if engineKind == desktop.ModelRunnerEngineKindMobyManual { cmd.Println("Standalone installation not supported with MODEL_RUNNER_HOST set") return nil } + // HACK: If we're in a Cloud context, then we need to use a + // different default port because it conflicts with Docker Desktop's + // default model runner host-side port. Unfortunately we can't make + // the port flag default dynamic (at least not easily) because of + // when context detection happens. So assume that a default value + // indicates that we want the Cloud default port. This is less + // problematic in Cloud since the UX there is mostly invisible. + if engineKind == desktop.ModelRunnerEngineKindCloud && + port == standalone.DefaultControllerPortMoby { + port = standalone.DefaultControllerPortCloud + } + // Create a Docker client for the active context. dockerClient, err := desktop.DockerClientForContext(dockerCLI, dockerCLI.CurrentContext()) if err != nil { @@ -176,7 +193,7 @@ func newInstallRunner() *cobra.Command { }, ValidArgsFunction: completion.NoComplete, } - c.Flags().Uint16Var(&port, "port", standalone.DefaultControllerPort, + c.Flags().Uint16Var(&port, "port", standalone.DefaultControllerPortMoby, "Docker container port for Docker Model Runner") c.Flags().StringVar(&gpuMode, "gpu", "auto", "Specify GPU support (none|auto|cuda)") return c diff --git a/desktop/context.go b/desktop/context.go index bc91bd42..a04cabda 100644 --- a/desktop/context.go +++ b/desktop/context.go @@ -159,14 +159,16 @@ func DetectContext(cli *command.DockerCli) (*ModelRunnerContext, error) { // Compute the URL prefix based on the associated engine kind. var rawURLPrefix string - if kind == ModelRunnerEngineKindMoby || kind == ModelRunnerEngineKindCloud { - rawURLPrefix = "http://localhost:" + strconv.Itoa(int(standalone.DefaultControllerPort)) + if kind == ModelRunnerEngineKindMoby { + rawURLPrefix = "http://localhost:" + strconv.Itoa(standalone.DefaultControllerPortMoby) + } else if kind == ModelRunnerEngineKindCloud { + rawURLPrefix = "http://localhost:" + strconv.Itoa(standalone.DefaultControllerPortCloud) } else if kind == ModelRunnerEngineKindMobyManual { rawURLPrefix = modelRunnerHost } else { // ModelRunnerEngineKindDesktop rawURLPrefix = "http://localhost" + inference.ExperimentalEndpointsPrefix if treatDesktopAsMoby { - rawURLPrefix = "http://localhost:" + strconv.Itoa(int(standalone.DefaultControllerPort)) + rawURLPrefix = "http://localhost:" + strconv.Itoa(standalone.DefaultControllerPortMoby) } } urlPrefix, err := url.Parse(rawURLPrefix) diff --git a/pkg/standalone/ports.go b/pkg/standalone/ports.go index 0c7a99a8..a2f22193 100644 --- a/pkg/standalone/ports.go +++ b/pkg/standalone/ports.go @@ -1,5 +1,10 @@ package standalone -// DefaultControllerPort is the default TCP port on which the standalone -// controller will listen for requests. -const DefaultControllerPort = 12434 +const ( + // DefaultControllerPortMoby is the default TCP port on which the standalone + // controller will listen for requests in Moby environments. + DefaultControllerPortMoby = 12434 + // DefaultControllerPortCloud is the default TCP port on which the + // standalone controller will listen for requests in Moby environments. + DefaultControllerPortCloud = 12435 +)