Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker compose: use GPU if available, else start container without one

I'm using docker compose to run a container:

version: "3.9"
services:
  app:
    image: nvidia/cuda:11.0.3-base-ubuntu20.04
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [ gpu ]

The container can benefit from the presence of a GPU, but it does not strictly need one. Using the above docker-compose.yaml results in an error

Error response from daemon: could not select device driver "" with capabilities: [[gpu]]

when being used on a machine without a GPU. Is it possible to specify "use a GPU, if one is available, else start the container without one"?

like image 682
Nos Avatar asked Jun 30 '26 04:06

Nos


2 Answers

While not being fully automatic, there at least exists an option to disable the GPU, using a profile, so that users can switch off GPU usage without fiddling with the docker-compose.yaml, but instead using a CLI flag.

Here is an example for how to remove the deploy section, based on the presence of a CLI parameter. The relevant part is:

services:
  ...
  auto-cpu:
    <<: *automatic
    profiles: ["auto-cpu"]
    deploy: {}

I hope this helps.

like image 129
Nos Avatar answered Jul 02 '26 23:07

Nos


@herku, there are no conditional statements in docker compose. In 2018 the feature was out of scope https://github.com/docker/compose/issues/5756

Anyway you can check this answer with options how to workaround the problem https://stackoverflow.com/a/50393225/3730077

like image 34
Slava Kuravsky Avatar answered Jul 02 '26 22:07

Slava Kuravsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!