Docker - 基于NVIDIA-Docker的Caffe-GPU环境搭建

Docker与NVIDIA-Docker的安装与配置
Caffe镜像使用
环境:
– Ubuntu14.04
– Nvidia-docker version=1.0

1 Docker与NVIDIA-Docker的安装与配置

1.1 Docker安装

Docker安装过程需要使用root权限, 主要有两种安装方式:

1.1.1 Ubuntu14.04 软件源安装

使用Ubuntu14.04系统默认自带的docker.io安装包安装Docker,版本相对较旧.
命令行操作过程如下:

sudo apt-get update
sudo apt-get -y install docker.io
sudo service docker.io status (检查Docker服务的状态)
sudo docker run hello-world (测试Docker安装是否成功)
ShellCopy
1.1.2 Docker官网

Docker CE 安装

采用官网安装方式可以获取最新版本Docker. 在安装Docker之前需要配置Docker官方仓库,然后从该仓库进行获取与安装.
– 首先,卸载 旧版本 docker:

sudo apt-get remove docker docker-engine docker.io
ShellCopy
AUFS
为了使 docker 使用 aufs 存储驱动,Ubuntu14.4 需要安装 linux-image-extra-* 包:
sudo apt-get update
sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual
ShellCopy
Ubuntu16.04 及更高版本, Linux kernel 自支持 OverlayFS, DockerCE 默认采用 overlay2 存储驱动.
也可以手动安装 aufs.

进行Docker仓库设置:
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - # Docker 官方 GPG key
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
ShellCopy
然后, 安装Docker:
sudo apt-get update
sudo apt-get install docker-ce
apt-cache madison docker-ce
sudo docker run hello-world
ShellCopy
1.1.3 Docker deb 安装(强烈推荐)

Ubuntu16.04 – amd 对应 Docker deb 下载路径:

https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/
TxtCopy
Ubuntu 其他版本 Docker deb 下载路径:

https://download.docker.com/linux/ubuntu/dists/
选择对应的 Ubuntu 版本号,依次选择下载列表.
TxtCopy
Docker deb 安装:

sudo dpkg -i /path/to/docker.deb
ShellCopy
1.1.4 查看 Docker 状态

sudo service docker status
sudo docker info
ShellCopy
1.2 阿里云加速器设置

由于官方 Docker Hub 网络速度较慢,这里使用阿里云提供的 Docker Hub. 需要配置 阿里云加速器,官方说明如下:

针对Docker客户端版本大于1.10的用户:
您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-‘EOF’
{
“registry-mirrors”: [“https://fird1mfg.mirror.aliyuncs.com“]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
ShellCopy
针对Docker客户的版本小于等于1.10的用户:
或者想配置启动参数,可以使用下面的命令将配置添加到docker daemon的启动参数中.
Ubuntu 12.04 14.04的用户:
echo “DOCKER_OPTS=\”$DOCKER_OPTS –registry-mirror=https://fird1mfg.mirror.aliyuncs.com\”” | sudo tee -a /etc/default/docker
sudo service docker restart
ShellCopy
1.3 NVIDIA-Docker 安装

NVIDIA-Docker

Prerequisties
GNU/Linux x86_64 with kernel version > 3.10
Docker >= 1.9 (official docker-engine, docker-ce or docker-ee only)
NVIDIA GPU with Architecture > Fermi (2.1)
NVIDIA drivers >= 340.29 with binary nvidia-modprobe (驱动版本与CUDA计算能力相关)
TxtCopy
CUDA与NVIDIA driver安装
处理NVIDIA-Docker依赖项 NVIDIA drivers >= 340.29 with binary nvidia-modprobe要求.
根据显卡,下载对应版本的 CUDA 并进行安装.
NVIDIA-Docker安装

Install nvidia-docker and nvidia-docker-plugin

wget -P /tmp https://github.com/NVIDIA/nvidia-docker/releases/download/v1.0.1/nvidia-docker_1.0.1-1_amd64.deb
sudo dpkg -i /tmp/nvidia-docker.deb && rm /tmp/nvidia-docker.deb

Test nvidia-smi

sudo nvidia-docker run –rm nvidia/cuda nvidia-smi
ShellCopy
2. Caffe镜像使用

这里使用阿里云的镜像服务.,从Docker注册服务器的Docker仓库下载一个已有的Docker镜像.

登录阿里云
sudo docker login registry.aliyuncs.com
Username: **
Password: ** # Registry 密码,非阿里云账户密码
ShellCopy
拉取Caffe镜像
sudo nvidia-docker pull registry.cn-hangzhou.aliyuncs.com/docker_learning_aliyun/caffe:v1
ShellCopy
查看拉取的Caffe镜像信息
sudo nvidia-docker images
ShellCopy
输出信息:如
《Docker - 基于NVIDIA-Docker的Caffe-GPU环境搭建》

查看Caffe镜像的显卡信息
sudo nvidia-docker run -it registry.cn-hangzhou.aliyuncs.com/docker_learning_aliyun/caffe:v1 nvidia-smi
ShellCopy
输出信息:如
《Docker - 基于NVIDIA-Docker的Caffe-GPU环境搭建》

进入容器,操作类似于Ubuntu系统,默认进入容器内的 /workspace 目录
sudo nvidia-docker run -it registry.cn-hangzhou.aliyuncs.com/docker_learning_aliyun/caffe:v1 /bin/bash
ShellCopy
基于容器Caffe镜像运行python程序
sudo nvidia-docker run –volume= ( p w d ) : / w o r k s p a c e v o l u m e = / p a t h / t o / d a t a : / d a t a r m c a f f e i m a g e : b a s e p y t h o n d e m o . p y S h e l l C o p y 1. n v i d i a d o c k e r r u n 2. v o l u m e = (pwd):/workspace –volume=/path/to/data:/data: 将主机的路径挂载到容器中, “:”前后分别为为主机目录和容器路径
3. -rm: 运行镜像后删除
4. caffe-image:base: Caffe镜像.
5. python demo.py: python程序运行,类似于Ubuntu环境.
3. Docker 错误解决

3.1 python 提示错误UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position

错误:

docker 中 python 代码的 print(“中文”) 出现错误,但宿主机不会出现该错误,如下:
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-127: ordinal not in range(128)
TxtCopy
错误原因:

因为locale 的设置导致 shell 的stdin/stdout/stderr 的默认编码为ascii,当用ascii编码去解释python3默认unicode编码的时候,则会有问题
TxtCopy
解决方法:

python3 的解决方式是 容器在初始化时候 需要设置shell的stdin/stdout/stderr 的默认编码方式为 utf-8,需要重启容器
docker run 方式
docker run -e PYTHONIOENCODING=utf-8 m_container:latest my-python3

docker-compose 方式
environment:
- PYTHONIOENCODING=utf-8
TxtCopy
docker python 提示错误UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position

3.2 start: Job failed to start invoke-rc.d: initscript docker, action “start” failed.

解决方案:

sudo apt-get purge docker-ce # 卸载删除
sudo rm -rf /var/lib/docker # 删除配置
sudo apt-get install docker-ce # 重新安装
ShellCopy
3.3 docker: Error response from daemon: pull access denied for hello, repository does not exist or may require ‘docker login’.

在运行测试 docker hello world 时出现错误:

sudo docker run hello world
ShellCopy
Unable to find image ‘hello:latest’ locally
docker: Error response from daemon: pull access denied for hello, repository does not exist or may require ‘docker login’.
TxtCopy
需要 docker login 到 Docker Hub:

docker login -u 用户名 -p 密码
docker logout # 登出 Docker Hub
ShellCopy
3.4 Error response from daemon: Get https://registry.cn-hangzhou.aliyuncs.com/v2/: unauthorized: authentication required

登录的阿里云账户的密码是 Registry 独立密码, 而不是阿里云的登录密码.
Registry的登陆密码是在容器镜像服务的控制台上设置与修改的。

猜你喜欢

转载自blog.csdn.net/intjun/article/details/81903075