简单的介绍
引用别人的:几个优点
- 可以单独配置开发环境,防止不同项目使用的环境配置不同而发生环境冲突问题
- 轻松配置不同的cuda/python/各种库的版本
- 充分保护主机环境不会因为环境搭建失误而发生系统崩溃
- 便于项目迁移和部署
具体步骤
写在前面,本人这里以ubuntu20.04 LTS为例,如果你是其他系统请留意安装中可能存在的不同
1. 安装英伟达驱动
(已有可以跳过,新装/重装可以接着看)
参考这篇:https://blog.motorbottle.site/archives/248
2. 安装docker环境
apt update
apt install curl -y
curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh
如果docker被墙了无法访问可以参考我的自建镜像源教程。
3. 安装NVIDIA Container Toolkit
Follow官方文档永远是最好的,这里给出链接
https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
Installing with Apt
1. Configure the production repository:
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
Optionally, configure the repository to use experimental packages:
sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/nvidia-container-toolkit.list
2. Update the packages list from the repository:
sudo apt-get update
3. Install the NVIDIA Container Toolkit packages:
sudo apt-get install -y nvidia-container-toolkit
4. 配置Docker
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
4. 测试
拉取Nvidia提供的镜像,产看在镜像中是否能够使用nvidia-smi
sudo docker run --rm --gpus all nvidia/cuda:11.6.1-devel-ubuntu20.04 nvidia-smi
你也可以使用指令nvcc -v查看容器内cuda版本是否正确,出现以下内容则安装成功
5. 拉取镜像并构建容器
关于Nvidia提供的镜像版本的说明:
Dockerhub官网上面,Nvidia官方提供了一些特定CUDA版本的镜像,地址:https://hub.docker.com/r/nvidia/cuda/tags?page=1&ordering=last_updated
可以直接拉取相关镜像,查询的技巧是cuda版本号带上你的系统版本,例如搜ubuntu20.04然后找对应的cuda版本。
我们以查询CUDA12.2.0的版本为例,可以发现Nvidia提供的镜像有三个版本,分别是base、runtime和devel,从镜像大小可以发现,三种镜像的区别如下:
- base:这个版本从CUDA9.0开始,包含了部署预构建CUDA应用程序的最低限度。如果你想自己安装需要的CUDA包,可以选择使用这个镜像版本,但如果想省事,不建议使用该镜像
- runtime:这个版本通过添加CUDA工具包中的所有共享库来扩展base镜像,如果使用多个CUDA库的预构建应用程序(也就是已经编译好的程序),可使用此镜像。但是如果想借助CUDA中的头文件对自己的工程进行编译,则会出现找不到头文件的错误。
- devel:这个版本通过添加编译器工具链、测试工具、头文件和静态库来扩展runtime镜像,使用此镜像可以从源代码编译CUDA应用程序,如果想用docker镜像做开发环境,建议使用devel版本的镜像。
注意:一般都使用devel镜像。
我个人喜欢用docker compose来创建容器,因为比较适合对容器做复杂的配置:
我们在服务器上创建一个用于配置容器的路径,例如 ~/training
。随后在路径下创建docker compose的配置文件docker-compose.yml:
services:
digit_training: # Your project's name
image: nvidia/cuda:11.6.1-devel-ubuntu20.04
runtime: nvidia
container_name: digit_training # Your container's name
environment:
- NVIDIA_VISIBLE_DEVICES=all # Use all GPUs
volumes:
- ./workspace:/workspace # Map working directory
ports:
- "18888:8888" # Map host port 18888 to container port 8888 for jupyter notebooks
stdin_open: true
tty: true
ipc: host # Share IPC namespace with host for large shared memory
- image: 你要指定的cuda版本对应的容器tag
- container_name: 给你的容器起个名字
- environment: NVIDIA_VISIBLE_DEVICES用于指定分配给容器的显卡,例如我的smi有3张卡,我只想把0号和1号分进来,那么就是
- NVIDIA_VISIBLE_DEVICES=0,1
- volumes: 在当前目录下新建一个workspace目录,用于和主系统共通文件,可以存放你的jupyter notebook、数据集、权重等等
- shm_size: 可选,限定以下共享内存大小,根据你自己的机器配置决定
- ports: 给你的jupyter notebook配置一个外部端口以备未来访问
配置完成后保存文件,在该目录下运行指令`docker compose up -d`创建容器。
20250220 – 补充:你也可以用pytorch官方预装好torch相关库的镜像,conda、torch、cuda都配置好了,可以省掉下面的一些步骤
services:
minicpm:
image: pytorch/pytorch:2.6.0-cuda12.6-cudnn9-devel
runtime: nvidia
container_name: minicpm
environment:
- NVIDIA_VISIBLE_DEVICES=0,1
volumes:
- ./workspace:/workspace # Map working directory
shm_size: '64g' # shared memory size
ports:
- "19088:8088"
- "19089:8089"
stdin_open: true
tty: true
6. 安装miniconda并初始化训练环境
命令行进入容器:
# docker exec -it 你刚刚设置的容器名称 bash,例如:
docker exec -it digit_training_container bash
miniconda安装的官方文档:https://docs.anaconda.com/miniconda/
我的方法(安装在/opt/miniconda并执行初始化):
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p /opt/miniconda
/opt/miniconda/bin/conda init bash
安装完成后,为你的项目创建一个新的conda环境,以名称digit_training为例:
source ~/.bashrc
conda create -n digit_training python=3.8 -y
conda activate digit_training
安装各种库:
conda install -y jupyter matplotlib numpy pandas scikit-learn
安装pytorch相关库:
前往此链接,按照一图中选择后,获取相关指令,如果是跟老版本的cuda,点击PREVIOUS VERSIONS OF PYTORCH查看安装的指令,如二图
7. 开启jupyter notebook开始你的工作
jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root --NotebookApp.token=''
这样在主系统127.0.0.1:18888即可访问jupyter notebook开始你的训练啦。
请注意将你的各种文件放置到/workspace文件夹内,这样在主系统的对应文件夹(你映射的路径)就能操作这部分数据了,更新容器的时候不会把这些东西一起保存进去,而是持久化保存在host系统中。
你可以在jupyter notebook添加以下代码块来验证配置是否完成:
import torch
print("CUDA available:", torch.cuda.is_available())
print("CUDA version:", torch.version.cuda)
print("Number of GPUs:", torch.cuda.device_count())
print("cuDNN available:", torch.backends.cudnn.is_available())
print("cuDNN version:", torch.backends.cudnn.version())
8. 环境的保存和迁移
为了保存你在Docker容器中安装的库和环境,以便以后可以方便地恢复或在其他机器上复用,你有几个选择。常用的几种方式有docker commit保存容器为新的镜像, dockerfile记录环境等,这里介绍docker commit
使用 docker commit
保存容器为新的镜像
docker commit
命令可以将当前容器的状态保存为一个新的镜像。这样你就不需要每次重新安装库和配置环境。
步骤:
确保容器正在运行并且你已经安装了所有需要的库和配置。
使用 docker commit
命令将当前容器保存为新的镜像:docker commit <container_id> my_new_image_name
<container_id>
是你当前运行的容器的ID,可以通过 docker ps
查看。
my_new_image_name
是你保存的新镜像名称,可以随意命名。
之后,你可以通过新镜像启动一个容器:docker run -it --gpus all my_new_image_name
参考链接
https://blog.csdn.net/m0_55127902/article/details/138074763