dockerベースtensorflowのインストール

7834 ワード

最近独学で機械学習をしているので、大ヒットしたTensorflowはもちろん見逃せないので、まずインストールの問題を解決して、現地の環境に影響しないように、本文はDockerに基づいてTensorflowをインストールして、私の環境はUbuntu 16です.04.

Dockerのインストール


DockerはCEとEEに分かれています.ここではCE、つまり通常のコミュニティ版を選択し、まず本機に存在する可能性のある古いバージョンを削除します.

古いバージョンの削除

$ sudo apt-get remove docker \
               docker-engine \
               docker.io

オプションカーネルモジュールのインストール


Ubuntu 14から.04以降、一部のトリミング後のシステムでは、カーネルモジュールの一部をオプションカーネルパッケージに移動し、linux-image-extra-*で始まることが多いが、Dockerが推奨するストレージ層駆動AFSはオプションカーネルモジュールパッケージに含まれているため、オプションカーネルモジュールパッケージのインストールを推奨する.次のコマンドを使用してインストールできます.
$ sudo apt-get update

$ sudo apt-get install \
    linux-image-extra-$(uname -r) \
    linux-image-extra-virtual

証明書および鍵の準備


正式にインストールする前に、証明書とHTTPS伝送のパッケージを追加して、ソフトウェアのダウンロード中に改ざんされないことを保証する必要があります.
$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

ソフトウェアソースのGPGキーを追加するには:
$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -


#    
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

最後にDockerソフトウェアソースを追加します.
$ sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"


#    
# $ sudo add-apt-repository \
#    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
#    $(lsb_release -cs) \
#    stable"


Dockerのインストール

$ sudo apt-get update

$ sudo apt-get install docker-ce

dockerユーザーグループの作成


dockerは通常Unix socketとDockerエンジンを使用して通信します.通常、rootとdockerユーザーグループのユーザーだけがこのsocketにアクセスできます.そうしないと、sudoを続ける必要があります.そのため、現在dockerを使用する必要があるユーザーをdockerユーザーグループに追加したほうがいいです.
dockerユーザーグループの作成
$ sudo groupadd docker

現在のユーザーをユーザー・グループに追加
$ sudo usermod -aG docker $USER

最後にシステムに再ログイン

Dockerのテスト


サービス開始の確認
$ sudo service docker start

HelloWorldテストの使用
        
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7
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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

表示できる場合は、インストールに成功したことを証明します.

Tensorflowのインストール


Dockerがあれば、Tensorflowのインストールはほとんど難しくありません.

ミラーのダウンロード

docker pull tensorflow/tensorflow

ダウンロードが完了すると、次のように表示されます.
Status: Downloaded newer image for tensorflow/tensorflow:latest

Tensorflowコンテナの作成

docker run --name my-tensorflow -it -p 8888:8888 -v ~/tensorflow:/test/data tensorflow/tensorflow
  • --name:作成されたコンテナ名、すなわちmy-tensorflow
  • -it:保留コマンドライン
  • を実行
  • p 8888:8888:ローカルの8888ポートとhttp://localhost:8888/
  • にマッピング
  • -v~/tensorflow:/test/data:ローカルの~/tensorflowを容器内の/test/data下
  • にマウントする.
  • tensorflow/tensorflow:デフォルトはtensorflow/tensorflow:latest、使用するミラー
  • を指定します.
    以上のコマンドを入力すると、デフォルトコンテナが起動され、コマンドラインが表示されます.
    [I 15:08:31.949 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
    [W 15:08:31.970 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
    [I 15:08:31.975 NotebookApp] Serving notebooks from local directory: /notebooks
    [I 15:08:31.975 NotebookApp] 0 active kernels
    [I 15:08:31.975 NotebookApp] The Jupyter Notebook is running at:
    [I 15:08:31.975 NotebookApp] http://[all ip addresses on your system]:8888/?token=649d7cab1734e01db75b6c2b476ea87aa0b24dde56662a27
    [I 15:08:31.975 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
    [C 15:08:31.975 NotebookApp] 
        
        Copy/paste this URL into your browser when you connect for the first time,
        to login with a token:
            ;
    [I 15:09:08.581 NotebookApp] 302 GET /?token=649d7cab1734e01db75b6c2b476ea87aa0b24dde56662a27 (172.17.0.1) 0.42ms
    

    コピーtokenのURLがブラウザで開きます
    http://[all ip addresses on your system]:8888/?token=649d7cab1734e01db75b6c2b476ea87aa0b24dde56662a27
    

    次のように表示されます.
    Jupyter Notebookが表示され、Jupyter Notebook(これまでIPython notebookと呼ばれていた)はインタラクティブなノートパソコンです.例ではTensorflowの入門チュートリアルが表示されています.
    上記の例のようにtensorflowを使用して2つのarrayを加算し、runをクリックすると実行結果が表示されます.

    容器を閉じる

    docker stop my-tensortflow
    

    再度開く

    docker start my-tensortflow
    

    Jupyter Notebookが気に入らない場合は、コマンドラインベースのコンテナを作成することもできます.

    コマンドラインベースコンテナ

    docker run -it --name bash_tensorflow tensorflow/tensorflow /bin/bash
    

    これでbash_という名前を作成しましたtensorflowの容器
    それともstartコマンドでコンテナを起動しますか?
    docker start bash_tensorflow
    

    コンテナを接続するには、次の手順に従います.
    docker attach bash_tensorflow
    

    端末でコンテナを接続し、Linuxを操作するのと同じようになっていることがわかります.
    このミラーはデフォルトでvimがインストールされていないので、自分でvimをダウンロードしてコードを書きます.
    これで、インストールプロセスは終了します.