一次配置Docker环境的完整记录
- Docker环境搭建
- 报错与解决
- 报错一
- 报错二
- 报错三
Docker环境搭建
本节介绍了一次配置docker环境的完整记录:
-
编写Dockerfile文件:
FROM pytorch/pytorch:1.10.0-cuda11.3-cudnn8-devel RUN rm /etc/apt/sources.list.d/cuda.list RUN rm /etc/apt/sources.list.d/nvidia-ml.list RUN apt-key del 7fa2af80 RUN sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list RUN apt update && apt install -y build-essential vim git wget RUN wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-keyring_1.0-1_all.deb RUN dpkg -i cuda-keyring_1.0-1_all.deb copy ./requirements.txt /root/
-
构建镜像:
# IMAGE_NAME为镜像名称,TAG为标签,缺省默认为latest,网络network使用host宿主的 docker build -t IMAGE_NAME:TAG . --network=host # eg:docker build -t demo:v1.0 . --network=host
-
启动容器,并进入容器配置环境:
docker run -it --network=host --gpus all demo:v1.0 /bin/bash # 第一次启动运行run命令,若容器exit停止后可以输入一下命令重新进入: # docker ps -a # docker start -ia CONTAINER_ID
配置pip国内镜像源:
# 没有自行创建 vim ~/.config/pip/pip.conf
使用阿里镜像源:
[global] index-url = https://mirrors.aliyun.com/pypi/simple [install] trusted-host = mirrors.aliyun.com
环境配置可根据自身情况进行配置,在此省略。
-
环境配置好后将容器导出为镜像:
# CONTAINER_ID为容器ID;IMAGE_NAME为镜像名称;TAG为标签,缺省默认为latest docker commit CONTAINER_ID IMAGE_NAME:TAG
-
将docker镜像推送至仓库:
# 仓库地址登录,输入你的用户名和密码,登录到Docker Hub。 docker login # 推送镜像(如果你是首次推送或你的仓库设定为私有的,你可能需要先在Docker Hub上创建仓库) docker push IMAGE_NAME:TAG
报错与解决
在配置环境时,需要编译cuda代码,遇到了一系列报错,本节将介绍相关报错与解决方法。
报错一
fatal error: cuda_runtime_api.h: No such file or directory
解决办法是修改bashrc文件,指定环境变量:
vim ~/.bashrc
# CUDA
export CUDA=11.3
export PATH=/usr/local/cuda-$CUDA/bin${PATH:+:${PATH}}
export CUDA_PATH=/usr/local/cuda-$CUDA
export CUDA_HOME=/usr/local/cuda-$CUDA
export LIBRARY_PATH=$CUDA_HOME/lib64:$LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/cuda-$CUDA/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH
export NVCC=/usr/local/cuda-$CUDA/bin/nvcc
export CFLAGS="-I$CUDA_HOME/include $CFLAGS"
修改完保存后,更新一下环境变量:
source ~/.bashrc
报错二
File "/opt/conda/lib/python3.7/site-packages/torch/utils/cpp_extension.py", line 1606, in _get_cuda_arch_flags
arch_list[-1] += '+PTX'
IndexError: list index out of range
问题的根源在于当没有检测到CUDA硬件时,torch.cuda.device_count()返回0,导致不会向arch_list中添加任何架构,arch_list[-1] += '+PTX’也就无法索引。即arch_list=[],导致无法索引。解决办法是添加环境变量,根据你的GPU compatibility修改具体的值:
CUDA_VERSION=$(/usr/local/cuda/bin/nvcc --version | sed -n 's/^.*release \([0-9]\+\.[0-9]\+\).*$/\1/p')
if [[ ${CUDA_VERSION} == 9.0* ]]; then
export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;7.0+PTX"
elif [[ ${CUDA_VERSION} == 9.2* ]]; then
export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0+PTX"
elif [[ ${CUDA_VERSION} == 10.* ]]; then
export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5+PTX"
elif [[ ${CUDA_VERSION} == 11.0* ]]; then
export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0+PTX"
elif [[ ${CUDA_VERSION} == 11.* ]]; then
export TORCH_CUDA_ARCH_LIST="3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX"
else
echo "unsupported cuda version."
exit 1
fi
报错三
运行算法显示缺少相关依赖包:
# ImportError: libGL.so.1: cannot open shared object file: No such file or directory
apt-get install libgl1
# ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
apt-get install -y libglib2.0-0 libsm6 libxrender1 libxext6
# ImportError:XXX.cpython-37m-x86_64-linux-gnu.so: undefined symbol
# 问题是编译cuda源码有问题,解决办法为启动容器时使用--gpus all,再进行源码的编译。