@@ -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+ }
0 commit comments