從 0 到 1 實(shí)現(xiàn)基于 Docker 的深度學(xué)習(xí)環(huán)境的安裝與配置!
在當(dāng)今人工智能技術(shù)飛速發(fā)展的時(shí)代,深度學(xué)習(xí)作為核心驅(qū)動(dòng)力之一,正引領(lǐng)著科研與產(chǎn)業(yè)界的革新。搭建一個(gè)高效、可靠的深度學(xué)習(xí)環(huán)境對(duì)于開(kāi)發(fā)者和研究人員來(lái)說(shuō),是最基本的需求。
本文筆者將從0到1實(shí)現(xiàn)基于Docker的深度學(xué)習(xí)環(huán)境的安裝與配置!與大家分享~
本文中的方法,可用于Nvidia顯卡及常見(jiàn)硬件,如:A100服務(wù)器、RTX4090家用顯卡。 操作系統(tǒng)以Ubuntu為例。
一、安裝Ubuntu操作系統(tǒng)
安裝Ubuntu的流程和以往并沒(méi)有太大不同,依舊是老生常談的三步曲:下載鏡像、制作啟動(dòng)盤(pán)、安裝系統(tǒng)。
1.下載合適的Ubuntu鏡像
首先訪(fǎng)問(wèn)Ubuntu官方網(wǎng)站,下載所需要的系統(tǒng)版本:
- 桌面版:https://releases.ubuntu.com/22.04/ubuntu-22.04-desktop-amd64.iso
- 服務(wù)器版:https://releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso
其中,服務(wù)器版適合于不需要圖形界面的服務(wù)器環(huán)境,或者構(gòu)建高度定制化的系統(tǒng)。它更側(cè)重于性能優(yōu)化、遠(yuǎn)程管理以及自動(dòng)化運(yùn)維,適合云服務(wù)器部署、容器化應(yīng)用、數(shù)據(jù)庫(kù)服務(wù)器等場(chǎng)景。桌面版提供了完整的圖形用戶(hù)界面(GUI),適合日常使用、開(kāi)發(fā)環(huán)境搭建及需要直接交互的工作站。它預(yù)裝了許多日常所需的軟件和實(shí)用工具,以及對(duì)各種硬件的良好支持,包括自動(dòng)安裝大部分驅(qū)動(dòng)程序,這對(duì)于筆記本電腦用戶(hù)尤為便利。
2.制作系統(tǒng)安裝盤(pán)
這里使用的是Balena Etcher工具制作系統(tǒng)安裝盤(pán)。
下載完畢軟件之后,打開(kāi)軟件,選擇我們下載好的系統(tǒng)鏡像,以及要制作成安裝盤(pán)的U盤(pán),點(diǎn)擊“制作”按鈕,稍等片刻,安裝盤(pán)就制作完成啦。
3.安裝系統(tǒng)
(1) 設(shè)置BIOS/UEFI啟動(dòng)順序
- 重啟計(jì)算機(jī),并在啟動(dòng)畫(huà)面出現(xiàn)時(shí)按下指定鍵(通常是F2、F10、F12、Del等,具體取決于主板型號(hào))進(jìn)入BIOS或UEFI設(shè)置。
- 尋找“Boot”或“啟動(dòng)”設(shè)置,將USB HDD或包含USB字樣的設(shè)備調(diào)整為第一啟動(dòng)項(xiàng)。
- 保存更改并退出,計(jì)算機(jī)將自動(dòng)重啟并從U盤(pán)啟動(dòng)。
(2) 啟動(dòng)并進(jìn)入安裝界面
- 當(dāng)看到Ubuntu的Logo出現(xiàn)時(shí),表明系統(tǒng)已成功從U盤(pán)啟動(dòng)。稍作等待,安裝程序?qū)⒆詣?dòng)加載。
- 加載完畢后,會(huì)看到Ubuntu安裝向?qū)У牡谝粋€(gè)界面,選擇“Install Ubuntu”。
(3) 安裝過(guò)程中的額外驅(qū)動(dòng)選項(xiàng)
在安裝過(guò)程中,安裝程序可能會(huì)檢測(cè)到您的系統(tǒng)可能需要額外的硬件驅(qū)動(dòng),尤其是對(duì)于Nvidia顯卡等。這時(shí),您會(huì)看到一個(gè)詢(xún)問(wèn)是否安裝第三方軟件(包括MP3編解碼器、Flash插件以及專(zhuān)有硬件驅(qū)動(dòng))的選項(xiàng)。如果不確定,建議勾選此選項(xiàng),以確保安裝后系統(tǒng)能立即識(shí)別并充分利用所有硬件功能。
(4) 等待安裝完成
- 點(diǎn)擊“Continue”開(kāi)始安裝過(guò)程,這可能需要一段時(shí)間,請(qǐng)耐心等待。
- 安裝結(jié)束后,您會(huì)被提示重啟系統(tǒng)。移除U盤(pán),點(diǎn)擊“Restart Now”。
重啟后,將直接進(jìn)入新安裝的Ubuntu系統(tǒng)登錄界面,使用之前設(shè)置的用戶(hù)名和密碼登錄。
二、系統(tǒng)基礎(chǔ)環(huán)境配置
安裝完Ubuntu系統(tǒng)后的首要任務(wù)之一就是進(jìn)行系統(tǒng)更新,以確保系統(tǒng)擁有最新的安全補(bǔ)丁、軟件包升級(jí)和 bug 修復(fù)。
sudo apt update && sudo apt -y upgrade
如果覺(jué)得更新軟件速度太慢,可以換國(guó)內(nèi)鏡像源,如清華大學(xué)鏡像源。
sudo sed -i -e "s/cn.archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/" /etc/apt/sources.list
sudo sed -i -e "s/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/" /etc/apt/sources.list
等待軟件和系統(tǒng)補(bǔ)丁更新完畢之后,執(zhí)行重啟操作,讓補(bǔ)丁生效即可(首次更新,會(huì)更新內(nèi)核)。
sudo reboot
安裝 OpenSSH Server
不論是選擇桌面版操作系統(tǒng),還是選擇服務(wù)端操作系統(tǒng),默認(rèn)情況下系統(tǒng)中不會(huì)包含 openssh-server 這個(gè)組件,如果有從局域網(wǎng)其他設(shè)備訪(fǎng)問(wèn)這臺(tái)Linux設(shè)備的需求,可以先執(zhí)行下面的命令,來(lái)安裝它。
sudo apt update && sudo apt install -y openssh-server
程序安裝完畢后即可執(zhí)行ssh username@host-ip訪(fǎng)問(wèn)Linux服務(wù)器。如果要登錄Linux使用的設(shè)備的用戶(hù)名和Linux允許登錄的用戶(hù)名一致,則可省略 “username”。
ssh 10.11.12.240
The authenticity of host '10.11.12.240 (10.11.12.240)' can't be established.
ED25519 key fingerprint is SHA256:cYodQ6Chywyna1JbHWfA7XAFonHKAz48cPmjRyVOCFU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.11.12.240' (ED25519) to the list of known hosts.
soulteary@10.11.12.240's password:
首次登錄的時(shí)候,需要先輸入yes讓當(dāng)前的設(shè)備信任目標(biāo)設(shè)備的指紋,然后輸入密碼,就能夠看到熟悉的終端提示信息了:
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-25-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
189 updates can be applied immediately.
73 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
三、安裝顯卡驅(qū)動(dòng)
可以通過(guò) nvidia-detector 來(lái)獲取最新的穩(wěn)定版本的驅(qū)動(dòng)。
# nvidia-detector
nvidia-driver-525
在安裝驅(qū)動(dòng)之前,暫時(shí)是不能使用 nvidia-smi 管理工具的。
# nvidia-smi
zsh: command not found: nvidia-smi
安裝驅(qū)動(dòng)時(shí),建議除了安裝 nvidia-driver 驅(qū)動(dòng),可以順帶安裝 nvidia-dkms ,方便后續(xù)如果需要升降級(jí)內(nèi)核的時(shí)候,減少不必要的麻煩:
sudo apt-get install -y nvidia-driver-525 nvidia-dkms-525
完成驅(qū)動(dòng)安裝之后,再次執(zhí)行nvidia-smi,就可以進(jìn)行顯卡管理啦。
# nvidia-smi
Tue Mar 21 22:53:37 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.85.05 Driver Version: 525.85.05 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| 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 NVIDIA GeForce ... Off | 00000000:01:00.0 On | Off |
| 31% 34C P8 19W / 450W | 53MiB / 24564MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1331 G /usr/lib/xorg/Xorg 36MiB |
| 0 N/A N/A 1552 G /usr/bin/gnome-shell 15MiB |
+-----------------------------------------------------------------------------+
四、GPU Docker環(huán)境的安裝和配置
1.宿主機(jī)Docker基礎(chǔ)環(huán)境安裝
參考Docker官方文檔,進(jìn)行Docker的快速安裝配置,確保Docker服務(wù)運(yùn)行正常。
(1) 使用apt remove命令移除可能存在的舊版Docker相關(guān)軟件包,避免沖突。
sudo apt remove -y docker docker-engine docker.io containerd runc
(2) 安裝必要的系統(tǒng)工具和庫(kù),如ca-certificates、curl、gnupg和lsb-release。
sudo apt install -y ca-certificates curl gnupg lsb-release
(3) 下載軟件包簽名使用的 GPG 密鑰,并配置系統(tǒng)信任該密鑰。
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
如果無(wú)法訪(fǎng)問(wèn)官方地址,可以將密鑰下載地址替換為下面的地址。
# 清華源
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg
# 阿里云
https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg
(4) 創(chuàng)建一個(gè)適合于當(dāng)前 CPU 架構(gòu)和系統(tǒng)版本的軟件源。
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
同樣的,如果希望能夠更快的下載到軟件,可以配置軟件源來(lái)替換官方地址。
# 清華源
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/ \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 阿里云
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu/ \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
最后一步,就是安裝 Docker 的社區(qū)版,以及常用的CLI命令。
sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
2.安裝Docker顯卡運(yùn)行時(shí)
想要在Docker中能夠“調(diào)用顯卡”,需要安裝“NVIDIA容器工具包存儲(chǔ)庫(kù)”。
distribution=ubuntu22.04 && \
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/$distribution/libnvidia-container.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
命令執(zhí)行完畢之后,系統(tǒng)中就添加好了Lib Nvidia Container工具的軟件源,然后更新系統(tǒng)軟件列表,使用命令安裝 nvidia-container-toolkit 即可:
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
完成 nvidia-container-toolkit 的安裝之后,繼續(xù)執(zhí)行nvidia-ctk runtime configure命令,為Docker添加nvidia運(yùn)行時(shí)。完成后,我們的應(yīng)用就能在容器中使用顯卡資源了:
sudo nvidia-ctk runtime configure --runtime=docker
命令執(zhí)行成功,將看到類(lèi)似下面的日志輸出:
# sudo nvidia-ctk runtime configure --runtime=docker
INFO[0000] Loading docker config from /etc/docker/daemon.json
INFO[0000] Successfully loaded config
INFO[0000] Flushing docker config to /etc/docker/daemon.json
INFO[0000] Successfully flushed config
INFO[0000] Wrote updated config to /etc/docker/daemon.json
INFO[0000] It is recommended that the docker daemon be restarted.
在完成配置之后,重啟 docker 服務(wù),讓配置生效:
sudo systemctl restart docker
服務(wù)重啟完畢,查看Docker運(yùn)行時(shí)列表,能夠看到 nvidia 已經(jīng)生效。
# docker info | grep Runtimes
Runtimes: nvidia runc io.containerd.runc.v2
五、安裝AI相關(guān)Docker鏡像并使用
相比較直接安裝和配置深度學(xué)習(xí)應(yīng)用所需要的環(huán)境,通過(guò)Docker,可以下載到各種具備不同能力的“開(kāi)箱即用”的環(huán)境,通??梢詮南旅嫒齻€(gè)地址獲取基礎(chǔ)鏡像:
- https://catalog.ngc.nvidia.com/containers
- https://hub.docker.com/u/nvidia
- https://hub.docker.com/u/pytorch
諸如在RTX 4090這類(lèi)卡剛發(fā)布后,相比較自己從零到一構(gòu)建鏡像,官方鏡像是個(gè)不錯(cuò)的額外選項(xiàng),能夠更好發(fā)揮顯卡性能,還不需要折騰。
舉個(gè)例子,如果想使用最新的CUDA版本,搭配一個(gè)能開(kāi)箱即用的 PyTorch 環(huán)境,而此時(shí) Conda 社區(qū)還未做兼容適配,最好的選擇不是去翻不同軟件包社區(qū),做一些Hack完成安裝,而是直接使用官方的鏡像。
比如,一條命令,就能夠啟動(dòng)一個(gè)包含了最新版本的 CUDA 和 PyTorch 的實(shí)驗(yàn)環(huán)境(環(huán)境的發(fā)布文檔):
docker run --gpus all -it --rm nvcr.io/nvidia/pytorch:23.02-py3
當(dāng)然,也可以調(diào)整命令,比如執(zhí)行nvidia-smi來(lái)檢查運(yùn)行環(huán)境以及獲取顯卡的狀態(tài):
# docker run --gpus all -it --rm nvcr.io/nvidia/pytorch:23.02-py3 nvidia-smi
=============
== PyTorch ==
=============
NVIDIA Release 23.02 (build 53420872)
PyTorch Version 1.14.0a0+44dac51
Container image Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Copyright (c) 2014-2023 Facebook Inc.
Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu)
Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
Copyright (c) 2011-2013 NYU (Clement Farabet)
Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)
Copyright (c) 2006 Idiap Research Institute (Samy Bengio)
Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)
Copyright (c) 2015 Google Inc.
Copyright (c) 2015 Yangqing Jia
Copyright (c) 2013-2016 The Caffe contributors
All rights reserved.
Various files include modifications (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved.
This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:
https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license
NOTE: The SHMEM allocation limit is set to the default of 64MB. This may be
insufficient for PyTorch. NVIDIA recommends the use of the following flags:
docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 ...
Tue Mar 21 15:30:19 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.85.05 Driver Version: 525.85.05 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| 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 NVIDIA GeForce ... Off | 00000000:01:00.0 On | Off |
| 31% 33C P0 33W / 450W | 174MiB / 24564MiB | 5% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+
在上面的命令中,雖然調(diào)用了顯卡,但是輸出的日志中提醒并行計(jì)算需要的緩存是不足的。為了最佳的性能實(shí)現(xiàn),可以繼續(xù)調(diào)整命令如下:
docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 -it --rm nvcr.io/nvidia/pytorch:23.02-py3
將--gpus all替換為顯卡編號(hào),即可在多卡機(jī)器中指定某張卡來(lái)運(yùn)行程序:
docker run --gpus "0" --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 -it --rm nvcr.io/nvidia/pytorch:23.02-py3
如果希望八卡的機(jī)器只有單數(shù)卡能夠被容器訪(fǎng)問(wèn),可以調(diào)整參數(shù)為:
--gpus "1,3,5,7"