【记录】win10 通过 docker 调用 nvidia

Last updated on March 19, 2024 pm

新版WSL

  1. 访问商店获取 Windows Subsystem for Linux
  2. wsl --version 直接跳转到 4. 安装 Docker
  3. 过时教程害人
  4. 如果已经执行了第2、3步,更新后需要再执行 wsl -s docker-desktop 切换回正确的 distro
  5. 关闭 Hyper-V,只开启 WSL 和 虚拟化平台 两个可选功能
  6. netsh winsock reset 然后重启

开启 Hyper-V by 壹佰

  1. 将下面内容复制到文本文件中,然后将文件命名为Hyper-V.bat,然后以管理员身份运行,运行完成后重启电脑(可能需要BIOS中开启处理器虚拟化支持)。
1
2
3
4
5
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
  1. 使用 PowerShell 启用 Hyper-V,以管理员身份打开 PowerShell 控制台,运行以下命令:
1
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

安装 WSL2 by Ali7&MS

  1. 应用商店搜索 Ubuntu 22.04.2 LTS 并安装(可以不装)
  2. 开启Windows Subsystem for Linux
1
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  1. 开启虚拟机特性
1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  1. 下载并安装 WSL2更新包
  2. 将WSL2设置成默认
1
wsl --set-default-version 2
  1. 运行商店中安装的 Ubuntu 22.04.2 LTS (可以不装)

安装 Docker Desktop by 追逐时光者

  1. 下载 Docker Desktop Installer
  2. 换源,在系统右下角托盘图标内右键菜单选择 Settings,打开配置窗口后左侧导航菜单选择 Docker Desktop。编辑窗口内的JSON串:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"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/"
]
}
  1. 测试,Powershell 中运行 docker run hello-world

调用 nvidia by 无人知晓&bpq

  1. Ubuntu 22.04.2 LTS 中 运行 nvidia-smi
  2. 右键卸载 Ubuntu 22.04.2 LTS,wsl -l -v 保证有 docker-desktop 和 docker-desktop-data 就行
  3. Powershell 中运行 docker pull anibali/pytorch:2.0.0-cuda11.8-ubuntu22.04
  4. 启动容器并测试 cuda
1
2
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
1
2
3
4
import torch # 如果pytorch安装成功即可导入
print(torch.cuda.is_available()) # 查看CUDA是否可用
print(torch.cuda.device_count()) # 查看可用的CUDA数量
print(torch.version.cuda) # 查看CUDA的版本号
  1. 测试网络访问,注意 -p 0.0.0.0
1
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

conda by bpq&LATLAJ

1
2
3
4
5
6
7
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

jupyter

1
2
3
4
5
6
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
1
2
3
4
c.ServerApp.ip = '*'
c.ServerApp.port = 8001
c.ExtensionApp.open_browser = False
c.ServerApp.token = 'limour'

添加内核

1
2
3
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


【记录】win10 通过 docker 调用 nvidia
https://hexo.limour.top/-ji-lu-win10-tong-guo-docker-diao-yong-nvidia
Author
Limour
Posted on
July 23, 2023
Updated on
March 19, 2024
Licensed under