[docker]的使用

1 docker的安装

首先普及一下基本概念:docker是一个系统镜像管理器,可以使用docker pull来下载网上已经打包好的特定环境的系统,这种系统称为image,将image拉下来之后,使用docker run进行运行,运行的系统实例称为容器(container)。因为docker可以封装任意环境的系统镜像,所以避免了复杂的环境配置,学习一些基本的docker命令可以使用我们做实验搭建环境事半功倍。
一些image需要nvidia-docker来pull,但nvidia-docker通用性差,所以我们使用需要改为官方docker。
官方docker安装参考地址
官方docker安装后,使用先前需要配置环境,nvidia-docker替换为官方docker

export CUDA_SO=$(\ls /usr/lib/x86_64-linux-gnu/libcuda.* | xargs -I{} echo '-v {}:{}')
export DEVICES=$(\ls /dev/nvidia* | xargs -I{} echo '--device {}:{}')

2 docker的使用

2.1 docker常用命令

pull系统镜像到本地

//下载theano系统镜像
docker pull kaixhin/cuda-theano:7.5
//下载tensorflow系统镜像
docker pull tensorflow/tensorflow:latest-gpu

创建一个名为theano的容器,将其文件系统中/work与本机中的’pwd’/theano目录进行关联,8000-9000作为对外开放的端口(尽量少占用点端口),参考

docker run --name xxx-theano -v `pwd`/theano:/work -w /work -itd -p 8000-9000:8000-9000 $CUDA_SO $DEVICES kaixhin/cuda-theano:7.5 bash

查看本地镜像与运行的容器

docker images    // watch installed image 
docker ps -a     // watch image instances in docker

创建容器实例之后,还需要将容器与本地bash关联才能进入容器,如下

//将bash与theano容器进行attach,并运行该bash
docker exec –it theano bash   

删除本地镜像或运行的容器

docker rm –vf theano	//删除名为theano的容器 
docker rm images_name	//删除名为imags_name的容器

2.2 局域网内用户直接登录docker中某容器

docker中安装ssh:

sudo apt-get install openssh-server

运行ssh/sshd或者用绝对路径( 有可能在/var/run/ssh中没有的话自己创建 )运行sshd
修改/etc/ssh/sshd_config,修改以下部分

port: 9020	//这个端口是运行docker run的时候留下的端口
Permitrootlogin:	yes

再启动ssh

sudo service ssh start	//启动ssh-server

之后就可以在xterm或者xshell中登录了,注意端口要设置为sshd_config中留的端口号,如9020。
如果要使用户同时具有sudo权限

sudo usermod –aG sudo username

3.1 使用过程遇到的问题

(1) error: docker: Network timed out while trying to connect to xxx/images
You may want to check your internet connection or if you are behind a proxy.
solution:
sudo vim /etc/default/docker
a.在文件下方加入代理,因为默认为国外image源
b.加入DNS域名,具体域名内容参见windows系统中“ipv4 dns server”中的IP

DOCKER_OPTS="--dns 10.248.2.5 --dns 10.239.27.228 --dns 172.17.6.9"

c. 在本地docker server中添加自己为用户

sudo usermod -aG docker $USER

(2) error: Tag latest not found in repository docker.io/kaixhin/cuda-theano
因为tag中没有latest的名字,所以指名我们要安装的具体版本:

docker pull kaixhin/cuda-theano:7.5   

参考
(3) error: docker: unauthorized: authentication required
image pull 到一半时会提示该信息,然后以失败告终
solution:
查看系统时间信息时否正确,如果不正确在图形化界面中修改为当前时间

date

(4) error: cannot import name raise_from when python import theano

solution: pip --proxy http://child-prc.intel.com:913 install -U six

(5) 宿主机关机后
如果宿主机关机重启后,会发现docker ps –a中没有运行的容器,这时需要

docker start container	

但有时候这句命令会报错:
dev/nvidia-uvm-tools: no such file or directory
Error: failed to start containers: caffe
解决

cd  /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo ./deviceQuery
posted @ 2017-07-15 21:34  fariver  阅读(691)  评论(0编辑  收藏  举报