LoginSignup
16
12

More than 5 years have passed since last update.

docker-composeからnvidia-dockerの利用がものすごく楽になっていた

Posted at

数ヶ月前にnvidia-dockerを使ったdeeplearningの作業環境をつくっていたのですが、糞めんどくさかった記憶が残っていました。

ですが、直近のnvidiaさんの頑張りとdocker-composeを支える有志たちのおかげでだいぶ楽になってました。ありがとうございます。

環境

  • EC2
  • ベースとしたAMI
    • Deep Learning Base AMI (Ubuntu) Version 3.0 (ami-de1476b8)

cuda8.0のセットアップがされているAMIをベースに作業をしました。

nvidia-docker2

インストール方法はこちらの記事を参考にさせていただきました。ありがとうござます。
https://qiita.com/yakigac/items/e5ea6f8fdc3c30b990c8

docker-composeのインストール

なんと!!!
(たぶん)三日前の2018/01/31頃にdocker-composeでruntime指定ができるようになっていました!!!!!!!!!!

nvidia-docker2では以下のように起動できるようになりましたが、

 docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi

docker-composeからはどう指定するんだろう...と数時間悩んでいた矢先でした(感動)

そんなruntimeに対応したdocker-composeのインストールは公式より
https://github.com/docker/compose/releases

curl -L https://github.com/docker/compose/releases/download/1.19.0-rc3/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

念のためversionの確認

docker-compose -v

>> docker-compose version 1.19.0-rc3, build cdd0282

対応verである1.19.0-rc3になってますね。

ちゃんと動くか確認

まずは今回のruntimeを使用しないverで動作チェックを行ってみます。
動作チェック自体は、下記のプログラムを実行します。

check.py

import tensorflow as tf
message = tf.constant('test message')
sess = tf.Session()
print(sess.run(message))

runtimeを特に指定しない docker-compose.yml

version: '2.3'
services:
  tensorflow:
    image: tensorflow/tensorflow:latest-gpu
    command: /bin/bash

check.pyを実行してみると...
下記のプログラムが実行された時点で

import tensorflow as tf

エラーが発生します。

>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: libcuda.so.1: cannot open shared object file: No such file or directory

正しくruntimeを設定したdocker-compose.ymlでコンテナを立ち上げて、check.pyを実行してみると

version: '2.3'
services:
  tensorflow:
    image: tensorflow/tensorflow:latest-gpu
    runtime: nvidia
    command: /bin/bash

>>> import tensorflow as tf

/usr/local/lib/python2.7/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

warningは出ますが、ImportError: libcuda.so.1: cannot open shared object file: No such file or directoryといったモジュールが見つからないなどのエラーは回避しているみたいですね。

それ以外のコマンドもうまく動いていますね。

>>> message = tf.constant('test message')
>>> sess = tf.Session()
2018-02-05 03:21:35.088328: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1195] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GRID K520, pci bus id: 0000:00:03.0, compute capability: 3.0)
>>> print(sess.run(message))
test message
16
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
12