发布时间:2020-04-15 15:14:21编辑:admin阅读(2560)
Docker引擎API的Python库。它允许您执行docker命令所做的任何操作,但可以在Python应用程序中运行容器、管理容器、管理群集等。
官方文档:
https://docker-py.readthedocs.io/en/stable/
pip3 install docker
import docker client = docker.from_env() r = client.containers.run("alpine", ["echo", "hello", "world"]) print(str(r, encoding='utf-8'))
r = client.containers.run('mysql/mysql-server:5.7',ports={'3306/tcp': 3300}, auto_remove=True, name='shark_mysql')
{'3306/tcp': 3300} 将容器的 TCP 3306 端口映射到宿主机的 3300 端口
name 指定了容器的名称
import docker client = docker.from_env() container = client.containers.run("busybox", detach=True) print(container.id)
import docker client = docker.from_env() for container in client.containers.list(): container.stop()
您还可以对单个容器执行操作。此示例打印给定其ID的容器的日志。您需要在运行代码之前修改代码以更改容器的硬编码ID以打印日志。
import docker client = docker.from_env() container = client.containers.get('f1064a8a4c82') print(container.logs())
import docker client = docker.from_env() for image in client.images.list(): print(image.id)
import docker client = docker.from_env() image = client.images.pull("alpine") print(image.id)
import docker client = docker.from_env() container = client.containers.run("alpine", ["touch", "/helloworld"], detach=True) container.wait() image = container.commit("helloworld")print(image.id)
线上服务器的应用服务都是通过docker跑的。上线时,通过jenkins来发布的。版本号以BUILD_NUMBER为准!比如:
app_user:1 app_user:2 app_user:3 app_user:4 app_user:5 app_user:6 ...
那么问题就来了,服务器本地镜像越来越多,会导致磁盘空间急剧上涨。但是我只需要最近3个版本即可,用了做回滚操作。
之前的镜像可以删除掉。
start.py
#!/usr/bin/env python3 # coding: utf-8 # 删除本地镜像,保留最近3个版本 import time import docker # 项目列表 project_list = ['app_user'] def write_log(content, colour='white', skip=False): """ 写入日志文件 :param content: 写入内容 :param colour: 颜色 :param skip: 是否跳过打印时间 :return: """ # 颜色代码 colour_dict = { 'red': 31, # 红色 'green': 32, # 绿色 'yellow': 33, # 黄色 'blue': 34, # 蓝色 'purple_red': 35, # 紫红色 'bluish_blue': 36, # 浅蓝色 'white': 37, # 白色 } choice = colour_dict.get(colour) # 选择颜色 path = "output.log" # 日志文件 with open(path, mode='a+', encoding='utf-8') as f: if skip is False: # 不跳过打印时间时 content = time.strftime('%Y-%m-%d %H:%M:%S') + ' ' + content info = "\033[1;{};1m{}\033[0m".format(choice, content) print(info) f.write(content + "\n") client = docker.from_env() # 镜像列表 images_obj = client.images.list() # 项目字典 project_dict = {} # project_dict ={ # 'app_user':['app_user:1', 'app_user:2'], # } for i in images_obj: # print(i,type(i),i.attrs.get('RepoTags'),type(i.attrs.get('RepoTags'))) for project in project_list: # print("RepoTags",i.attrs.get('RepoTags')) if project not in project_dict: project_dict[project] = [] for image in i.attrs.get('RepoTags'): if project in image: project_dict[project].append(image) # print("project_dict",project_dict) for i in project_dict: # print("i",len(project_dict[i])) # 判断镜像列表是否小于等于3 if len(project_dict[i]) > 3: # print(project_dict[i]) # 镜像列表排序,默认升序(从小到大) project_dict[i].sort() # print(project_dict[i]) # 删除最后3个元素 for n in range(3): project_dict[i].pop() # print(project_dict[i]) # 将剩下的镜像删除 for image in project_dict[i]: # print("删除",image) try: result = client.images.remove(image) write_log("删除镜像: %s 成功"%image, "green") except Exception as e: print(e) write_log("删除镜像: %s 失败"%image, "red")
注意:修改project_list变量即可。
执行代码
python3 start.py
本文参考链接:
https://www.jianshu.com/p/c1bfc14d5c02
上一篇: Traefik-v2.x快速入门
47745
46237
37110
34627
29229
25886
24745
19863
19417
17909
5716°
6315°
5836°
5888°
6985°
5829°
5846°
6361°
6316°
7673°