38

I am able to run a tensorflow container w/ access to the GPU from the command line w/ the following command

$ sudo docker run --runtime=nvidia --rm gcr.io/tensorflow/tensorflow:latest-gpu

I would like to be able to run this container from docker-compose. Is it possible to specify the --runtime flag from docker-compose.yml?

rissem
  • 503
  • 1
  • 5
  • 11

3 Answers3

45

Currently (Aug 2018), NVIDIA container runtime for Docker (nvidia-docker2) supports Docker Compose.

Yes, use Compose format 2.3 and add runtime: nvidia to your GPU service. Docker Compose must be version 1.19.0 or higher.

Example docker-compose.yml:

version: '2.3'

services:
  nvsmi:
    image: ubuntu:16.04
    runtime: nvidia
    environment:
      - NVIDIA_VISIBLE_DEVICES=all
    command: nvidia-smi

More example from NVIDIA blog uses Docker Compose to show how to launch multiple GPU containers with the NVIDIA Container Runtime.

cedrickchee
  • 832
  • 8
  • 12
  • 5
    This option is possible only for docker-compose v2 for now. For more info, please look at the corresponding pull request here https://github.com/docker/compose/pull/5405 – hgasimov Dec 30 '18 at 09:33
  • This option is more elegant as it allows for better packaging using docker-compose – Blaze Jan 22 '19 at 08:13
  • hm can different containers have different runtimes in one docker-compose file? – kodlan Feb 01 '21 at 20:59
32

You should edit /etc/docker/daemon.json, adding the first level key "default-runtime": "nvidia", restart docker daemon (ex. "sudo service docker restart") and then all containers on that host will run with nvidia runtime.

More info on daemon.json here

5

Or better: using systemd and assuming the path is /usr/libexec/oci/hooks.d/nvidia

Configure

mkdir -p /etc/systemd/system/docker.service.d/
cat > /etc/systemd/system/docker.service.d/nvidia-containers.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -D --add-runtime nvidia=/usr/libexec/oci/hooks.d/nvidia --default-runtime=nvidia
EOF

Restart

systemctl daemon-reload
systemctl restart docker

Demo

Don't need to specify --runtime=nvidia since we set default-runtime=nvidia in the configuration step.

docker run --rm gcr.io/tensorflow/tensorflow:latest-gpu

Solution Inspired from my tutorial about KATA runtime.

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254