基於docker安裝tensorflow

卡巴拉的樹發表於2018-02-24

最近在自學機器學習,大熱的Tensorflow自然不能錯過,所以首先解決安裝問題,為了不影響本地環境,所以本文基於Docker來安裝Tensorflow,我的環境是Ubuntu16.04。

基於docker安裝tensorflow

安裝Docker

Docker分為CE和EE,這裡我們選擇CE,也就是常規的社群版,首先移除本機上可能存在的舊版本。

移除舊版本

$ sudo apt-get remove docker \
               docker-engine \
               docker.io
複製程式碼

安裝可選核心模組

從Ubuntu14.04以後,某些裁剪後的系統會把一部分核心模組移到可選核心包中,常以linux-image-extra-*開頭,而Docker推薦的儲存層驅動AUFS包含在可選核心模組包中,所以還是建議安裝可選核心模組包的。可以使用以下命令安裝:

$ 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
複製程式碼

基於docker安裝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
複製程式碼

顯示如下:

基於docker安裝tensorflow
顯示Jupyter Notebook,Jupyter Notebook(此前被稱為 IPython notebook)是一個互動式筆記本。示例中已經顯示了Tensorflow的入門教程,點開一個可以看見

基於docker安裝tensorflow
如上面這個例子,是使用tensorflow來使兩個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一樣了。

基於docker安裝tensorflow

這個映象預設沒有裝vim,所以自己又下載了vim來寫程式碼。

至此,安裝過程結束。

相關文章