docker安装pytorch环境

1.安装docker

# 移除现有的Docker软件包及其依赖项:
sudo apt-get remove docker docker-engine docker.io containerd runc

# 安装依赖项并添加Docker官方GPG密钥:
sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# 添加Docker官方软件包仓库:
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 更新软件包列表并安装Docker:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 重启docker
sudo systemctl restart docker

# 安装nvidia tookit
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
      && 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

sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit

2. 把docker挂在到磁盘空间较大的目录

/data/docker/

# 停止Docker运行
sudo service docker stop
# 挂载到有磁盘空间较大的文件夹下面
sudo dockerd --data-root /data/docker

3. 安装pytorch_的docker

https://catalog.ngc.nvidia.com/orgs/nvidia/containers/pytorch/tags

docker run --gpus all -it --name env_pyt_1.12 -v $(pwd):/app nvcr.io/nvidia/pytorch:22.03-py3 

4. 检查容器内是否有docker

# 容器内部检查pytorch可用性
$ python
>>> import torch
>>> torch.__version__
>>> print(torch.cuda.is_available())
True

5. 打开container新建一个python文件

import torch
import torchvision

print("PyTorch version:", torch.__version__)
print("Torchvision version:", torchvision.__version__)

if torch.cuda.is_available():
    device = torch.device("cuda")
    print("CUDA is available on", device)
else:
    print("CUDA is not available")
PyTorch version: 1.12.0a0+2c916ef
Torchvision version: 0.13.0a0
CUDA is available on cuda

成功!

你可能感兴趣的:(小工具,docker,pytorch,容器)