Docker学习|通过天池赛事入门

简介: Docker学习|通过天池赛事入门

简说Python,号主老表,Python终身学习者,数据分析爱好者,从18年开始分享Python知识,原创文章227篇,写过Python、SQL、Excel入门文章,也写过Web开发、数据分析文章,老表还总结整理了一份2022Python学习资料和电子书资源,关注后私信回复:2022 即可领取。

大家好,我是老表~Docker学习系列,学习了记得点赞、留言、转发,三连哦~


image.png

本次入门学习我们通过阿里云天池Docker练习场赛事展开,边学边实战,赛事链接如下:


https://tianchi.aliyun.com/s/60dc673cca889619ad9e6c8e9d7725db


1、下载安装Docker

初学者建议在本地系统安装Windows/Mac OS桌面版,比较简单。


Mac:https://docs.docker.com/docker-for-mac/install/Windows:https://docs.docker.com/docker-for-windows/install/

下载成功后,直接点击安装包进行安装即可,基本就是一Next,然后OK,如果是Windows可以注意下安装路径,还挺大的,不怎么建议安装到C盘,大学修电脑遇到最多的问题就是C盘满了,电脑卡死。。。

image.png

2、基本使用

通过上面的基本步骤就算安装成功了,感觉比Java、Python这些编程语言安装环境舒服多了。

安装成功后点击运行Docker桌面版软件,然后打开终端,利用简单的命令行查看Docker是否真的安装成功:


docker --version

Docker version 19.03.12, build 48a66213fe


成功显示Docker版本就表示安装成功啦,是的后面如果执行相关命令一般使用docker作为关键字。

  • 查看容器
# 查看正在运行的容器
docker ps
# 查看全部的容器,包括已经停止运行的
docker ps -a
  • 暂停正在运行的容器
docker stop 容器id
  • 删除容器
docker rm 容器id
  • 查看镜像
# 查看正在运行的镜像
docker iamges
# 查看全部的镜像,包括已经停止运行的
docker images -a
  • 删除镜像
# 通过镜像id删除
docker rmi IMAGE ID(镜像id)
# 通过仓库名和标签删除
docker rmi REPOSITORY:TAG(仓库:标签)
# 删除异常停止的docker容器
docker rm `docker ps -a | grep Exited | awk '{print $1}'`   
# 删除名称或标签为none的镜像
docker rmi -f  `docker images | grep '<none>' | awk '{print $3}'`  
'''
注意,删除镜像前需要先暂停所有用了该镜像的容器
'''


3、创建阿里云容器镜像服务(免费)

大家可以跟着天池Docker入门赛事中手把手教程进行创建,过程非常详细,我就不赘述了。

https://tianchi.aliyun.com/competition/entrance/231759/tab/174


image.png
4、测试,输出你的Hello world

通过上面步骤,我们安装好了Docker并且开通了阿里云容器镜像服务、创建了一个私有仓库。这个时候我们可以在Mac终端进行命令行操作,先拉取Python基础镜像:

tianchi@xxxx ~ % docker pull registry.cn-shanghai.aliyuncs.com/tcc-public/python:3
3: Pulling from tcc-public/python
c5e155d5a1d1: Pull complete
221d80d00ae9: Pull complete
4250b3117dca: Pull complete
3b7ca19181b2: Pull complete
425d7b2a5bcc: Pull complete
dc3049ff3f44: Pull complete
472a6afc6332: Pull complete
5f79c90f8d7c: Pull complete
1051ee813012: Pull complete
Digest: sha256:6268ecdce5f04d54bd411cba64e49c714589e53ae482a49c6c12eaf91a5d0425
Status: Downloaded newer image for registry.cn-shanghai.aliyuncs.com/tcc-public/python:3
registry.cn-shanghai.aliyuncs.com/tcc-public/python:3

然后创建一个本地文件目录(名称自己取,比如tianchi_demo),用于存放这次任务镜像所需的文件,包含四个文件,结构如下:

tianchi_demo
├── Dockerfile
├── hello_world.py
├── result.json
└── run.sh
  • Dockerfile 文件内容如下
# Base Images
## 从天池基础镜像构建
FROM registry.cn-shanghai.aliyuncs.com/tcc-public/python:3
## 把当前文件夹里的文件构建到镜像的根目录下
ADD . /
## 指定默认工作目录为根目录(需要把run.sh和生成的结果文件都放在该文件夹下,提交后才能运行)
WORKDIR /
## 镜像启动后统一执行 sh run.sh
CMD ["sh", "run.sh"]
  • hello_world.py 文件内容如下
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
print("hello world, 简说Python")
  • result.json 暂时为空,用于存储赛事结果数据。
  • run.sh 文件内容如下
python hello_world.py
再然后,在终端进入tianchi_demo目录,输入下面命令,创建一个镜像:
tianchi@xxx tianchi_demo % docker build -t registry.你的仓库公网地址:1.0 .
Sending build context to Docker daemon  14.85kB
Step 1/4 : FROM registry.cn-shanghai.aliyuncs.com/tcc-public/python:3
 ---> a4cc999cf2aa
Step 2/4 : ADD . /
 ---> 876b4cc17122
Step 3/4 : WORKDIR /
 ---> Running in b9ae6055fdc7
Removing intermediate container b9ae6055fdc7
 ---> 7dca7fe8bdd7
Step 4/4 : CMD ["sh", "run.sh"]
 ---> Running in 7d3eb51f6767
Removing intermediate container 7d3eb51f6767
 ---> 16de739132c7
Successfully built 16de739132c7
Successfully tagged registry.你的仓库公网地址:1.0

注意,命令格式,首先是docker build -t表示构建中为一个镜像设置多个标签;registry.你的仓库公网地址是在哪里构建这个镜像;:1.0表示为自己当前创建的镜像指定版本号,用于区分每次build的镜像;.表示指定镜像构建过程中的上下文环境的目录,不可缺少.

最后,按默认设置运行镜像,命令如下:

tianchi@xxx tianchi_demo % docker run 16de739132c7 sh run.sh


hello world, 简说Python

上面操作没有错误的话,就会打印出你指定输入的内容。注意,命令格式,docker run表示运行指定镜像;16de739132c7是镜像id,在之前创建镜像的时候有出现过:Successfully built 16de739132c7sh run.sh指运行该镜像下的run.sh文件,即运行python hello_world.py

5、推送你的仓库内容到镜像云端 首先需要登录阿里云容器镜像

tianchi@xxx tianchi_demo % docker login --username=你的登录账号 registry.cn-shanghai.aliyuncs.com
# 需要输入登录密码,自己之前设置的
Password: 
Login Succeeded

然后直接输入docker push命令即可:

tianchi@xxx tianchi_demo % docker push registry.你的仓库公网地址:1.0
The push refers to repository [registry.你的仓库公网地址]
14e2a2d93485: Pushed 
2633623f6cf4: Mounted from tcc-public/python 
5194c23c2bc2: Mounted from tcc-public/python 
69bbfe9f27d4: Mounted from tcc-public/python 
2492a3be066b: Mounted from tcc-public/python 
910d7fd9e23e: Mounted from tcc-public/python 
4230ff7f2288: Mounted from tcc-public/python 
2c719774c1e1: Mounted from tcc-public/python 
ec62f19bb3aa: Mounted from tcc-public/python 
f94641f1fe1f: Mounted from tcc-public/python 
1.0: digest: sha256:xxxxxxxxxxxxxxxx size: 2426

这样就推送成功了。

6、正式开始实战练习


1)赛事要求

image.png

  • 输出Hello world
  • 计算 /tcdata/num_list.csv中一列数字的总和。
  • 在/tcdata/num_list.csv文件中寻找最大的10个数,从大到小生成一个ListList. 结果形式:
{  
    "Q1":"Hello world", 
    "Q2":sum值, 
    "Q3":[top10_list] 
}

最后输出的结果是json格式的,存储在result.json里。

2)首先在本地把Python代码写好

  • 输出Hello world
# 1、输出Hello world
print('Hello world')
  • 计算 /tcdata/num_list.csv中一列数字的总和
# 假设数据读取后为列表 tc_list
sum(tc_list)
  • 在/tcdata/num_list.csv文件中寻找最大的10个数,从大到小生成一个ListList
# 假设数据读取后为列表 tc_list
tc_list.sort(reverse=True)
tc_list[:10]
  • 读取指定目录下文件数据转化为列表
# 文件路径
filename = "/Users/tianchi/tianchi_demo/tcdata/num_list.csv"
# 读取
with open(filename, 'r') as f:
    tc_list = list(f.read().split("\n"))
    tc_list = [int(i) for i in tc_list if i != '']
  • 合并修改hello_world.py文件
# coding:utf-8
# Author:老表
# 微信公众号:简说Python
import json
# 文件路径
# filename = "/Users/tianchi/tianchi_demo/tcdata/num_list.csv"
filename = "./tcdata/num_list.csv"
# 读取
with open(filename, 'r') as f:
    tc_list = list(f.read().split("\n"))
    tc_list = [int(i) for i in tc_list if i != '']
result = {}  # 是个坑,提交到云端的代码必须先定义字典
# 1、输出Hello world,并存入结果
# print('Hello world')
result['Q1']= "Hello world"
# 2、计算数据和,并存入结果
tc_sum = sum(tc_list)
result['Q2'] = tc_sum
# print(tc_sum)
# 3、找到最大的10个数,从大到小生成一个List,并存入结果
tc_list.sort(reverse=True)
result['Q3'] = tc_list[:10]
# print(tc_list) 
# 4、存储结果到result.json
with open("result.json", 'w') as f:
    f.write(json.dumps(result))


3)再次创建镜像,然后push到云端

tianchi@xxx tianchi_demo % docker build -t registry.你的仓库公网地址:1.1 .
Sending build context to Docker daemon   25.6kB
Step 1/4 : FROM registry.cn-shanghai.aliyuncs.com/tcc-public/python:3
 ---> a4cc999cf2aa
Step 2/4 : ADD . /
 ---> 0d5c1e8c8baf
Step 3/4 : WORKDIR /
 ---> Running in b6ca7d7bc0df
Removing intermediate container b6ca7d7bc0df
 ---> 5023088e827a
Step 4/4 : CMD ["sh", "run.sh"]
 ---> Running in b11967f76e31
Removing intermediate container b11967f76e31
 ---> fa6485ee7dda
Successfully built fa6485ee7dda
Successfully tagged registry.你的仓库公网地址:1.1

创建成功后再进行push

tianchi@xxx tianchi_demo % docker push registry.你的仓库公网地址:1.1   
The push refers to repository [registry.你的仓库公网地址]
a9d89b627701: Pushed 
2633623f6cf4: Layer already exists 
5194c23c2bc2: Layer already exists 
69bbfe9f27d4: Layer already exists 
2492a3be066b: Layer already exists 
910d7fd9e23e: Layer already exists 
4230ff7f2288: Layer already exists 
2c719774c1e1: Layer already exists 
ec62f19bb3aa: Layer already exists 
f94641f1fe1f: Layer already exists 
1.1: digest: sha256:xxxxxxxx size: 2426

7、提交仓库,完成比赛先分享几个大坑:

我一共提交了4次,最后一次满分。先说一下提交过程:进入赛事页面->点击左边的菜单栏的提交结果->点击配置路径->输入镜像路径、用户名、密码->点击确定->点击提交,即可完成提交,然后等待出分即可,一般5分钟左右即可出分。

  • 第一次提交,只写了镜像路径,没有填用户名和密码,结果几个小时都没出结果,坑1。

image.png

解决方法:输入用户名和密码即可,就是你开通阿里云镜像时的账号密码,用户名一般为手机号。

  • 第二次提交,报错说''字符串不能被转换成整形数据
File "hello_world.py", line 14, in <listcomp>
tc_list = [int(i) for i in tc_list]
ValueError: invalid literal for int() with base 10: ''

解决方法:

# 代码修改下,去除''
tc_list = [int(i) for i in tc_list if i != '']
  • 第三次提交,报错说需要先定义result字典
result['Q1']= "Hello world"
NameError: name 'result' is not defined

解决方法:

# 使用前先定义字典
result = {}  # 太坑了
result['Q1']= "Hello world"
  • 第四次提交,完美,100分

image.png


8、其他操作

1)配置加速器,一般国外镜像下载安装包会比较慢,所以我们可以使用国内镜像,我是用阿里云的,点击下方链接登录阿里云即可获取到自己专属的镜像加速地址了

https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors


image.png

也还有其他加速器可以用,比如:

网易加速器:https://hub-mirror.c.163.com/

阿里云加速器:https://mirror.baidubce.com

七牛云加速器:https://reg-mirror.qiniu.com

由于单个加速器可能失效,所以我们可以多加几个,点击Docker应用图标-> Perferences...-> Docker Engine,配置加速器需要输入JSON格式代码,大家可以直接复制下面语句粘贴过去,注意格式:

{
  "registry-mirrors": [
    "https://<用自己的替换下>.mirror.aliyuncs.com",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}


image.png

然后点击Apply & Restart,即可。


2)如果你在 docker build 时遇到这个问题,可以参考这个解决方法

tianchi tianchi_demo % docker build -t registry.你的仓库地址:1.0.


错误:

"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile

原因是最后一个 . 表示的是构建镜像的路径,不可以省掉,但是加入的时候需要和前面的仓库路径加一个空格,如下即可运行成功:tianchitianchi_demo % dockerbuild-tregistry.你的仓库地址:1.0 .

相关文章
|
5天前
|
存储 Ubuntu Linux
如何安装和使用 Docker:入门指南
如何安装和使用 Docker:入门指南
28 1
|
8天前
|
Ubuntu Linux Docker
弃用Docker Desktop:在WSL2中玩转Docker之Docker Engine 部署与WSL入门
弃用Docker Desktop:在WSL2中玩转Docker之Docker Engine 部署与WSL入门
48 4
|
8天前
|
Docker 容器
Docker 入门常用命令
Docker 入门常用命令
14 1
|
11天前
|
存储 Docker 容器
Docker精华篇(一)-常用命令大全,入门到精通
Docker精华篇(一)-常用命令大全,入门到精通
|
2月前
|
NoSQL Redis Docker
Docker再学习 - 实战
Docker再学习 - 实战
25 1
|
3月前
|
应用服务中间件 持续交付 nginx
【Docker专栏】Docker入门指南:快速构建你的第一个容器
【5月更文挑战第7天】Docker 入门指南:容器化应用利器。了解 Docker 核心概念——镜像、容器和仓库。安装 Docker 后,运行官方 `hello-world` 验证安装,再尝试运行 `nginx` Web 服务器。通过端口映射访问容器内服务,学习管理容器命令。创建自定义镜像,编写 Dockerfile,实现 Python Web 应用容器化。Docker 助力高效开发与运维,探索更多自动化部署与微服务场景。
92 14
【Docker专栏】Docker入门指南:快速构建你的第一个容器
|
2月前
|
关系型数据库 MySQL 数据库
轻松入门:使用Docker安装MySQL数据库的完全指南
轻松入门:使用Docker安装MySQL数据库的完全指南
|
2月前
|
Java C++ Docker
Docker再学习 - 阿里加速配置篇
Docker再学习 - 阿里加速配置篇
151 0
|
3月前
|
Shell iOS开发 Docker
|
3月前
|
Ubuntu 应用服务中间件 Shell
Docker入门
Docker入门
51 0