title: 【记录】win10 通过 docker 调用 nvidia urlname: -ji-lu-win10-tong-guo-docker-diao-yong-nvidia date: 2023-07-23 20:25:19 tags: ['win10', 'docker', 'nvidia', 'jupyter', 'torch']
wsl --version
直接跳转到 4. 安装 Docker
wsl -s docker-desktop
切换回正确的 distronetsh winsock reset
然后重启
将下面内容复制到文本文件中,然后将文件命名为Hyper-V.bat,然后以管理员身份运行,运行完成后重启电脑(可能需要BIOS中开启处理器虚拟化支持)。
pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /LimitAccess /ALL
使用 PowerShell 启用 Hyper-V,以管理员身份打开 PowerShell 控制台,运行以下命令:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
应用商店搜索 Ubuntu 22.04.2 LTS 并安装(可以不装)
开启Windows Subsystem for Linux
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
开启虚拟机特性
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
下载并安装 WSL2更新包
将WSL2设置成默认
wsl --set-default-version 2
运行商店中安装的 Ubuntu 22.04.2 LTS (可以不装)
换源,在系统右下角托盘图标内右键菜单选择 Settings,打开配置窗口后左侧导航菜单选择 Docker Desktop。编辑窗口内的JSON串:
{
"builder": {
"features": {
"buildkit": true
},
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"registry-mirrors": [
"https://hub-mirror.c.163.com/",
"https://docker.mirrors.ustc.edu.cn/"
]
}
测试,Powershell 中运行 docker run hello-world
右键卸载 Ubuntu 22.04.2 LTS,wsl -l -v
保证有 docker-desktop 和 docker-desktop-data 就行
Powershell 中运行 docker pull anibali/pytorch:2.0.0-cuda11.8-ubuntu22.04
启动容器并测试 cuda
docker run -p 0.0.0.0:8001:8001 --rm -it --name torch --gpus all anibali/pytorch:2.0.0-cuda11.8-ubuntu22.04 /bin/bash
python
import torch # 如果pytorch安装成功即可导入
print(torch.cuda.is_available()) # 查看CUDA是否可用
print(torch.cuda.device_count()) # 查看可用的CUDA数量
print(torch.version.cuda) # 查看CUDA的版本号
测试网络访问,注意 -p 0.0.0.0
docker run -p 0.0.0.0:8001:8001 --rm -it --name torch --gpus all anibali/pytorch:2.0.0-cuda11.8-ubuntu22.04 python -m http.server 8001
mkdir data # G:\data
docker run -p 0.0.0.0:8001:8001 -it --name torch --gpus all -v //g/data:/app/data anibali/pytorch:2.0.0-cuda11.8-ubuntu22.04 /bin/bash
conda init # 然后退出 bash
docker start torch
docker exec -it torch /bin/bash
conda install nano -c conda-forge
nano ~/.condarc
conda create -n jupyter jupyter notebook -c conda-forge
conda activate jupyter
jupyter notebook --ip 0.0.0.0 --port 8001
jupyter notebook --generate-config
nano ~/.jupyter/jupyter_notebook_config.py
jupyter notebook # http://localhost:8001/lab?token=limour
c.ServerApp.ip = '*'
c.ServerApp.port = 8001
c.ExtensionApp.open_browser = False
c.ServerApp.token = 'limour'
conda activate base
conda install ipykernel -c conda-forge
python -m ipykernel install --user --name pytorch
快速启动:docker exec -it torch /home/user/micromamba/envs/jupyter/bin/jupyter notebook