前言 到这里我们基本已经将深度学习的相关环境已经搭建起来了,接下来我们就需要使用Docker容器创建符合我们自己深度学习需求的Docker镜像了。
在开始创建DockerFile
文件之前,我会回想为什么要做这件事情,以及最总达成的目标是什么:我们希望有一个容器,能够便于折腾和快速上手,并且内置了主流AI学习框架。
为了能够达成上面的这个目标,在这里我选择使用NVIDIA
官方的CUDA
镜像作为我的基础镜像,并在上面安装Python
以及PyTorch
等软件包。
编写DockerFile
文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04 ARG USER=test ARG PASSWORD=${USER} 123$ ENV LANG C.UTF-8 ENV DEBIAN_FRONTEND noninteractive ENV MPLLOCALFREETYPE 1 RUN apt-get update && apt-get install -y software-properties-common RUN add-apt-repository ppa:deadsnakes/ppa RUN apt-get update && apt-get install -y \ build-essential \ git \ wget \ vim \ curl \ zip \ zlib1g-dev \ unzip \ pkg-config \ libgl-dev \ libblas-dev \ liblapack-dev \ python3-tk \ python3-wheel \ graphviz \ libhdf5-dev \ python3.9 \ python3.9-dev \ python3.9-distutils \ openssh-server \ swig \ apt-transport-https \ lsb-release \ libpng-dev \ ca-certificates &&\ apt-get clean &&\ ln -s /usr/bin/python3.9 /usr/local /bin/python &&\ ln -s /usr/bin/python3.9 /usr/local /bin/python3 &&\ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py &&\ python3 get-pip.py &&\ rm get-pip.py &&\ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* ENV TZ=Asia/Seoul RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN useradd --create-home --shell /bin/bash --groups sudo ${USER} RUN echo ${USER} :${PASSWORD} | chpasswd USER ${USER} ENV HOME /home/${USER} WORKDIR $HOME RUN python3 -m pip --no-cache-dir install \ blackcellmagic\ pytest \ pytest-cov \ numpy \ matplotlib \ scipy \ pandas \ jupyter \ scikit-learn \ scikit-image \ seaborn \ graphviz \ gpustat \ h5py \ gitpython \ ptvsd \ Pillow==6.1.0 \ opencv-python RUN python3 -m pip --no-cache-dir install \ torch==1.13.1 \ torchvision==0.14.1 \ torchaudio==0.13.1 \ 'jupyterlab>=2' RUN python3 -m pip --no-cache-dir install datajoint==0.12.9 ENV LD_LIBRARY_PATH /usr/local /cuda/extras/CUPTI/lib64:${LD_LIBRARY_PATH} USER root RUN mkdir /var/run/sshd EXPOSE 22 CMD ["/usr/sbin/sshd" , "-D" ]
编写Docker-Compose
文件 1 2 3 4 5 6 7 8 9 10 11 12 version: "3.9" services: nvidia: build: . runtime: nvidia environment: - NVIDIA_VISIBLE_DEVICES=all ports: - "22:22" - "80:80" - "8000:8000" tty: true