天天看點

《AI開發者的docker實踐》之實踐篇

1.建立鏡像倉庫賬号

這裡以申請阿裡雲容器鏡像服務(免費),并建立倉庫為例,其他倉庫如dockerhub、谷歌、亞馬遜、騰訊等詳見對應産品說明書。

打開阿裡雲容器服務位址為(

https://cr.console.aliyun.com

注冊開通後産品頁面如下

《AI開發者的docker實踐》之實踐篇

第一步切換标簽頁到命名空間,建立位址唯一的命名空間

《AI開發者的docker實踐》之實踐篇

根據大賽要求選擇對應的地域,其他的按照自己需求選擇或填寫

《AI開發者的docker實踐》之實踐篇

下一步,選擇本地倉庫,不建議其他選項,完成建立。

《AI開發者的docker實踐》之實踐篇

點選管理,可檢視詳情。

《AI開發者的docker實踐》之實踐篇

詳情頁如下,有基本的操作指令,倉庫位址一般使用公網位址即可。

《AI開發者的docker實踐》之實踐篇

按照頁面的指令在本地完成登陸:

export DOCKER_REGISTRY= your_registry_url<docker registry url>
(注意這裡your_registry_url最後字段結尾,不能多不能少E.g registry.cn-shanghai.aliyuncs.com/xxxx/xxxx)
docker login $DOCKER_REGISTRY \
    --username your_username \
    --password your_password      

2.實踐docker 練習賽(天池)

2.1 練習賽連結&資料

gpu版本練習賽連結

【推薦】

cpu版本練習賽連結 從0開始大賽docker送出視訊

示範

2.2 GPU版docker練習賽實踐

這裡以GPU版docker 練習賽中的練習一為例來建構鏡像并送出:

首先我們寫一個main.py,實作讀取/tcdata下的資料,計算a*b 生成result.npy檔案

#main.py
import os
import numpy as np
import torch

device = torch.device("cuda")

data_dir = '/tcdata'
a = np.load(os.path(data_dir,a.npy))
b = np.load(os.path(data_dir,b.npy))

a = torch.from_numpy(a).to(device)
b = torch.from_numpy(b).to(device)
c = torch.matmul(a,b).cpu()

print(c)
np.save("result.npy", c)
      

編寫入口檔案run.sh

#bin/bash
#列印GPU資訊
nvidia-smi
#執行math.py
python3 math.py      

然後編寫Dockerfile 用于打包main.py和運作環境為鏡像

# Base Images
## 從天池基礎鏡像建構(from的base img 根據自己的需要更換,建議使用天池open list鏡像連結:https://tianchi.aliyun.com/forum/postDetail?postId=67720)
FROM registry.cn-shanghai.aliyuncs.com/tcc-public/pytorch:1.1.0-cuda10.0-py3
##安裝python依賴包
RUN pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
## 把目前檔案夾裡的檔案建構到鏡像的根目錄下,并設定為預設工作目錄
ADD . /
WORKDIR /
## 鏡像啟動後統一執行 sh run.sh
CMD ["sh", "run.sh"]      

指令行執行,建構鏡像:

tips: 鏡像命名根據自己申請的倉庫registry來,可以省去tag步驟直接上傳,保持本地鏡像清潔。

$ docker build -t registry.cn-shanghai.aliyuncs.com/xxxx/test:0.1 .      

上傳鏡像倉庫

$ docker push registry.cn-shanghai.aliyuncs.com/xxxx/test:0.1      

天池頁面送出

《AI開發者的docker實踐》之實踐篇

正在運作的狀态running,其他狀态對應展開也可以看到詳情,如錯誤狀态展開可看到大緻reason.

《AI開發者的docker實踐》之實踐篇

運作結束或者運作失敗都會有郵件通知到送出人留在天池的郵箱裡,收到提示即可回到大賽頁面檢視成績及日志

《AI開發者的docker實踐》之實踐篇

3.docker 在本地使用gpu

docker在新的版本中均已支援直接調用gpu,通過--gpu 指定使用哪個gpu, --gpu all 則是使用所有gpu

前提:請確定已按照前文環境篇linux末尾安裝了Nvidia對docker的軟體支援。

docker run --gpu all registry.cn-shanghai.aliyuncs.com/xxxx/test:0.1      

調試:

docker run -it --gpu all registry.cn-shanghai.aliyuncs.com/xxxx/test:0.1 /bin/bash
#  nvidia-smi 
Thu Jan  7 19:04:55 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.87.01    Driver Version: 418.87.01    CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P100-PCIE...  On   | 00000000:00:08.0 Off |                    0 |
| N/A   29C    P0    24W / 250W |      0MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla P100-PCIE...  On   | 00000000:00:09.0 Off |                    0 |
| N/A   31C    P0    26W / 250W |      0MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+