11package desktop
22
33import (
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