当前位置: 代码迷 >> 综合 >> docker - 安装(ubuntu20.04+cuda11.1)
  详细解决方案

docker - 安装(ubuntu20.04+cuda11.1)

热度:66   发布时间:2023-11-18 05:08:07.0

docker安装(ubuntu20.04)

1.安装docker

# 替换国内源
apt-get install vim  # 安装vim编辑器
vim /etc/apt/sources.list  # 编辑源文件
# 删掉内容,换成以下的国内源#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#添加清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse multiverse
# :wq 保存退出apt-get update  # 更新源
apt-get install curl  # 安装curl
sudo update-ca-certificates 
curl -sSL https://get.daocloud.io/docker | sh  # 国内daocloud一键安装docker
vim  /etc/docker/daemon.json  # 新建或编辑文件,docker镜像加速# 添加以下内容
{
    "registry-mirrors": ["https://hub-mirror.c.163.com","https://ustc-edu-cn.mirror.aliyuncs.com","https://ghcr.io","https://mirror.baidubce.com"]
}
# :wq 保存退出systemctl restart docker.service  # 重启docker服务
docker info  # 查看Registry Mirrors:的信息是否修改成功
docker run hello-world  # 打印信息查看是否安装成功
# ps:有报错缺少命令,安装缺少命令后运行

2.安装显卡驱动以及cuda11.1

# 卸载旧的显卡驱动和cuda
apt-get autoremove --purge nvidia*
apt-get autoremove --purge cuda*
# 查看显卡信息,第一行会有NVIDIA Corporation Device
lspci -k | grep -A 2 -i "VGA"
# 添加nvidia显卡驱动ppa源
add-apt-repository ppa:graphics-drivers/ppa
apt-get update 
# 执行推荐显卡驱动设置
ubuntu-drivers devices
# 下载推荐的版本
apt-get install nvidia-***(recommended版本)
# 等待安装,显卡驱动安装完成
# 百度网盘链接: https://pan.baidu.com/s/14G49Btc13pSlPSUJxtfSdA 提取码: jbsl 
# cuda官网:https://developer.nvidia.com/cuda-toolkit-archive可下载其他版本
# cudnn官网:https://developer.nvidia.com/rdp/cudnn-download可下载其他版本
# 百度网盘下载cuda11.1安装所需文件
sh cuda_11.1.1_455.32.00_linux.run  # 安装cuda
# 安装过程中会首先会遇到提示显卡驱动已经安装,这时候选择continue继续
# 输出accept继续
# 新界面出现需要安装的内容,回车取消显卡驱动安装,选择install完成安装
# 配置环境变量
vim ~/.bashrc  # 打开文件# 在末尾添加以下内容
# CUDA Soft Link
export PATH=/usr/local/cuda-11.1/bin${
    PATH:+:${
    PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
# :wq 添加完成# 测试cuda是否安装成功
nvcc -V  # 输出cuda版本信息,安装成功# 解压cudnn-11.3-linux-x64-v8.2.1.32.tgz,并进入解压后的文件夹
cp cuda/include/cudnn.h /usr/local/cuda/include
cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn* 
# 安装剩余的三个deb包
dpkg -i libcudnn8_8.2.1.32-1+cuda11.3_amd64.deb
dpkg -i libcudnn8-dev_8.2.1.32-1+cuda11.3_amd64.deb
dpkg -i libcudnn8-samples_8.2.1.32-1+cuda11.3_amd64.deb
# 重启,测试是否安装成功
cp -r /usr/src/cudnn_samples_v8/ ~
cd ~/cudnn_samples_v8/mnistCUDNN/
apt-get install libfreeimage3 libfreeimage-dev
make clean && make
./mnistCUDNN 
# 显示Test passed!安装成功

3.nvidia docker安装

# 设置stable存储库和密钥
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \&& curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - \&& curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.listcurl -s -L https://nvidia.github.io/nvidia-container-runtime/experimental/$distribution/nvidia-container-runtime.list | sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.listapt-get update
# 安装nvidia-docker2安装包
apt-get install -y nvidia-docker2
# 重启docker
systemctl restart docker
# 测试安装是否成功
docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi
# 输出显卡信息即安装成功

4.安装PyTorch_gpu+ jupyter

# 拉取一个要使用的镜像
docker pull anibali/pytorch:1.7.0-cuda11.0  # 版本自己确定
# 创建容器,自己选择映射端口,1778可改为其他端口
docker run -it --init   --gpus=all   --ipc=host  --name pytorch -p 1778:8888  --volume="$PWD:/app"   anibali/pytorch python3
# 查看容器ID
docker ps -a
# 开启容器
docder start 容器ID
# 以交互式进入容器
docker exec -it 容器ID /bin/bash
# 安装jupyterlab
conda install -c conda-forge jupyterlab
# 启动jupyterlab后远程浏览器登陆
jupyter lab --ip=0.0.0.0 --no-browser --allow-root
# 在浏览器打开给定的链接,端口8888改为设定的映射端口,即可登录jupyter