前言:Dockerfile可以快速部署一个特定环境的镜像,用来部署深度学习环境,解决模型因为环境/版本不同而不能运行的问题。这篇博客主要解决ubuntu下通过Dockerfile创建镜像。
1.Docker安装
这里可以参看我之前的博客ubuntu20.04下 nvidia-docker的安装并载入镜像
刚刚完成安装。
2.准备工作
需要明确运行代码所需的python版本要求,依赖包的版本要求,特别是torch和tensorflow的版本。
- python版本和容器中ubuntu版本相关,一般来说,ubuntu18.04对应默认python版本是3.6,ubuntu19.04对应python3.7,ubuntu20.04对应python3.8。
这里我们建议安装所需python版本对应的ubuntu版本,否则创建容器后再编译python还是相当麻烦的。 - 另外就是torch的版本和tensorflow的版本,torch和tensorflow的版本和cuda的版本相关,cuda的版本和ubuntu的版本相关。这些信息在torch和tensorflow的官网都可以看到。
- 我们需要提前下载好cuda对应的cudnn文件。
我们尝试使用以下参数构建一个容器:
ubuntu==18.04
cuda==10.0
cudnn==7.6.5
python==3.6
tensorflow-gpu==1.13.1
3.Dockerfile
创建一个包含以下三个内容的新文件夹:
- cudnn的压缩包
- requirements.txt
写入需要安装的python包以及对应的版本 - Dockerfile(内容如下)
ARG ubuntu_version=18.04
ARG cuda_version=10.0
FROM nvidia/cuda:${cuda_version}-devel-ubuntu${ubuntu_version}
ARG cudnn_version=7.6.5
ARG cudnn_ln_version=7
# 这里会有一些环境配置的过程
# cuda
ENV PATH=/usr/local/cuda/bin:$PATH
ENV LD_LIBRARY_PATH=/usr/local/cuda/lib64
# cudnn
COPY ./cudnn-10.0-linux-x64-v7.6.5.32.tgz /usr/local/
RUN \
cd /usr/local/ && \
tar -zxvf cudnn-10.0-linux-x64-v7.6.5.32.tgz && \
chmod +x /usr/local/cuda/lib64/libcudnn* && \
chmod +x /usr/local/cuda/include/cudnn* && \
cd /usr/local/cuda/lib64/ && \
ln -snf libcudnn.so.${cudnn_version} libcudnn.so.${cudnn_ln_version} && \
ln -snf libcudnn_adv_train.so.${cudnn_version} libcudnn_adv_train.so.${cudnn_ln_version} && \
ln -snf libcudnn_cnn_train.so.${cudnn_version} libcudnn_cnn_train.so.${cudnn_ln_version} && \
ln -snf libcudnn_ops_train.so.${cudnn_version} libcudnn_ops_train.so.${cudnn_ln_version} && \
ln -snf libcudnn_adv_infer.so.${cudnn_version} libcudnn_adv_infer.so.${cudnn_ln_version} && \
ln -snf libcudnn_cnn_infer.so.${cudnn_version} libcudnn_cnn_infer.so.${cudnn_ln_version} && \
ln -snf libcudnn_ops_infer.so.${cudnn_version} libcudnn_ops_infer.so.${cudnn_ln_version}
# 添加清华源
RUN \
apt-get update && apt-get install --assume-yes apt-utils && apt-get upgrade -y && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse" > /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list
# 更新源、安装软件
RUN \
apt-get update && apt-get upgrade -y && \
apt-get install gcc automake autoconf libtool make wget openssl libssl-dev software-properties-common vim cmake curl git python3 python3-pip -y && \
python3 -m pip install --upgrade pip && \
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 安装包,后面可以添加其他的安装包
COPY requirements.txt ./
RUN \
cd / && \
pip3 install --no-cache-dir -r requirements.txt
三个文件准备好后,我们进入终端,进入新建的文件夹,通过以下命令创建镜像:
docker build -t 镜像名:版本号 .
运行需要很长时间。创建成功后会显示如下信息:
使用以下命令创建容器:
docker run -idt --name 容器名 --gpus all --shm-size 16G 镜像名:版本号
可以通过以下命令打开并进入容器:
sudo nvidia-docker start 容器名
sudo nvidia-docker attach 容器名
其他说明
Dockerfile中软连接的设置对于cudnn7来说是冗余的,为了保证cudnn8也可以用。
文章出处登录后可见!
已经登录?立即刷新