Skip to content

Commit c421c14

Browse files
authored
Merge pull request #3784 from crazy-max/fix-container-wsl-local
driver: only mount WSL libraries for local docker-container endpoints
2 parents d61060e + 48fd4cb commit c421c14

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

builder/node.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []N
122122
Name: driver.BuilderName(n.Name),
123123
EndpointAddr: n.Endpoint,
124124
DockerAPI: dockerapi,
125-
DockerContext: b.opts.dockerCli.CurrentContext(),
126125
ContextStore: b.opts.dockerCli.ContextStore(),
127126
BuildkitdFlags: n.BuildkitdFlags,
128127
Files: n.Files,

driver/docker-container/driver.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/docker/buildx/util/imagetools"
2121
"github.com/docker/buildx/util/progress"
2222
"github.com/docker/cli/cli/context/docker"
23+
contextstore "github.com/docker/cli/cli/context/store"
2324
"github.com/docker/cli/opts"
2425
"github.com/moby/buildkit/client"
2526
mobyarchive "github.com/moby/go-archive"
@@ -153,19 +154,15 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
153154
// is a local socket as requesting GPU on container builder creation
154155
// is not enough when generating the CDI specification for GPU devices.
155156
// https://github.com/docker/buildx/pull/3320
156-
if os.Getenv("WSL_DISTRO_NAME") != "" {
157-
if cm, err := d.ContextStore.GetMetadata(d.DockerContext); err == nil {
158-
if epm, err := docker.EndpointFromContext(cm); err == nil && isSocket(epm.Host) {
159-
wslLibPath := "/usr/lib/wsl"
160-
if st, err := os.Stat(wslLibPath); err == nil && st.IsDir() {
161-
mounts = append(mounts, mount.Mount{
162-
Type: mount.TypeBind,
163-
Source: wslLibPath,
164-
Target: wslLibPath,
165-
ReadOnly: true,
166-
})
167-
}
168-
}
157+
if os.Getenv("WSL_DISTRO_NAME") != "" && hasLocalSocketEndpoint(d.EndpointAddr, d.ContextStore) {
158+
wslLibPath := "/usr/lib/wsl"
159+
if st, err := os.Stat(wslLibPath); err == nil && st.IsDir() {
160+
mounts = append(mounts, mount.Mount{
161+
Type: mount.TypeBind,
162+
Source: wslLibPath,
163+
Target: wslLibPath,
164+
ReadOnly: true,
165+
})
169166
}
170167
}
171168
hc.Mounts = mounts
@@ -574,6 +571,24 @@ func getBuildkitFlags(initConfig driver.InitConfig) []string {
574571
return flags
575572
}
576573

574+
func hasLocalSocketEndpoint(endpoint string, contextStore contextstore.Reader) bool {
575+
if isSocket(endpoint) {
576+
return true
577+
}
578+
if contextStore == nil {
579+
return false
580+
}
581+
cm, err := contextStore.GetMetadata(endpoint)
582+
if err != nil {
583+
return false
584+
}
585+
epm, err := docker.EndpointFromContext(cm)
586+
if err != nil {
587+
return false
588+
}
589+
return isSocket(epm.Host)
590+
}
591+
577592
func isSocket(addr string) bool {
578593
switch proto, _, _ := strings.Cut(addr, "://"); proto {
579594
case "unix", "npipe", "fd":

driver/manager.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ type InitConfig struct {
3131
Name string
3232
EndpointAddr string
3333
DockerAPI dockerclient.APIClient
34-
DockerContext string
3534
ContextStore store.Reader
3635
BuildkitdFlags []string
3736
Files map[string][]byte

0 commit comments

Comments
 (0)