本篇基于macOS 使用 Docker安装TensorFlow
未安装Docker?参考《AI初探-MacOS Docker 安装》
在终端键入命令:
$ docker run -it -p 8888:8888 tensorflow/tensorflow
其中,
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-p 8888:8888”代表将容器中的8888端口映射到本地机器的8888端口上,可以使tensorflow程序运行在Jupyter notebook上;
最后的“tensorflow/tensorflow”代表启动的容器名称及其版本标签。
从镜像tensorflow/tensorflow实例化容器,如果容器在本地不存在,会自动下载到本地再运行:
$ docker run -it -p 8888:8888 tensorflow/tensorflow
Unable to find image 'tensorflow/tensorflow:latest' locally
latest: Pulling from tensorflow/tensorflow
1be7f2b886e8: Pull complete
6fbc4a21b806: Pull complete
...
Digest: sha256:ba27241364bc4aa2a49120877d3e741b8b57f436bfc626c5388d9b6fe2e280bc
Status: Downloaded newer image for tensorflow/tensorflow:latest
[I 09:02:48.497 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[W 09:02:48.539 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 09:02:48.557 NotebookApp] Serving notebooks from local directory: /notebooks
[I 09:02:48.557 NotebookApp] 0 active kernels
[I 09:02:48.557 NotebookApp] The Jupyter Notebook is running at:
[I 09:02:48.557 NotebookApp] http://[all ip addresses on your system]:8888/?token=a8dc18a90aa46034800aeb46291cb4d415e7175d162a96ef
[I 09:02:48.558 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 09:02:48.560 NotebookApp]
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8888/?token=a8dc18a90aa46034800aeb46291cb4d415e7175d162a96ef
访问http://localhost:8888/?token=a8dc18a90aa46034800aeb46291cb4d415e7175d162a96ef,进入Jupyter Notebook后台,运行一个简短的tensorflow程序:
# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
输出Hello, TensorFlow!,安装成功,后续文章将继续TensorFlow之旅,~~!
PS:下面提供一些简单的docker命令 - -
在终端键入命令,查看运行中的容器:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 541c3842db3b tensorflow/tensorflow "/run_jupyter.sh --a…" 2 minutes ago Up 3 minutes 6006/tcp, 8888/tcp, 0.0.0.0:8888->8888/tcp clever_goldberg
其中“CONTAINER ID”为容器编号;
在终端键入命令,停止运行中的容器:
$ docker stop 541c3842db3b 541c3842db3b
其中“541c3842db3b”是容器ID;
在终端键入命令,查看停止状态的容器:
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 541c3842db3b tensorflow/tensorflow "/run_jupyter.sh --a…" 20 minutes ago Exited (137) 2 minutes ago clever_goldberg
在终端键入命令,启动停止状态的容器:
$ docker start 541c3842db3b 541c3842db3b
参考资料:
https://www.tensorflow.org/install/install_mac#ValidateYourInstallation
https://docs.docker.com/engine/reference/commandline/run/
https://www.jianshu.com/p/f3f1e6cefda0