基于cuda镜像构建Python环境
基于cuda镜像构建Python环境
准备工作
- 在linux主机安装docker(参考 https://docs.docker.com/engine/install/#server)
- 在linux主机的工作目录编写 sources.list:
vim sources.list
# 中科大ubuntu镜像
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
1 拉取镜像并运行容器
# 拉取镜像并运行容器(主机中执行)
docker pull nvidia/cuda:10.1-cudnn7-runtime-ubuntu16.04
docker run -it --name test_01 --gpus all nvidia/cuda:10.1-cudnn7-runtime-ubuntu16.04
# 查看操作系统版本和显卡信息(容器中执行)
cat /etc/issue # Ubuntu 16.04.7 LTS \n \l
nvidia-smi # CUDA Version: 10.1
# 更新软件源
docker cp sources.list test_01:/etc/apt/sources.list # (主机中执行)
apt-get update # (容器中执行)
2 安装Python(容器中执行)
从Python官网获取所需版本的下载链接(这里安装Python-3.6.12)
# 安装相关软件
apt-get -y install vim wget gcc make automake libtool zlibc tk-dev libssl-dev
apt-get -y install libglib2.0-dev libsm6 libxrender1 libxext-dev libreadline-dev
# 下载并解压 Python 源码(Gzipped source tarball)
cd /root && wget https://www.python.org/ftp/python/3.6.12/Python-3.6.12.tgz
tar -zxvf Python-3.6.12.tgz
# 安装 Python
cd Python-3.6.12
./configure --prefix=/usr/local --with-ssl # 生成 Makefile
make && make install # 编译并安装python
# 创建快捷方式并让python3.6能找到_bz2
unlink /usr/bin/python
ln -s /usr/local/bin/python3 /usr/bin/python && ln -s /usr/local/bin/pip3 /usr/bin/pip
cp /usr/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so /usr/local/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
# 测试能否导入bz2和tkinter
python
>>> import bz2
>>> import tkinter
相关软件的作用
软件 | 作用 |
---|---|
vim wget | 编辑器和下载工具 |
gcc make automake libtool | 编译工具 |
zlibc | 数据压缩 |
tk-dev | tkinter |
libssl-dev | pip install 需要使用 ssl |
libglib2.0-dev libsm6 libxrender1 libxext-dev | import cv2 需要的依赖 |
libreadline-dev | 在python命令行中使用backspace等 |
Note:因为tk-dev依赖于tk,所以安装tk-dev时会自动安装tk(查看软件依赖关系的命令为:apt-cache depends tk-dev
)
3 安装常用Python库
- 准备 requirements.txt(可从其他环境导出:
pip freeze > requirements.txt
)
chardet==4.0.0
Flask==1.1.2
lmdb==1.0.0
matplotlib==2.2.2
numpy==1.19.5
opencv-python==3.4.2.16
PyYAML==5.4.1
requests==2.25.1
torch==1.1.0
torchvision==0.3.0
XlsxWriter==1.3.7
- 安装Python库:
pip install -r requirements.txt --no-cache-dir
4 Dockerfile
若不想频繁的复制粘贴以上命令,可将以上操作通过Dockerfile实现
- vim Dockerfile(主机中执行)
FROM nvidia/cuda:10.1-cudnn7-runtime-ubuntu16.04
MAINTAINER zx<zx@163.com>
ENV LANG C.UTF-8
WORKDIR /root
COPY sources.list /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install vim wget gcc make automake libtool zlibc tk-dev libssl-dev
RUN apt-get -y install libglib2.0-dev libsm6 libxrender1 libxext-dev libreadline-dev
RUN wget https://www.python.org/ftp/python/3.6.12/Python-3.6.12.tgz
RUN tar -zxvf Python-3.6.12.tgz
RUN /root/Python-3.6.12/configure --prefix=/usr/local --with-ssl
RUN make && make install
RUN unlink /usr/bin/python
RUN ln -s /usr/local/bin/python3 /usr/bin/python && ln -s /usr/local/bin/pip3 /usr/bin/pip
RUN cp /usr/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so /usr/local/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
RUN rm -rf *
- 通过Dockerfile构建镜像,然后运行容器(没有GPU需删除 --gpus all )
docker build -f Dockerfile -t zxc/python .
docker run -it --name test_02 --gpus all zxc/python
- 在容器中根据需要安装相应的Python库