[筆記] Ubuntu 18.04安裝Docker CE及NVIDIA Container Toolkit流程


之前寫的[筆記] Ubuntu 18.04安裝Docker CE及nvidia-docker2流程已經out了,以這篇為准。

Docker的好處之一,就是在Container里面可以隨意瞎搞,不用擔心弄崩Host的環境。
為了在docker中支持GPU,NVidia之前是弄了個nvidia-docker2,現在升級為NVIDIA Container Toolkit了。官方說法是"Usage of nvidia-docker2 packages are deprecated since NVIDIA GPUs are now natively supported as devices in the Docker runtime"。

NVIDIA Container Runtime for Docker

安裝環境

  • OS:Ubuntu 18.04 64 bit
  • 顯卡:NVidia GTX 2080 Ti x 2
  • CUDA:10.0
  • cnDNN:7.4

任務:安裝Docker CENVIDIA Container Toolkit,以便后期開展深度學習

配置Docker源

# 更新源
$ sudo apt update

# 啟用HTTPS
$ sudo apt install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

# 添加GPG key
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 添加穩定版的源
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

安裝Docker CE

此刻Docker版本需要19.03,此后可能需要更新。

# 更新源
$ sudo apt update

# 安裝Docker CE
$ sudo apt install -y docker-ce

如果這種方式安裝失敗,也有解決方案。
報錯時屏幕上會顯示下載失敗的deb文件,想辦法下載下來,然后挨個手動安裝就好。

此刻我需要下載的是下面三個文件,此后更新為當時最新版本即可:

手動依次安裝:

$ sudo dpkg -i containerd.io_1.2.6-3_amd64.deb
$ sudo dpkg -i docker-ce-cli_19.03.0~3-0~ubuntu-bionic_amd64.deb
$ sudo dpkg -i docker-ce_19.03.0~3-0~ubuntu-bionic_amd64.deb

驗證Docker CE

如果出現下面的內容,說明安裝成功。

$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

配置nvidia-docker

# 添加源
$ 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.list

# 安裝並重啟docker
$ sudo apt update && sudo apt install -y nvidia-container-toolkit
$ sudo systemctl restart docker

使用

# 在官方CUDA鏡像上測試 nvidia-smi
$ sudo docker run --gpus all nvidia/cuda:9.0-base nvidia-smi

# 啟動支持雙GPU的容器
$ sudo docker run --gpus 2 nvidia/cuda:9.0-base nvidia-smi

# 指定GPU 1,運行容器
$ sudo docker run --gpus device=0 nvidia/cuda:9.0-base nvidia-smi

能看到顯卡信息就說明OK了,當前image是基於Ubuntu 16.04的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM