【Docker】使用docker配置深度学习开发环境

1、run一个cuda环境:

docker run --runtime=nvidia -v /home:/home -w /home/jay -it --entrypoint bash -d --name pytorch1.3-lzc -h pytorch1.3 --shm-size 32G nvidia/cuda:10.1-cudnn7-devel-ubuntu16.04

2、进入容器之后,“啥都没有!!!”,先换apt源:

  • 可先解决上网问题,然后直接使用阿里云的源,不使用内网服务器上的源(源比较旧);
  • 编辑 /etc/apt/sources.list;
  • 输入一下内容:
# 内网服务器的源
deb [arch=amd64] http://192.168.202.12/apt/mirror/mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb [arch=amd64] http://192.168.202.12/apt/mirror/mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb [arch=amd64] http://192.168.202.12/apt/mirror/mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb [arch=amd64] http://192.168.202.12/apt/mirror/mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb [arch=amd64] http://192.168.202.12/apt/mirror/mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse

# 以上为内网服务器上的源,可能不够用,可加入以下源:
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse 
  • 换成以上内容以后,一定要更新源:

apt-get update
  • 运行以上命令会卡住,原因是找不着nvidia驱动和cuda的源,删除:
rm -rf /etc/apt/sources.list.d
  • 再次更新源,成功:
apt-get update

3、安装vim:

apt-get install vim

4、安装Python3.5,安装之后直接输入Python,会找不到,输Python3.5就好了,在bash里设置一下就好了:

apt-get install python3.5

5、安装pip:

apt-get install python3-pip

6、更新pip:

pip3 install --upgrade pip

7、升级pip后出现ImportError: cannot import name main:

  • 解决办法:

    cd /usr/bin
    vim pip3
    
    # 将下面3行:
    from pip import main
    if __name__ == '__main__':
        sys.exit(main())
    
    # 改成:
    from pip import __main__
    if __name__ == '__main__':
        sys.exit(__main__._main())
  • 参考资料:升级pip后出现ImportError: cannot import name main

8、更换pip源:

  • 编辑~/.pip/pip.conf,没有的话就新建,加入以下内容(其他源自行搜索):
[global]
index-url = http://mirrors.aliyun.com/pypi/simple
trusted-host = mirrors.aliyun.com

# 其他源:
  阿里云 https://mirrors.aliyun.com/pypi/simple/
  中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  豆瓣(douban) http://pypi.douban.com/simple/
  清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

9、解决上网问题,在bashrc里面编辑proxy

至此,基本配置已完成,使用apt install安装软件没有问题,使用pip安装各种Python包也没有问题。

 


Optional:

1、将shell由bash换成zsh:

  • 安装zsh:
apt-get install zsh
  • (optional)更换shell(安装好oh-my-zsh之后,自动切换成zsh):
chsh -s /bin/zsh

# 换回bash:
chsh -s /bin/bash

2、安装oh-my-zsh(安装的时候会让你选是否将默认shell换成zsh,选y):

# 安装wget
apt-get install wget
# 安装curl
apt-get install curl
# 安装git
apt-get install git
# 安装oh-my-zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# 或者
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

3、更换zsh主题:

# 打开配置文件
vim ~/.zshrc

# 更改主题
ZSH_THEME="frisk"

4、安装tmux:

apt-get install tmux

 


修改默认Python:

# 查看系统中有哪些Python版本:
ls /usr/bin/python*

# 打开用户配置文件:
vim ~/.zshrc

#添加新的别名来修改默认Python版本:
alias python='/usr/bin/python3.5'

参考:【Python】Ubuntu修改默认Python版本

 


在docker里安装opencv,调用时报错:

1、报错:ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

apt-get install libglib2.0-dev

2、报错:ImportError: libSM.so.6: cannot open shared object file: No such file or directory

# 安装apt-file
apt-get install apt-file
# 更新
apt-file update
# 寻找依赖
apt-file search libSM.so.6
# 安装依赖
apt-get install libsm6

3、报错:ImportError: libXrender.so.1: cannot open shared object file: No such file or directory

apt-get install libxrender1

4、日!终于行了!

 

 


修改容器的挂载目录:

提交现有容器为新镜像,然后重新运行它:

# 查看现有容器id
docker ps -a
# 提交现有容器为新镜像
docker commit 5a3422adeead newimagename
# 重新运行,挂载/mnt/data
docker run --runtime=nvidia -v /home:/home -v /mnt/data:/mnt/data -w /home/jay -it --entrypoint bash -d --name pytorch1.3.1-lzc -h pytorch --shm-size 32G pytorch1.3-cuda10.1:latest	

参考:docker-修改容器的挂载目录三种方式

 


解决docker容器内ubuntu镜像中文乱码问题:

  • 在ubantu系统中设置;
# 编辑zshrc或者bashrc
vim ~/.zshrc
# or
vim ~/.bashrc

# 在最后添加以下3句;
export LC_ALL="C.UTF-8"
export LANG="C.UTF-8"
export LANGUAGE="C.UTF-8"
  • 退出容器(CTRL+P  CTRL+Q),重新登入就好了。

参考:docker 学习 - 解决ubuntu镜像中文乱码问题

Docker容器开发环境转移

使用场景:

在84服务器上配置好一个docker环境之后,想把该环境部署到10服务器上使用

# 将84服务器上的docker容器提交为镜像
docker commit container_id image_name:tag
# 将镜像打包为压缩文件
docker save image_id -o /somepath/image_name.tar
# 将镜像的压缩文件传输到10服务器
scp /somepath/image_name.tar jay@192.168.202.10:~/packages
# 在10服务器上将压缩文件load为镜像
docker load < image_name.tar 
# 此时,在10服务器上运行docker ps,便可以看到该镜像,但是其image_name和tag均为none,重命名镜像:
docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签)
# 接下来,将镜像run起来就好了。

版权声明:本文为zhicai_liu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。