1.创建镜像仓库账号
这里以申请阿里云容器镜像服务(免费),并创建仓库为例,其他仓库如dockerhub、谷歌、亚马逊、腾讯等详见对应产品说明书。
打开阿里云容器服务地址为(
https://cr.console.aliyun.com)
注册开通后产品页面如下

第一步切换标签页到命名空间,创建地址唯一的命名空间

根据大赛要求选择对应的地域,其他的按照自己需求选择或填写

下一步,选择本地仓库,不建议其他选项,完成创建。

点击管理,可查看详情。

详情页如下,有基本的操作命令,仓库地址一般使用公网地址即可。

按照页面的指令在本地完成登陆:
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
天池页面提交

正在运行的状态running,其他状态对应展开也可以看到详情,如错误状态展开可看到大致reason.

运行结束或者运行失败都会有邮件通知到提交人留在天池的邮箱里,收到提示即可回到大赛页面查看成绩及日志

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 |
+-----------------------------------------------------------------------------+