Detect nvidia in docker compose

Hi Everyone, I have a compose file and one of the containers uses “runtime: nvidia”. This works fine on systems nvidia drivers are installed but the container needs to run on other systems in which it is not. Right now on a system w/o nvidia I get the error “Unknown runtime specified nvidia”.

Is there a way in the compose file to detect this case and either include “runtime: nvidia” or not as the case may be?

Thanks,
Dave

I don’t know about such a feature in Docker Compose. The runtime is something that you should chk before you run the containers. I would create a shell script which checks the available runtimes and save the chosen runtime into an environment variable which I could use in the Docker Compose file.

docker-compose.yml

services:
  test:
    runtime: $RUNTIME

docker-compose.sh

#!/bin/bash
function runtime_check() {
   if nvidia runtime exists
     echo nvidia
   else
     echo runc
   fi
}

RUNTIME=$(runtime_check)

export RUNTIME

exec docker compose "$@"

Then you can run:

chmod +x docker-compose.sh
./docker-compose.sh up -d