Skip to content

Commit 10605b8

Browse files
authored
Merge pull request #3320 from crazy-max/mount-wsl-lib
driver: mount wsl lib folder for docker-container driver
2 parents 4f9f47d + ed67ab7 commit 10605b8

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

builder/node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ 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(),
125126
ContextStore: b.opts.dockerCli.ContextStore(),
126127
BuildkitdFlags: n.BuildkitdFlags,
127128
Files: n.Files,

driver/docker-container/driver.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/buildx/util/confutil"
1919
"github.com/docker/buildx/util/imagetools"
2020
"github.com/docker/buildx/util/progress"
21+
"github.com/docker/cli/cli/context/docker"
2122
"github.com/docker/cli/opts"
2223
"github.com/docker/docker/api/types/container"
2324
"github.com/docker/docker/api/types/image"
@@ -125,15 +126,38 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
125126
hc := &container.HostConfig{
126127
Privileged: true,
127128
RestartPolicy: d.restartPolicy,
128-
Mounts: []mount.Mount{
129-
{
130-
Type: mount.TypeVolume,
131-
Source: d.Name + volumeStateSuffix,
132-
Target: confutil.DefaultBuildKitStateDir,
133-
},
129+
Init: &useInit,
130+
}
131+
132+
mounts := []mount.Mount{
133+
{
134+
Type: mount.TypeVolume,
135+
Source: d.Name + volumeStateSuffix,
136+
Target: confutil.DefaultBuildKitStateDir,
134137
},
135-
Init: &useInit,
136138
}
139+
140+
// Mount WSL libaries if running in WSL environment and Docker context
141+
// is a local socket as requesting GPU on container builder creation
142+
// is not enough when generating the CDI specification for GPU devices.
143+
// https://github.com/docker/buildx/pull/3320
144+
if os.Getenv("WSL_DISTRO_NAME") != "" {
145+
if cm, err := d.ContextStore.GetMetadata(d.DockerContext); err == nil {
146+
if epm, err := docker.EndpointFromContext(cm); err == nil && isSocket(epm.Host) {
147+
wslLibPath := "/usr/lib/wsl"
148+
if st, err := os.Stat(wslLibPath); err == nil && st.IsDir() {
149+
mounts = append(mounts, mount.Mount{
150+
Type: mount.TypeBind,
151+
Source: wslLibPath,
152+
Target: wslLibPath,
153+
ReadOnly: true,
154+
})
155+
}
156+
}
157+
}
158+
}
159+
hc.Mounts = mounts
160+
137161
if d.netMode != "" {
138162
hc.NetworkMode = container.NetworkMode(d.netMode)
139163
}
@@ -531,3 +555,12 @@ func getBuildkitFlags(initConfig driver.InitConfig) []string {
531555
}
532556
return flags
533557
}
558+
559+
func isSocket(addr string) bool {
560+
switch proto, _, _ := strings.Cut(addr, "://"); proto {
561+
case "unix", "npipe", "fd":
562+
return true
563+
default:
564+
return false
565+
}
566+
}

driver/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type InitConfig struct {
3030
Name string
3131
EndpointAddr string
3232
DockerAPI dockerclient.APIClient
33+
DockerContext string
3334
ContextStore store.Reader
3435
BuildkitdFlags []string
3536
Files map[string][]byte

0 commit comments

Comments
 (0)