Docker Engine API enable GPU in create call

I asked this question already on stack overflow but maybe this forum is the better location for it. I would like to avoid installing the docker cli to create a docker sibling and hence create/start/remove a docker container using the docker engine api. For non GPU tasks this works great but not for containers which need the GPU. For example to create:

curl -s -X POST --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -d '{"Image": "nvidia/cuda:11.8.0-base-ubuntu22.04", "Cmd": ["nvidia-smi"], "AttachStdout": true}' http://localhost/containers/create

works but starting it doesn’t work since the GPU is missing. I can’t find in the reference ( how to attach the gpu of the system.

I can create this via the cli and then start it without issues on the machine:

docker create --gpus all nvidia/cuda:11.8.0-base-ubuntu22.04 nvidia-smi

According to the docker create reference (docker create | Docker Docs) the --gpus is available since API 1.40+. Any idea how to do this in the curl command?

Update: I mixed --gpus all and runtime in my head, as they are related. Though, setting the runtime is just a workaround for what you are looking for. /Update:end

You could have found it by using docker inspect on one of the containers that uses the nvidia runtime.

The setting you are looking for is buried in the submenu for the HostConfig settings in the api docs.

Try this:

curl -s -X POST --unix-socket /var/run/docker.sock \
  -H "Content-Type: application/json" \
  -d '{"Image": "nvidia/cuda:11.8.0-base-ubuntu22.04", "Cmd": ["nvidia-smi"], "AttachStdout": true, "HostConfig": { "Runtime": "name of your runtime in daemon.json"}' \

Of course after replacing the actual value for Runtime with a runtime that exists in /etc/docker/daemon.json.

Thanks for your answer @meyay. I finally found the section in the reference:

“DeviceRequests”: [

  • {
    • “Driver”: “nvidia”,

    • “Count”: -1,

    • “DeviceIDs”": ,

    • “Capabilities”: ,

    • “Options”: {}}

It was defined in the sample request section not in the main help body. This works great.

1 Like