您的位置:首页 > 运维架构 > Docker

Ubuntu下安装Docker,TensorFlow和Caffe

2017-01-14 00:00 288 查看
摘要: 简要介绍如何在Ubuntu下安装Docker,并构建TensorFlow和Caffe运行环境。

最近需要用到人工神经网络(ANN)方面的东西,调研了一下,发现几个比较流行的框架(见如下链接),
http://www.leiphone.com/news/201612/rFVygnQf4WjogJQR.html http://www.leiphone.com/news/201701/Lutmxs35U8ZNF7p6.html
选了其中两个TensorFlow和Caffe试了一下。本文记录了安装TensorFlow和Caffe的过程,因为2者均支持Docker安装,简单起见,先利用Docker在Ubuntu Linux下搭建好运行环境。Windows下安装略麻烦些,以后再写。

首先安装Docker,安装过程可参考如下链接
https://docs.docker.com/engine/installation/linux/ubuntulinux/
我的系统是Ubuntu 14.04,所以简单按照步骤一步一步来很容易就安装完成了。以下为步骤(我用了aptitude代替apt-get,如果没有安装aptitude,可将以下命令中的aptitude全部替换为apt-get)

1. 用root用户登陆系统。

2. 更新安装包信息,并确保apt支持https,以及CA认证已安装

aptitude update
aptitude install apt-transport-https ca-certificates

3. 新增GPG-key,以下代码将ID为58118E89F3A912897C070ADBF76221572C52609D的key从服务器hkp://ha.pool.sks-keyservers.net:80添加到本地adv密钥串中

apt-key adv \
--keyserver hkp://ha.pool.sks-keyservers.net:80 \
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D

如果本命令不成功,可将hkp://ha.pool.sks-keyservers.net:80替换为hkp://pgp.mit.edu:80或hkp://keyserver.ubuntu.com:80再试。

4. 添加Ubuntu安装升级源(因为我的操作系统是Ubuntu 14.04,因此使用如下地址。其他版本地址可参考前面给出的链接)

echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" | sudo tee /etc/apt/sources.list.d/docker.list

5. 更新包索引

aptitude update

6. 安装内核扩展

aptitude install linux-image-extra-$(uname -r) linux-image-extra-virtual

7. 安装docker

aptitude install docker-engine

8. 启动docker

service docker start

至此,Docker就已经安装完成了。可通过执行如下指令测试安装结果

root@MyServer:~$ docker run hello-world

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.
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 Hub account: https://hub.docker.com 
For more examples and ideas, visit: https://docs.docker.com/engine/userguide/[/code] 9. 但是以上安装步骤完成后,只有root用户可以运行docker镜像,为了实现普通用户也能进行docker操作,还需要创建docker用户组并将相关用户添加到该组中。比如想让praise用户也可以运行docker镜像,则执行如下指令

groupadd docker
usermod -aG docker praise

此时登陆praise用户,则可发现该用户也可成功运行docker镜像了。

​praise@MyServer:~$ docker run hello-world

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.
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 Hub account: https://hub.docker.com 
For more examples and ideas, visit: https://docs.docker.com/engine/userguide/


接下来安装TensorFlow。TensorFlow是google的开源深度学习框架,可以方便地实现各种神经网络计算。TensorFlow的文档也写得很清楚,可惜网站不时被墙,不太方便。
https://www.tensorflow.org/get_started/os_setup#docker_installation
安装docker之后,执行如下指令,

docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow bash

则会自动下载tensorflow镜像,并进入镜像bash环境。如果成功进入镜像bash环境,则可以测试tensorflow是否安装成功。

root@5e0b52f09e8c:/notebooks# python -c 'import os; import inspect; import tensorflow; print(os.path.dirname(inspect.getfile(tensorflow)))'
/usr/local/lib/python2.7/dist-packages/tensorflow


由于TensorFlow不时被墙很不爽,因此又安装了校友贾扬清创建的Caffe深度学习框架。
http://caffe.berkeleyvision.org/ https://github.com/BVLC/caffe/tree/master/docker
先从github上拉取最新开发环境

git clone https://github.com/BVLC/caffe.git[/code] 进入caffe/docker目录,生成caffe的docker镜像

cd caffe/docker
docker build -t caffe:cpu standalone/cpu

此时会自动生成docker镜像并下载相关依赖的软件包。整个过程结束后,可执行如下指令测试Caffe是否安装成功。

praise@MyServer:~/Software/CaffeBVLC$ docker run -it caffe:cpu caffe --version
libdc1394 error: Failed to initialize libdc1394
caffe version 1.0.0-rc3

若显示如上信息,则表明Caffe已经安装成功。类似TensorFlow,可通过如下命令进入Caffe镜像的bash环境。

docker run -it caffe:cpu bash

进入Caffe镜像的bash环境之后,还可通过如下命令测试Caffe是否安装正常。

root@aa42d96dfdbe:/workspace# cd /opt/caffe/build/
root@aa42d96dfdbe:/opt/caffe/build# make runtest


至此,Docker,TensorFlow和Caffe均已成功安装。后面的文章中会介绍TensorFlow和Caffe的使用,以及二者在Windows上的安装。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Docker TensorFlow Caffe