小知识:基于docker安装tensorflow的完整步骤

前言

google又一次成为大家膜拜的大神了。google大神在引导这机器学习的方向。 同时docker 也是一个非常好的工具,大大的方便了开发环境的构建,之前需要配置安装。 最近在自学机器学习,大热的tensorflow自然不能错过,所以首先解决安装问题,为了不影响本地环境,所以本文基于docker来安装tensorflow,我的环境是ubuntu16.04。

%小知识:基于docker安装tensorflow的完整步骤-猿站网-插图

安装docker

docker分为ce和ee,这里我们选择ce,也就是常规的社区版,首先移除本机上可能存在的旧版本。

移除旧版本

?
1
2
3
$ sudo apt-get remove docker \
docker-engine \
docker.io

安装可选内核模块

从ubuntu14.04以后,某些裁剪后的系统会把一部分内核模块移到可选内核包中,常以linux-image-extra-*开头,而docker推荐的存储层驱动aufs包含在可选内核模块包中,所以还是建议安装可选内核模块包的。可以使用以下命令安装:

?
1
2
3
4
$ sudo apt-get update
$ sudo apt-get install \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual

证书及密钥准备

在正式安装之前,我们需要添加证书以及https传输的软件包以保证软件下载过程中不被篡改:

?
1
2
3
4
5
6
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

添加软件源的gpg密钥:

?
1
2
3
$ 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软件源:

?
1
2
3
4
5
6
7
8
9
$ 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

?
1
2
$ sudo apt-get update
$ sudo apt-get install docker-ce

建立docker用户组

docker通常会使用unix socket和docker引擎通讯,通常只有root和docker用户组的用户才可以访问该socket,不然你就要一直sudo,所以最好把你当前需要使用docker的用户添加到docker用户组中。

建立docker用户组

?
1
$ sudo groupadd docker

将当前用户加入用户组

?
1
$ sudo usermod -ag docker $user

最后重新登录下系统

测试docker

确保服务启动

?
1
$ sudo service docker start

使用helloworld测试

测试安装是否成功

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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基本没有什么难度。

下载镜像

?
1
docker pull tensorflow/tensorflow

%小知识:基于docker安装tensorflow的完整步骤-1猿站网-插图

下载完毕后显示:

?
1
status: downloaded newer image for tensorflow/tensorflow:latest

创建tensorflow容器

?
1
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,指定使用的镜像

输入以上命令后,默认容器就被启动了,命令行显示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[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在浏览器打开

?
1
http://[all ip addresses on your system]:8888/?token=649d7cab1734e01db75b6c2b476ea87aa0b24dde56662a27

显示如下:

%小知识:基于docker安装tensorflow的完整步骤-2猿站网-插图

显示jupyter notebook,jupyter notebook(此前被称为 ipython notebook)是一个交互式笔记本。示例中已经显示了tensorflow的入门教程,点开一个可以看见

%小知识:基于docker安装tensorflow的完整步骤-3猿站网-插图

如上面这个例子,是使用tensorflow来使两个array相加,我们点击run,就可以看到运行的结果了。

关闭容器

?
1
docker stop my-tensortflow

再次打开

?
1
docker start my-tensortflow

如果不喜欢用jupyter notebook,我们也可以创建基于命令行的容器

基于命令行的容器

?
1
docker run -it –name bash_tensorflow tensorflow/tensorflow /bin/bash

这样我们就创建了名为bash_tensorflow的容器

还是用start命令启动容器:

?
1
docker start bash_tensorflow

再连接上容器:

?
1
docker attach bash_tensorflow

可以看到我们用终端连接上了容器,和操作linux一样了。

这个镜像默认没有装vim,所以自己又下载了vim来写代码。

至此,安装过程结束。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://juejin.im/post/5a8fea695188257a7450cb4c

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Windows10下安装Docker的步骤图文教程

2023-4-12 10:10:13

建站知识

小知识:CentOS7 Nvidia Docker环境搭建

2023-4-12 10:25:06

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索