[自记录]一次Nvidia显卡的AI容器基础镜像制作过程(含Torch版本和ONNXRuntime版本选择)
即实际运行依赖的cuda版本12.1(不能高于宿主机但最好接近),cudnn版本9.01。如果用较低版本的torch,还需要关注cuDNN的版本不要冲突。默认apt按照的python版本为3.10.12。下载,不建议pip安装。torch版本建议在。
1 宿主机情况
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.161.07 Driver Version: 535.161.07 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 Tesla V100S-PCIE-32GB Off | 00000000:00:0D.0 Off | 0 |
| N/A 28C P0 37W / 250W | 15572MiB / 32768MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
2 制作镜像
2.1 基础镜像选型
官方分base,runtime和devel。一般建议用runtime,但是实测base也可以。
是否带cudnn看个人选择,如果你只用onnx的话,由于onnx自完备了,不需要,省点空间。
如果有网络限制,可以在这里查看对应的国内镜像资源。
比方说我们选定了nvidia/cuda:12.2.0-runtime-ubuntu22.04 ,根据界面可得swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nvidia/cuda:12.2.0-runtime-ubuntu22.04 这个对应地址。
docker run --gpus=all --rm -it swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nvidia/cuda:12.2.0-runtime-ubuntu22.04
apt install python3 python3-pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
默认apt按照的python版本为3.10.12。
2.1.1 其他系统改造
默认的系统装的很简陋,建议至少补充安装以下包方便维护。
参考ubuntu镜像站,切换apt源到国内
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse" > /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse" >> /etc/apt/sources.list
apt-get update
apt install psmisc vim wget curl iputils-ping net-tools
pip3 install nvitop
echo "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" >> /etc/vim/vimrc
echo "set termencoding=utf-8" >> /etc/vim/vimrc
echo "set encoding=utf-8" >> /etc/vim/vimrc
2.1.2 指定python版本
*安装vim也会引入安装python,所以这种情况下务必先装python。
目前较新的ubuntu容器连接镜像源,会将python升级到3.12,需要人工指定成3.10或希望的版本。
可以使用apt-cache showpkg python3 查看实际可选的版本,然后使用人工安装pip的方式执行。(这样安装出来是没有gcc的)
apt install python3=3.10.6-1~22.04.1 wget
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
2.2 torch版本选择
torch版本建议在torch官网下载,不建议pip安装
2.3 onnxruntime选择
pip3 install onnxruntime-gpu[cuda,cudnn]
*不要同时安装onnxruntime,如果onnxruntime的版本比gpu的新,会使用onnxruntime。
onnxruntime版本兼容性检查
如果用较低版本的torch,还需要关注cuDNN的版本不要冲突。
3 结果展示
- 以下是基于
nvidia/cuda:12.2.2-base-ubuntu22.04制作含torch版本的结果。
$ python3
Python 3.10.12 (main,***) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
2.5.1+cu121
>>> print(torch.version.cuda)
12.1
>>> print(torch.backends.cudnn.version())
90100
即实际运行依赖的cuda版本12.1(不能高于宿主机但最好接近),cudnn版本9.01
- 以下是基于
cuda:12.2.0-runtime-ubuntu22.04制作仅集成onnxruntime-gpu的结果
Python 3.10.12 (main, ***) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import onnxruntime as ort
>>> print(ort.get_device()) # 'GPU' or 'CPU'
GPU
更多推荐



所有评论(0)