2

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"?

Nos
  • 215
  • 2
  • 9
  • I found this resource, hoping it can help you: https://github.com/eywalker/nvidia-docker-compose/issues/1 – damdamo Aug 24 '22 at 14:09
  • @damdamo Thanks for the resource! But if I understand it correctly, it still does not solve the problem since I'm not looking for a way to disable the GPU (then I could just remove the `deploy` section) but I need something adaptive. – Nos Aug 24 '22 at 15:54
  • did you find any solution to that? – Mpizos Dimitris Jul 01 '23 at 15:43
  • Unfortunately not. – Nos Jul 01 '23 at 19:30

2 Answers2

1

@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

Slava Kuravsky
  • 2,702
  • 10
  • 16
  • Seems like this problem is as old as docker compose itself. In this specific case, it would be enought for docker to fail less agressively if a GPU is not available. The linked issue is somewhat helpful, at least it tells me that there does not seem to be any way of using environment variables to do conditionals. – Herku Aug 25 '22 at 14:21
1

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.

Nos
  • 215
  • 2
  • 9