Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit 7756ae4

Browse files
authored
Merge pull request #86 from doringeman/desktop-context
context: Detect DD based on the OS and allow WSL2 client
2 parents ba13a62 + bcc528e commit 7756ae4

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

commands/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func NewRootCmd(cli *command.DockerCli) *cobra.Command {
6666

6767
// Detect the model runner context and create a client for it.
6868
var err error
69-
modelRunner, err = desktop.DetectContext(dockerCLI)
69+
modelRunner, err = desktop.DetectContext(cmd.Context(), dockerCLI)
7070
if err != nil {
7171
return fmt.Errorf("unable to detect model runner context: %w", err)
7272
}

desktop/context.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package desktop
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"net/url"
78
"os"
89
"runtime"
910
"strconv"
1011
"strings"
12+
"time"
1113

1214
"github.com/docker/cli/cli/command"
1315
"github.com/docker/cli/cli/context/docker"
@@ -18,11 +20,20 @@ import (
1820

1921
// isDesktopContext returns true if the CLI instance points to a Docker Desktop
2022
// context and false otherwise.
21-
func isDesktopContext(cli *command.DockerCli) bool {
23+
func isDesktopContext(ctx context.Context, cli *command.DockerCli) bool {
24+
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
25+
defer cancel()
26+
serverInfo, _ := cli.Client().Info(ctx)
27+
2228
// We don't currently support Docker Model Runner in Docker Desktop for
2329
// Linux, so we won't treat that as a Docker Desktop case (though it will
2430
// still work as a standard Moby or Cloud case, depending on configuration).
2531
if runtime.GOOS == "linux" {
32+
if strings.Contains(serverInfo.KernelVersion, "-microsoft-standard-WSL2") {
33+
// We can use Docker Desktop from within a WSL2 integrated distro.
34+
// https://github.com/search?q=repo%3Amicrosoft%2FWSL2-Linux-Kernel+path%3A%2F%5Earch%5C%2F.*%5C%2Fconfigs%5C%2Fconfig-wsl%2F+CONFIG_LOCALVERSION&type=code
35+
return true
36+
}
2637
return false
2738
}
2839

@@ -32,18 +43,8 @@ func isDesktopContext(cli *command.DockerCli) bool {
3243
return false
3344
}
3445

35-
// Otherwise used name-based heuristics to identify the environment.
36-
name := cli.CurrentContext()
37-
if name == "desktop-linux" {
38-
return true
39-
} else if name == "default" && os.Getenv("DOCKER_HOST") == "" {
40-
return true
41-
} else if runtime.GOOS == "windows" && name == "desktop-windows" {
42-
// On Windows, we'll still target the Linux engine, even if the Windows
43-
// engine is currently active.
44-
return true
45-
}
46-
return false
46+
// docker run -it --rm --privileged --pid=host justincormack/nsenter1 /bin/sh -c 'cat /etc/os-release'
47+
return serverInfo.OperatingSystem == "Docker Desktop"
4748
}
4849

4950
// isCloudContext returns true if the CLI instance points to a Docker Cloud
@@ -136,7 +137,7 @@ func NewContextForMock(client DockerHttpClient) *ModelRunnerContext {
136137
}
137138

138139
// DetectContext determines the current Docker Model Runner context.
139-
func DetectContext(cli *command.DockerCli) (*ModelRunnerContext, error) {
140+
func DetectContext(ctx context.Context, cli *command.DockerCli) (*ModelRunnerContext, error) {
140141
// Check for an explicit endpoint setting.
141142
modelRunnerHost := os.Getenv("MODEL_RUNNER_HOST")
142143

@@ -148,7 +149,7 @@ func DetectContext(cli *command.DockerCli) (*ModelRunnerContext, error) {
148149
kind := ModelRunnerEngineKindMoby
149150
if modelRunnerHost != "" {
150151
kind = ModelRunnerEngineKindMobyManual
151-
} else if isDesktopContext(cli) {
152+
} else if isDesktopContext(ctx, cli) {
152153
kind = ModelRunnerEngineKindDesktop
153154
if treatDesktopAsMoby {
154155
kind = ModelRunnerEngineKindMoby

0 commit comments

Comments
 (0)