@@ -56,6 +56,7 @@ type Driver struct {
56
56
restartPolicy container.RestartPolicy
57
57
env []string
58
58
defaultLoad bool
59
+ gpus []container.DeviceRequest
59
60
}
60
61
61
62
func (d * Driver ) IsMobyDriver () bool {
@@ -158,6 +159,9 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
158
159
if d .cpusetMems != "" {
159
160
hc .Resources .CpusetMems = d .cpusetMems
160
161
}
162
+ if len (d .gpus ) > 0 && d .hasGPUCapability (ctx , cfg .Image , d .gpus ) {
163
+ hc .Resources .DeviceRequests = d .gpus
164
+ }
161
165
if info , err := d .DockerAPI .Info (ctx ); err == nil {
162
166
if info .CgroupDriver == "cgroupfs" {
163
167
// Place all buildkit containers inside this cgroup by default so limits can be attached
@@ -429,6 +433,31 @@ func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
429
433
return nil , errors .New ("host-gateway is not supported by the docker-container driver" )
430
434
}
431
435
436
+ // hasGPUCapability checks if docker daemon has GPU capability. We need to run
437
+ // a dummy container with GPU device to check if the daemon has this capability
438
+ // because there is no API to check it yet.
439
+ func (d * Driver ) hasGPUCapability (ctx context.Context , image string , gpus []container.DeviceRequest ) bool {
440
+ cfg := & container.Config {
441
+ Image : image ,
442
+ Entrypoint : []string {"/bin/true" },
443
+ }
444
+ hc := & container.HostConfig {
445
+ NetworkMode : container .NetworkMode (container .IPCModeNone ),
446
+ AutoRemove : true ,
447
+ Resources : container.Resources {
448
+ DeviceRequests : gpus ,
449
+ },
450
+ }
451
+ resp , err := d .DockerAPI .ContainerCreate (ctx , cfg , hc , & network.NetworkingConfig {}, nil , "" )
452
+ if err != nil {
453
+ return false
454
+ }
455
+ if err := d .DockerAPI .ContainerStart (ctx , resp .ID , container.StartOptions {}); err != nil {
456
+ return false
457
+ }
458
+ return true
459
+ }
460
+
432
461
func demuxConn (c net.Conn ) net.Conn {
433
462
pr , pw := io .Pipe ()
434
463
// TODO: rewrite parser with Reader() to avoid goroutine switch
0 commit comments