docker安装与gpu支持

文章目录

  • 1.基础篇
  • 2.ubuntu 安装docker-GPU
    • 2-1 安装docker-ce
    • 2-2 安装NVIDIA-docker

1.基础篇

推荐文章
https://blog.csdn.net/q610376681/article/details/90483576

2.ubuntu 安装docker-GPU

https://blog.csdn.net/BigData_Mining/article/details/99681168?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

docker-CE 社区版 需要先安装

Docker Compose是 docker 提供的一个命令行工具,用来定义和运行由多个容器组成的应用。使用 compose,我们可以通过 YAML 文件声明式的定义应用程序的各个服务,并由单个命令完成应用的创建和启动。
(先跳过)

Docker Machine可以用来批量安装docker、配置管理docker远程主机,而主机可以是物理机、虚拟机,甚至是云主机。Docker Machine像docker一样也提供了丰富的命令,后面我们可以学习到。下图对docker machine的功能做了很好的描绘。
(先跳过)

2-1 安装docker-ce

1)移除旧的版本
$ sudo apt-get remove docker docker-engine docker-ce docker.io

2) 
$ sudo apt update(如果安装docker时候报错找不到docker,那这里使用sudo apt-get update)
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common

3) 
在/etc/apt/source.list或者/etc/apt/sources.list.d/docker.list文件中添加下面内容
deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable

$ sudo apt update

4) 
添加秘钥,可以添加官方的和阿里的,国内当然添加阿里的啦
$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
验证
$ sudo apt-key fingerprint 0EBFCD88

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ 未知 ] Docker Release (CE deb) 
sub   rsa4096 2017-02-22 [S]

5)安装最新docker-ce
$ sudo apt-get install -y docker-ce
验证
$ docker -v
Docker version 19.03.8, build afacb8b7f0

6)查看服务是否启动
$ systemctl status docker

$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
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.

ps:这里介绍了发生了什么事情
1.当使用run命令运行一个名叫"hello world"的本地镜像时,发现没有这个本地镜像
2.docker client 连接到本地docker daemon
3.docker daemon 从远程仓库下载了"hello world"镜像
4.docker daemon 为hello world镜像创建容器
5.docker daemon输出给docker client,最终输出到终端
也就是说,现在有了hello world的本地镜像,可以直接run创建容器

再次运行hello world镜像
$ sudo docker run hello-world

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

查看本地镜像
$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        14 months ago       1.84kB

2-2 安装NVIDIA-docker

nvidia-docker 是英伟达公司专门为docker方便使用GPU设备,而开发的一种插件,细心的朋友,打开nvidia-docker文件即可看到它实际上就是docker的一层薄封装

前提:拥有nvidia gpu
   安装好Nvidia驱动,CUDA和CUDNN

1)设置源并更新apt-get

$curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
$curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu18.04/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
$sudo apt-get update

2)安装nvidia-docker2软件包并重新加载docker守护程序配置

$sudo apt-get install nvidia-docker2
$sudo pkill -SIGHUP dockerd

运行实例

#最新版nvidia/cuda
$docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
#由于我提前装好了nvidia驱动,这里指定版本
$sudo nvidia-docker run --rm nvidia/cuda:10.1-devel nvidia-smi

Unable to find image 'nvidia/cuda:10.1-devel' locally
10.1-devel: Pulling from nvidia/cuda
7ddbc47eeb70: Pull complete 
c1bbdc448b72: Pull complete 
8c3b70e39044: Pull complete 
45d437916d57: Pull complete 
d8f1569ddae6: Pull complete 
85386706b020: Pull complete 
ee9b457b77d0: Pull complete 
be4f3343ecd3: Pull complete 
30b4effda4fd: Pull complete 
Digest: sha256:31e2a1ca7b0e1f678fb1dd0c985b4223273f7c0f3dbde60053b371e2a1aee2cd
Status: Downloaded newer image for nvidia/cuda:10.1-devel
Wed Mar 18 03:54:12 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.26       Driver Version: 430.26       CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 166...  Off  | 00000000:01:00.0 Off |                  N/A |
|  0%   44C    P8     2W / 130W |    375MiB /  5944MiB |     29%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
+-----------------------------------------------------------------------------+

查看本地镜像
$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nvidia/cuda         10.1-devel          9e47e9dfcb9a        3 months ago        2.83GB
hello-world         latest              fce289e99eb9        14 months ago       1.84kB

ps:可以看到nvidia/cuda这个镜像已经可以调用gpu了.接下来基于该镜像进行开发就好了

你可能感兴趣的:(linux,docker)