天天看点

Docker练习场实践Docker练习场

Docker练习场

官方链接:https://tianchi.aliyun.com/competition/entrance/231759/tab/226

基础环境

阿里云 centos 7.0

1. 环境安装

# dcoker 安装
yum install docker -y
# 查看 docker信息
docker info
# 拉取docker样例并运行
docker pull hello-world
docker run hello-world
           

2. 按照官网创建docker镜像仓库

3. 创建样例代码

Docker练习场实践Docker练习场

创建如下四个文件:

Dockerfile,hello.py,requirements.txt,run.sh

** 其中py文件名可以修改,具体内容如下

Dockerfile:

# Base Images
## 从天池基础镜像构建
FROM registry.cn-shanghai.aliyuncs.com/tcc-public/python:3

## 把当前文件夹里的文件构建到镜像的根目录下(.后面有空格,不能直接跟/)
ADD . /

## 指定默认工作目录为根目录(需要把run.sh和生成的结果文件都放在该文件夹下,提交>后才能运行)
WORKDIR /

## Install Requirements(requirements.txt包含python包的版本)
## 这里使用清华镜像加速安装
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt

## 镜像启动后统一执行 sh run.sh
CMD ["sh", "run.sh"]
           

hello.py:

#!/usr/bin/env python
# coding: utf-8
import pandas as pd
import numpy as np
import json
#num_list.csv文件在天池给的基础镜像中,已在tcdata文件夹中包含
#这里为了方便测试程序,自己编写一个num_list.csv文件,实际提交结果时,用不到此文件
# data=np.random.randint(1,100,200)
data = pd.read_csv('/tcdata/num_list.csv',header = None)

# 第一题
result_1 = "Hello world"
# 第二题
result_2 = 0
for i,num in enumerate(data[0]):
    result_2 += num
# 第三题
data.sort_values(by=0, ascending=False, inplace=True)
result_3 = data[0][:10]
result_3 = list(result_3)
result = {
    "Q1" : result_1,
    "Q2" : result_2,
    "Q3" : result_3
}
with open('result.json', 'w', encoding='utf-8') as f:
    json.dump(result, f)
           

requirements.txt

该文件可以通过pipreqs生成

pandas==2.0.3
numpy==1.18.2
           

run.sh:

#!/bin/sh
CURDIR="`dirname $0`"
echo $CURDIR
cd $CURDIR
python hello.py
           

4. 创建docker镜像并推送

# 需要登陆才能创建与推送
sudo docker login --username=账号 docker镜像仓库公网地址
docker build -t docker镜像仓库:版本号(字母+数字) .
docker push docker镜像仓库:版本号
           

5.提交docker镜像

Docker练习场实践Docker练习场

点击配置路径

Docker练习场实践Docker练习场

镜像路径填写刚刚创建的 docker镜像仓库:版本号

6.等待任务完成

会有邮件通知

Docker练习场实践Docker练习场