docker-compose v3.7. How to give access to GPU for service

Viewed 377

I have docker-compose.yml file with configuration of my application.

One of the services needs access to the GPU. I built the container separately and using the command docker run -it --gpus=all <my_image> /bin/bash quite simply started everything.

But I need to start this service with GPU access using docker-compose.

Version of my docker-compose.yml - 3.7

And if I use the code examples from the official documentation, then they do not work, for example these one:

services:
  test:
    image: tensorflow/tensorflow:latest-gpu
    command: python -c "import tensorflow as tf;tf.test.gpu_device_name()"
    deploy:
      resources:
        reservations:
          devices:
          - capabilities: [gpu]

docker-compose throws an error on the keyword reservations

Please tell me how can I give access to the GPU for my service with docker-compose.yml version 3.7

1 Answers

Check the version of your docker-compose >= 1.28, I had a similar issue and updating solved it for me.

From the documentation:

Docker Compose v1.28.0+ allows to define GPU reservations

You can check the documentation for how to install docker-compose. Yet, here is what worked for me:

Check your version (to make sure this is the issue)

$ docker-compose --version
docker-compose version 1.25.0, build unknown

The version should be at least 1.28. Then you can install the latest by running the following comands:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Start a new terminal and check for the version again:

$ docker-compose --version
docker-compose version 1.29.2, build 5becea4c
Related