Ubuntu18.04にDoker19.03環境構築(+NVIDIA Container Toolkit)


参考文献

本ページは以下を参考にしています。

概要

Dockerを使えば

  • 環境を汚すことなくCUDA ToolkitやcuDNNのバージョンが変えられる
  • 環境構築に要する時間が短くなる

と聞いたので上記ページを参考に自身の環境にDocker環境を構築しました。
自身の備忘録ですので、参考程度にご覧ください。

手順1 Dockerのインストール

Install Docker Engine on Ubuntuに従って進めます。

  1. 古いDockerの削除

    $ sudo apt-get remove docker docker-engine docker.io containerd runc
    
  2. リポジトリの設定

    $ sudo apt-get update
    $ sudo apt-get install \
           apt-transport-https \
           ca-certificates \
           curl \
           gnupg-agent \
           software-properties-common
    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    $ sudo apt-key fingerprint 0EBFCD88
    # x86_64 / amd64 の場合
    $ sudo add-apt-repository \
           "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
           $(lsb_release -cs) \
           stable"
    
  3. Docker Engineのインストール

    $ sudo apt-get update
    # 最新版をインストール(特定のバージョンをインストールする方法は公式ページを参照)
    $ sudo apt-get install docker-ce docker-ce-cli containerd.io
    # 再起動
    $ sudo reboot
    
  4. 確認

    $ sudo docker run hello-world
    # --- 以下のような情報が出力される ---
    # Unable to find image 'hello-world:latest' locally
    # latest: Pulling from library/hello-world
    # 0e03bdcc26d7: Pull complete 
    # Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
    # Status: Downloaded newer image for hello-world:latest
    # 
    # Hello from Docker!
    # This message shows that your installation appears to be working correctly.
    # 
    # To generate this message, Docker took the following steps:
    #  1. The Docker client contacted the Docker daemon.
    #  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    #     (amd64)
    #  3. The Docker daemon created a new container from that image which runs the
    #     executable that produces the output you are currently reading.
    #  4. The Docker daemon streamed that output to the Docker client, which sent it
    #     to your terminal.
    # 
    # To try something more ambitious, you can run an Ubuntu container with:
    #  $ docker run -it ubuntu bash
    #
    # Share images, automate workflows, and more with a free Docker ID:
    #  https://hub.docker.com/
    # 
    # For more examples and ideas, visit:
    #  https://docs.docker.com/get-started/
    

手順2 NVIDIA Container Toolkitのインストール

NVIDIA/nvidia-dockerのQuickstartに従って進めます。

  1. リポジトリ追加

    $ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
    $ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
    $ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
    
  2. インストール

    $ sudo apt-get update
    $ sudo apt-get install -y nvidia-container-toolkit
    # Dockerリスタート
    $ sudo systemctl restart docker
    
  3. 確認

    # CUDA10.0,すべてのGPUを使用
    docker run --gpus all nvidia/cuda:10.0-base nvidia-smi
    

おまけ 非toot権限でDockerコマンド使用

Post-installation steps for Linuxに従って進めます。

  1. dockerグループの作成

    $ sudo groupadd docker
    
  2. ユーザーの追加

    $ sudo usermod -aG docker $USER
    
  3. 設定の反映(ログアウト&ログイン or 以下コマンド実行)

    $ newgrp docker
    
  4. 確認

    $ docker run hello-world
    

感想

インストールは手順がしっかり記載されていたため、特に問題なくできました。
ただ、Dockerの使い方が全然わかりません(笑)