Using GPU from a docker container?

后端 未结 10 1704
名媛妹妹
名媛妹妹 2020-11-27 09:12

I\'m searching for a way to use the GPU from inside a docker container.

The container will execute arbitrary code so i don\'t want to use the privileged mode.

<
10条回答
  •  独厮守ぢ
    2020-11-27 09:17

    Goal:

    My goal was to make a CUDA enabled docker image without using nvidia/cuda as base image. Because I have some custom jupyter image, and I want to base from that.

    Prerequisite:

    The host machine had nvidia driver, CUDA toolkit, and nvidia-container-toolkit already installed. Please refer to the official docs, and to Rohit's answer.

    Test that nvidia driver and CUDA toolkit is installed correctly with: nvidia-smi on the host machine, which should display correct "Driver Version" and "CUDA Version" and shows GPUs info.

    Test that nvidia-container-toolkit is installed correctly with: docker run --rm --gpus all nvidia/cuda:latest nvidia-smi

    Dockerfile

    I found what I assume to be the official Dockerfile for nvidia/cuda here I "flattened" it, appended the contents to my Dockerfile and tested it to be working nicely:

    FROM sidazhou/scipy-notebook:latest
    # FROM ubuntu:18.04 
    
    ###########################################################################
    # See https://gitlab.com/nvidia/container-images/cuda/-/blob/master/dist/10.1/ubuntu18.04-x86_64/base/Dockerfile
    # See https://sarus.readthedocs.io/en/stable/user/custom-cuda-images.html
    ###########################################################################
    USER root
    
    ###########################################################################
    # base
    RUN apt-get update && apt-get install -y --no-install-recommends \
        gnupg2 curl ca-certificates && \
        curl -fsSL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub | apt-key add - && \
        echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list && \
        echo "deb https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/nvidia-ml.list && \
        apt-get purge --autoremove -y curl \
        && rm -rf /var/lib/apt/lists/*
    
    ENV CUDA_VERSION 10.1.243
    ENV CUDA_PKG_VERSION 10-1=$CUDA_VERSION-1
    
    # For libraries in the cuda-compat-* package: https://docs.nvidia.com/cuda/eula/index.html#attachment-a
    RUN apt-get update && apt-get install -y --no-install-recommends \
        cuda-cudart-$CUDA_PKG_VERSION \
        cuda-compat-10-1 \
        && ln -s cuda-10.1 /usr/local/cuda && \
        rm -rf /var/lib/apt/lists/*
    
    # Required for nvidia-docker v1
    RUN echo "/usr/local/nvidia/lib" >> /etc/ld.so.conf.d/nvidia.conf && \
        echo "/usr/local/nvidia/lib64" >> /etc/ld.so.conf.d/nvidia.conf
    
    ENV PATH /usr/local/nvidia/bin:/usr/local/cuda/bin:${PATH}
    ENV LD_LIBRARY_PATH /usr/local/nvidia/lib:/usr/local/nvidia/lib64
    
    
    ###########################################################################
    #runtime next
    ENV NCCL_VERSION 2.7.8
    
    RUN apt-get update && apt-get install -y --no-install-recommends \
        cuda-libraries-$CUDA_PKG_VERSION \
        cuda-npp-$CUDA_PKG_VERSION \
        cuda-nvtx-$CUDA_PKG_VERSION \
        libcublas10=10.2.1.243-1 \
        libnccl2=$NCCL_VERSION-1+cuda10.1 \
        && apt-mark hold libnccl2 \
        && rm -rf /var/lib/apt/lists/*
    
    # apt from auto upgrading the cublas package. See https://gitlab.com/nvidia/container-images/cuda/-/issues/88
    RUN apt-mark hold libcublas10
    
    
    ###########################################################################
    #cudnn7 (not cudnn8) next
    
    ENV CUDNN_VERSION 7.6.5.32
    
    RUN apt-get update && apt-get install -y --no-install-recommends \
        libcudnn7=$CUDNN_VERSION-1+cuda10.1 \
        && apt-mark hold libcudnn7 && \
        rm -rf /var/lib/apt/lists/*
    
    
    ENV NVIDIA_VISIBLE_DEVICES all
    ENV NVIDIA_DRIVER_CAPABILITIES all
    ENV NVIDIA_REQUIRE_CUDA "cuda>=10.1"
    
    
    ###########################################################################
    #docker build -t sidazhou/scipy-notebook-gpu:latest .
    
    #docker run -itd -gpus all\
    #  -p 8888:8888 \
    #  -p 6006:6006 \
    #  --user root \
    #  -e NB_UID=$(id -u) \
    #  -e NB_GID=$(id -g) \
    #  -e GRANT_SUDO=yes \
    #  -v ~/workspace:/home/jovyan/work \
    #  --name sidazhou-jupyter-gpu \
    #  sidazhou/scipy-notebook-gpu:latest
    
    #docker exec sidazhou-jupyter-gpu python -c "import tensorflow as tf; print(tf.config.experimental.list_physical_devices('GPU'))"
    

提交回复
热议问题