Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit cf549c5

Browse files
authored
Merge pull request #156 from docker/cuda-detection
Improve CUDA detection
2 parents 3d7f55e + 6dfffc5 commit cf549c5

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

pkg/gpu/gpu.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gpu
22

33
import (
44
"context"
5+
"os/exec"
56

67
"github.com/docker/docker/client"
78
)
@@ -18,12 +19,30 @@ const (
1819

1920
// ProbeGPUSupport determines whether or not the Docker engine has GPU support.
2021
func ProbeGPUSupport(ctx context.Context, dockerClient client.SystemAPIClient) (GPUSupport, error) {
21-
info, err := dockerClient.Info(ctx)
22+
// First search for nvidia-container-runtime on PATH
23+
if _, err := exec.LookPath("nvidia-container-runtime"); err == nil {
24+
return GPUSupportCUDA, nil
25+
}
26+
27+
// Next look for explicitly configured nvidia runtime. This is not required in Docker 19.03+ but
28+
// may be configured on some systems
29+
hasNvidia, err := HasNVIDIARuntime(ctx, dockerClient)
2230
if err != nil {
2331
return GPUSupportNone, err
2432
}
25-
if _, hasNvidia := info.Runtimes["nvidia"]; hasNvidia {
33+
if hasNvidia {
2634
return GPUSupportCUDA, nil
2735
}
36+
2837
return GPUSupportNone, nil
2938
}
39+
40+
// HasNVIDIARuntime determines whether there is an nvidia runtime available
41+
func HasNVIDIARuntime(ctx context.Context, dockerClient client.SystemAPIClient) (bool, error) {
42+
info, err := dockerClient.Info(ctx)
43+
if err != nil {
44+
return false, err
45+
}
46+
_, hasNvidia := info.Runtimes["nvidia"]
47+
return hasNvidia, nil
48+
}

pkg/standalone/containers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ func CreateControllerContainer(ctx context.Context, dockerClient *client.Client,
269269
nat.Port(portStr + "/tcp"): portBindings,
270270
}
271271
if gpu == gpupkg.GPUSupportCUDA {
272-
hostConfig.Runtime = "nvidia"
272+
if ok, err := gpupkg.HasNVIDIARuntime(ctx, dockerClient); err == nil && ok {
273+
hostConfig.Runtime = "nvidia"
274+
}
273275
hostConfig.DeviceRequests = []container.DeviceRequest{{Count: -1, Capabilities: [][]string{{"gpu"}}}}
274276
}
275277

0 commit comments

Comments
 (0)