最近在自學機器學習,大熱的Tensorflow天然不能錯過,因此首先解決安裝問題,爲了避免影響本地環境,因此本文基於Docker來安裝Tensorflow,個人環境是Ubuntu16.04。linux
Docker分爲CE和EE,這裏咱們選擇CE,也就是常規的社區版,首先移除本機上可能存在的舊版本。docker
$ sudo apt-get remove docker \
docker-engine \
docker.io
複製代碼
從Ubuntu14.04之後,某些裁剪後的系統會把一部份內核模塊移到可選內核包中,常以linux-image-extra-*
開頭,而Docker推薦的存儲層驅動AUFS包含在可選內核模塊包中,因此仍是建議安裝可選內核模塊包的。能夠使用如下命令安裝:ubuntu
$ sudo apt-get update
$ sudo apt-get install \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual
複製代碼
在正式安裝以前,咱們須要添加證書以及HTTPS傳輸的軟件包以保證軟件下載過程當中不被篡改:vim
$ 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軟件源:bash
$ 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"
複製代碼
$ sudo apt-get update
$ sudo apt-get install docker-ce
複製代碼
docker一般會使用Unix socket和Docker引擎通信,一般只有root和docker用戶組的用戶才能夠訪問該socket,否則你就要一直sudo,因此最好把你當前須要使用docker的用戶添加到docker用戶組中。cookie
創建docker用戶組app
$ sudo groupadd docker
複製代碼
將當前用戶加入用戶組curl
$ sudo usermod -aG docker $USER
複製代碼
最後從新登陸下系統機器學習
確保服務啓動
$ 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/
複製代碼
若能顯示,證實安裝成功。
有了Docker,安裝Tensorflow基本沒有什麼難度。
docker pull tensorflow/tensorflow
複製代碼
下載完畢後顯示:
Status: Downloaded newer image for tensorflow/tensorflow:latest
複製代碼
docker run --name my-tensorflow -it -p 8888:8888 -v ~/tensorflow:/test/data tensorflow/tensorflow
複製代碼
http://localhost:8888/
映射輸入以上命令後,默認容器就被啓動了,命令行顯示:
[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 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來寫代碼。
至此,安裝過程結束。