Docker中使用TensorFlow

下载TensorFlow images


使用hub.docker.com的镜像

  • docker pull tensorflow/tensorflow:latest

使用daocloud 的镜像,在国内用速度还是挺快的,如果docker.io的镜像慢,可以用daocloud的。

  • docker pull daocloud.io/daocloud/tensorflow:latest

启动镜像


启动命令,设置端口,同时配置volume 数据卷,用于永久保存数据。加上 –rm 在停止的时候删除镜像。

  • sudo mkdir -p /data/tensorflow/notebooks
  • docker run -it --rm --name myts -v /data/tensorflow/notebooks:/notebooks -p 8888:8888 daocloud.io/daocloud/tensorflow:latest

启动的时候并不是daemon 模式的,而是前台模式,同时显示了运行的日志。

打开浏览器就可以直接看到界面了。 
Docker中使用TensorFlow

同时可以编辑内容: 
Docker中使用TensorFlow

写第一个 hello world: 
Docker中使用TensorFlow

import tensorflow as tf 
a = tf.constant(10) 
b = tf.constant(32) 
with tf.Session():
 c = tf.add(a,b) 
 print(c) 
 print(c.eval())

其他的使用参考中文手册: 
https://github.com/jikexueyuanwiki/tensorflow-zh 
里面有pdf 可以下载使用。

还有一个超级炫酷的playground : 
http://playground.tensorflow.org/ 
Docker中使用TensorFlow

5,打个补丁


vi run_jupyter.sh

jupyter notebook --no-browser --NotebookApp.token='token1234' > /notebooks/jupyter-notebook.log

然后重新打一个docker镜像。 
vi Dockerfile

FROM daocloud.io/daocloud/tensorflow:latest

RUN rm -f /run_jupyter.sh

COPY run_jupyter.sh /run_jupyter.sh

ENTRYPOINT ["/run_jupyter.sh"]

这样就固定token了。

docker build -t mytf:1.0 
docker run -it --rm --name myts -v /data/tensorflow/notebooks:/notebooks -p 8888:8888 -d mytf:1.0

Docker中使用TensorFlow

然后就可以 -d 参数,将docker 运行放到后台。然后就可以使用 docker exec -it xxx bash 登录进去查看系统的状况了。