基于 Docker - 基于NVIDIA-Docker的Caffe-GPU环境搭建 - AIUAI 重新梳理 Docker的安装与 Nvidia-docker2 的安装.

1. Docker 安装与配置

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

1.1. Ubuntu 软件源安装

使用 Ubuntu 系统默认自带的 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安装是否成功)

1.2. Docker 官网安装

采用 Docker官网 安装方式可以获取最新版本Docker.

有两个版本:

[1] - Docker Community Edition(CE)

[2] - Docker Enterprise Edition(EE)

这里安装 Docker CE,最新版本支持 Ubuntu Disco 19.04、Ubuntu 18.10、Ubuntu Bionic 18.04 (LTS) 和 Ubuntu Xenial 16.04 (LTS)的 64 位系统.

1.2.1. 卸载旧版本 docker

Docker 旧版本叫作 dockerdocker-engine,在安装前需要先卸载旧版本.

sudo apt-get remove docker docker-engine docker.io containerd runc

/var/lib/docker 路径中,保留了镜像images,容器containers,卷volumes,以及网络networks,等.

Docker CE 新版本叫作 docker-ce.

1.2.2. Docker仓库设置

在安装Docker之前需要配置Docker官方仓库,然后从该仓库进行获取与安装.

首先,进行Docker仓库设置:

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

# Docker 官方 GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 验证 fingerprint 
sudo apt-key fingerprint 0EBFCD88

# 添加 Docker CE 的 stable 版本的仓库
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

1.2.3. Docker CE 最新版本安装

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
# 存在多个 Docker 仓库时,会安装最新版本的 docker-ce

1.2.4. Docker CE 指定版本安装

[1] - 列出仓库中所有的 Docker CE 版本

apt-cache madison docker-ce

如:

 docker-ce | 5:19.03.5~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:19.03.4~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:19.03.3~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:19.03.2~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:19.03.1~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:19.03.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.9~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.8~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.7~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.6~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.5~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.4~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.3~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.2~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.3~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.2~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.1~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.0~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.03.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.03.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.12.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.12.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.09.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.09.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.2~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.3~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.2~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages

[2] - 安装 Docker CE 指定版本

sudo apt-get install \
    docker-ce=<VERSION_STRING> \ 
    docker-ce-cli=<VERSION_STRING> \
    containerd.io

# 如:
sudo apt-get install \ 
    docker-ce=18.09.1~3-0~ubuntu-xenial \
    docker-ce-cli=18.09.1~3-0~ubuntu-xenial \
    containerd.io

1.2.5. Docker CE deb 安装(强烈推荐)

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

https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/

Ubuntu 其他版本 Docker deb 下载路径:

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

Docker deb 安装:

sudo dpkg -i /path/to/docker-ce.deb

1.3. 查看 Docker 状态

sudo service docker status 
sudo docker info

1.4. Docker - hello-world 镜像测试

sudo docker run hello-world

自动下载 hello-world 镜像,并在容器内运行. 输出如:

Hello from Docker!

2. 阿里云加速器设置

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

推荐安装 Docker CE > 1.10.0 版本.

通过修改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

3. NVIDIA-Docker 安装与测试

NVIDIA-Docker

Nvidia-Docker 已经升级到 2.0 版本.

3.1. Nvidia-Docker 安装

Ubuntu 16.04/18.04 安装:

distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

sudo apt-get update 
sudo apt-get install nvidia-docker2
#sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker

3.2. nvidia/cuda 镜像测试

sudo nvidia-docker run --rm nvidia/cuda:10.0-cudnn7-devel-ubuntu16.04 nvidia-smi

4. 问题解决

4.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)

错误原因:

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

解决方法:

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

From docker python 提示错误UnicodeEncodeError: 'ascii' codec can't encode characters in position

4.2. docker: Error response from daemon: Unknown runtime specified nvidia

问题:

docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
# 出错:
# docker: Error response from daemon: Unknown runtime specified nvidia.

解决方法:

#重启 docker 服务
sudo systemctl daemon-reload
sudo systemctl restart docker
Last modification:November 27th, 2019 at 01:37 pm