├── .gitignore ├── config └── __init__.py ├── venv ├── Scripts │ └── python.exe └── Lib │ └── site-packages │ └── setuptools-28.8.0-py3.6.egg ├── docker ├── __pycache__ │ ├── db.cpython-36.pyc │ └── __init__.cpython-36.pyc ├── db.py └── __init__.py ├── window ├── __pycache__ │ ├── __init__.cpython-36.pyc │ ├── images.cpython-36.pyc │ ├── running.cpython-36.pyc │ └── Tableview.cpython-36.pyc ├── version.py ├── __init__.py ├── Tableview.py ├── images.py ├── running.py ├── demo.py └── create_container.py └── run.py /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | */__pycache__/* 3 | -------------------------------------------------------------------------------- /config/__init__.py: -------------------------------------------------------------------------------- 1 | title = "标题" 2 | 3 | geometry = "700x400" 4 | -------------------------------------------------------------------------------- /venv/Scripts/python.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctfang/docker-tkinter/master/venv/Scripts/python.exe -------------------------------------------------------------------------------- /docker/__pycache__/db.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctfang/docker-tkinter/master/docker/__pycache__/db.cpython-36.pyc -------------------------------------------------------------------------------- /docker/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctfang/docker-tkinter/master/docker/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /window/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctfang/docker-tkinter/master/window/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /window/__pycache__/images.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctfang/docker-tkinter/master/window/__pycache__/images.cpython-36.pyc -------------------------------------------------------------------------------- /window/__pycache__/running.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctfang/docker-tkinter/master/window/__pycache__/running.cpython-36.pyc -------------------------------------------------------------------------------- /window/__pycache__/Tableview.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctfang/docker-tkinter/master/window/__pycache__/Tableview.cpython-36.pyc -------------------------------------------------------------------------------- /venv/Lib/site-packages/setuptools-28.8.0-py3.6.egg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctfang/docker-tkinter/master/venv/Lib/site-packages/setuptools-28.8.0-py3.6.egg -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | import window 2 | 3 | 4 | class run: 5 | def __init__(self): 6 | root = window.Tk() 7 | root.mainloop() 8 | 9 | 10 | if __name__ == '__main__': 11 | run() 12 | -------------------------------------------------------------------------------- /window/version.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | 3 | 4 | class version(tk.LabelFrame): 5 | title = "版本说明" 6 | 7 | version = "版本:RC0.0.1\n作者:明月有色\n版权所有:blog.ctfang.com\n源码地址:github.com/selden1992" 8 | 9 | def __init__(self, master=None, **kw): 10 | super().__init__(master, kw) 11 | 12 | text = tk.Text(self) 13 | text.insert(tk.INSERT,self.version) 14 | text.pack() -------------------------------------------------------------------------------- /docker/db.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | 4 | def create(): 5 | create_tables = "CREATE TABLE docker (task text, finished integer, date text)" 6 | query(create_tables) 7 | 8 | 9 | def query(sql, data=None, receive=False): 10 | conn = sqlite3.connect("docker.db") 11 | cursor = conn.cursor() 12 | if data: 13 | cursor.execute(sql, data) 14 | else: 15 | cursor.execute(sql) 16 | 17 | if receive: 18 | return cursor.fetchall() 19 | else: 20 | conn.commit() 21 | 22 | conn.close() 23 | -------------------------------------------------------------------------------- /window/__init__.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter.ttk import Notebook 3 | from window.running import running 4 | from window.images import images 5 | from window.version import version 6 | 7 | 8 | class Tk(tk.Tk): 9 | w = 700 10 | h = 400 11 | 12 | def __init__(self): 13 | super(Tk, self).__init__() 14 | self.title("docker简易管理") 15 | ws = self.winfo_screenwidth() 16 | hs = self.winfo_screenheight() 17 | self.x = (ws / 2) - (self.w / 2) 18 | self.y = (hs / 2) - (self.h / 2) 19 | self.geometry('%dx%d+%d+%d' % (self.w, self.h, self.x, self.y)) 20 | self.handel() 21 | 22 | def handel(self): 23 | notebook = Notebook(self) 24 | running_tab = running(notebook) 25 | images_tab = images(notebook) 26 | version_tab = version(notebook) 27 | 28 | notebook.add(running_tab, text=running.title) 29 | notebook.add(images_tab, text=images.title) 30 | notebook.add(version_tab, text=version.title) 31 | notebook.pack(fill=tk.BOTH, expand=1) 32 | -------------------------------------------------------------------------------- /window/Tableview.py: -------------------------------------------------------------------------------- 1 | import tkinter 2 | from tkinter import ttk 3 | 4 | 5 | class Tableview: 6 | def __init__(self, master=None, **kw): 7 | self.CheckbuttonValue = {} 8 | self.title_config = {} 9 | self.row = 0 10 | self.master = master 11 | self.titleLabel = ttk.Frame(self.master, padding='0 0 0 0') 12 | self.titleLabel.pack(side=tkinter.TOP, expand=0, fill=tkinter.X) 13 | 14 | def title_check(self, **value): 15 | self.all_check = tkinter.StringVar(value=0) 16 | value['variable'] = self.all_check 17 | value['command'] = self.check_all 18 | ttk.Checkbutton(self.titleLabel, **value).grid(row=0, column=0) 19 | 20 | def title(self, column=1, **value): 21 | ttk.Label(self.titleLabel, **value).grid(row=0, column=column) 22 | self.title_config[column] = value 23 | 24 | def insert(self, *values): 25 | column = 1 26 | for data in values: 27 | temp_config = self.title_config[column] 28 | temp_config['text'] = data 29 | ttk.Label(self.titleLabel, **temp_config).grid(row=self.row, column=column) 30 | column = column + 1 31 | 32 | def set_check(self, id): 33 | self.row = self.row + 1 34 | self.CheckbuttonValue[id] = tkinter.StringVar(value=0) 35 | tempLabel = ttk.Checkbutton(self.titleLabel, variable=self.CheckbuttonValue[id]) 36 | tempLabel.grid(row=self.row, column=0) 37 | 38 | def check_all(self): 39 | if self.all_check.get() == '1': 40 | for data in self.CheckbuttonValue.values(): 41 | data.set(1) 42 | else: 43 | for data in self.CheckbuttonValue.values(): 44 | data.set(0) 45 | 46 | def get_check(self): 47 | return self.CheckbuttonValue -------------------------------------------------------------------------------- /window/images.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | import tkinter.messagebox 3 | import docker 4 | from window.Tableview import Tableview 5 | from window.create_container import create 6 | 7 | 8 | class images(tk.Frame): 9 | title = "镜像管理" 10 | left_width = 150 11 | 12 | def __init__(self, master=None, cnf={}, **kw): 13 | super().__init__(master,cnf, **kw) 14 | left_label = tk.Frame(self, width=self.left_width) 15 | left_label.pack(fill=tk.Y, side=tk.LEFT) 16 | 17 | self.opent_right_label() 18 | 19 | tk.Button(left_label, text="刷新列表", width=20, command=self.resert_images()).pack() 20 | tk.Button(left_label, text="下载新镜像", width=20, command=self.down_docker).pack() 21 | tk.Button(left_label, text="创建容器", width=20, command=self.create_container).pack() 22 | tk.Button(left_label, text="删除", width=20, command=self.del_image).pack(side=tk.BOTTOM) 23 | 24 | # 打开右边列表 25 | def opent_right_label(self): 26 | self.right_label = tk.Frame(self, background='white', width=self.winfo_screenwidth() - self.left_width, padx=0, pady=0) 27 | self.right_label.pack(fill=tk.Y, side=tk.LEFT) 28 | 29 | images = docker.images.list() 30 | 31 | tableVies = Tableview(self.right_label) 32 | tableVies.title_check() 33 | tableVies.title(text='名称', column=1, width='20') 34 | tableVies.title(text='版本', column=2, width='20') 35 | tableVies.title(text='大小', column=3, width='20') 36 | tableVies.title(text='时间', column=4, width='30') 37 | 38 | for data in images: 39 | tableVies.set_check(data['IMAGE_ID']) 40 | tableVies.insert(data['REPOSITORY'], data['TAG'], data['SIZE'], data['CREATED']) 41 | 42 | self.tableVies = tableVies 43 | del tableVies 44 | 45 | # 创建容器 46 | def create_container(self): 47 | CheckbuttonValue = self.tableVies.get_check() 48 | for key in CheckbuttonValue: 49 | if CheckbuttonValue[key].get()=="1": 50 | create(key) 51 | return 52 | 53 | tkinter.messagebox.showinfo(title='提示',message='没有选择镜像') 54 | 55 | # 下载镜像 56 | def down_docker(self): 57 | tkinter.messagebox.showinfo(title='提示', message='还没有开发') 58 | 59 | # 删除镜像 60 | def del_image(self): 61 | is_del = tkinter.messagebox.askokcancel('敏感警告','你将删除镜像,是否执行') 62 | if is_del == False: 63 | return False 64 | iamges = docker.images() 65 | CheckbuttonValue = self.tableVies.get_check() 66 | for key in CheckbuttonValue: 67 | if CheckbuttonValue[key].get()=="1": 68 | iamges.del_image(key) 69 | self.resert_images() 70 | 71 | # 重载当前窗口 72 | def resert_images(self): 73 | self.right_label.destroy() 74 | self.opent_right_label() -------------------------------------------------------------------------------- /window/running.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | import docker 3 | from window.Tableview import Tableview 4 | import tkinter.messagebox 5 | 6 | 7 | class running(tk.LabelFrame): 8 | title = "运行中的容器" 9 | left_width = 150 10 | 11 | def __init__(self, master=None, **kw): 12 | super().__init__(master, kw) 13 | 14 | self.left_label = tk.Frame(self, width=self.left_width) 15 | self.left_label.pack(fill=tk.Y, side=tk.LEFT) 16 | 17 | self.opent_right_label() 18 | 19 | tk.Button(self.left_label, text="刷新列表", width=20, command=self.resert_images).pack() 20 | tk.Button(self.left_label, text="启动", width=20, command=self.start_docker).pack() 21 | tk.Button(self.left_label, text="停止", width=20, command=self.stop_docker).pack() 22 | 23 | tk.Button(self.left_label, text="删除", width=20, command=self.del_docker).pack(side=tk.BOTTOM) 24 | 25 | def stop_docker(self): 26 | CheckbuttonValue = self.tableVies.get_check() 27 | for key in CheckbuttonValue: 28 | if CheckbuttonValue[key].get()=="1": 29 | docker.container.stop(key) 30 | self.resert_images() 31 | 32 | # 重载当前窗口 33 | def resert_images(self): 34 | self.right_label.destroy() 35 | self.opent_right_label() 36 | 37 | # 打开右边列表 38 | def opent_right_label(self): 39 | self.right_label = tk.Frame(self, background='white', width=self.winfo_screenwidth() - self.left_width) 40 | self.right_label.pack(fill=tk.Y, side=tk.LEFT) 41 | 42 | docker_ps = docker.container.all() 43 | 44 | self.tableVies = Tableview(self.right_label) 45 | self.tableVies.title_check() 46 | self.tableVies.title(text='名称', column=1, width='15') 47 | self.tableVies.title(text='状态', column=2, width='8') 48 | self.tableVies.title(text='镜像', column=3, width='20') 49 | self.tableVies.title(text='ip', column=4, width='12') 50 | self.tableVies.title(text='端口', column=5, width='100') 51 | 52 | for data in docker_ps: 53 | if data['STATUS'].find('Up')<0: 54 | status = "off" 55 | else: 56 | status = "up" 57 | ip = docker.container.getIp(data['CONTAINER_ID']) 58 | self.tableVies.set_check(data['NAMES']) 59 | self.tableVies.insert(data['NAMES'], status, data['IMAGE'], ip, data['PORTS']) 60 | 61 | def start_docker(self): 62 | CheckbuttonValue = self.tableVies.get_check() 63 | for key in CheckbuttonValue: 64 | if CheckbuttonValue[key].get()=="1": 65 | docker.container.start(key) 66 | self.resert_images() 67 | 68 | def del_docker(self): 69 | is_del = tkinter.messagebox.askokcancel('敏感警告','你将删除实例,是否执行') 70 | if is_del == False: 71 | return False 72 | CheckbuttonValue = self.tableVies.get_check() 73 | for key in CheckbuttonValue: 74 | if CheckbuttonValue[key].get()=="1": 75 | docker.container.delete(key) 76 | self.resert_images() -------------------------------------------------------------------------------- /window/demo.py: -------------------------------------------------------------------------------- 1 | import os 2 | import config 3 | import tkinter as tk 4 | from tkinter import ttk 5 | from tkinter.ttk import Notebook 6 | import docker.db as db 7 | import docker 8 | 9 | 10 | class Timer(tk.Tk): 11 | def __init__(self): 12 | super().__init__() 13 | 14 | self.title(config.title) 15 | self.geometry(config.geometry) 16 | 17 | self.notebook = Notebook(self) 18 | running_tab = RunTag(self.notebook) 19 | images_tab = ImagesTab(self.notebook) 20 | 21 | self.notebook.add(running_tab, text="运行中的容器") 22 | self.notebook.add(images_tab, text="镜像管理") 23 | self.notebook.pack(fill=tk.BOTH, expand=1) 24 | 25 | 26 | class RunTag(tk.Frame): 27 | def __init__(self, master=None, cnf={}, **kw): 28 | super().__init__() 29 | 30 | rigth_frame = tk.LabelFrame(self) 31 | rigth_frame.pack(side=tk.RIGHT, expand=1, fill=tk.X, anchor=tk.N) 32 | 33 | content = tk.LabelFrame(self, background="cyan") 34 | content.pack(side=tk.RIGHT, expand=0, fill=tk.X) 35 | 36 | docker_ps = docker.container.running() 37 | 38 | tree = ttk.Treeview(content, columns=("NAMES", "IMAGE", "PORTS"), height=18, show="headings") 39 | tree.heading("NAMES", text=docker.container.titleKeyToName("NAMES"), anchor=tk.W) 40 | tree.column("NAMES", anchor=tk.W, width=100) 41 | tree.heading("IMAGE", text=docker.container.titleKeyToName("IMAGE"), anchor=tk.W) 42 | tree.column("IMAGE", anchor=tk.W, width=180) 43 | tree.heading("PORTS", text=docker.container.titleKeyToName("PORTS"), anchor=tk.W) 44 | tree.column("PORTS", anchor=tk.W, width=250) 45 | 46 | for values in docker_ps: 47 | value = (values["NAMES"], values["IMAGE"], values["PORTS"]) 48 | tree.insert("", tk.END, values=value) 49 | tree.pack() 50 | 51 | translate_button = tk.Button(rigth_frame, text="开始") 52 | translate_button.pack(side=tk.TOP, fill=tk.BOTH) 53 | translate_button = tk.Button(rigth_frame, text="停止") 54 | translate_button.pack(side=tk.TOP, fill=tk.X) 55 | translate_button = tk.Button(rigth_frame, text="重启") 56 | translate_button.pack(side=tk.TOP, fill=tk.X) 57 | translate_button = tk.Button(rigth_frame, text="删除") 58 | translate_button.pack(side=tk.BOTTOM, fill=tk.X) 59 | 60 | 61 | class ImagesTab(tk.Frame): 62 | def __init__(self, master=None, cnf={}, **kw): 63 | super().__init__() 64 | self.italian_copy_button = tk.Button(self, text="Copy to Clipboard", command=self.test()) 65 | self.italian_copy_button.pack(side=tk.BOTTOM, fill=tk.X) 66 | 67 | self.italian_translation = tk.StringVar(self) 68 | self.italian_translation.set("") 69 | 70 | self.italian_label = tk.Label(self, textvar=self.italian_translation, bg="lightgrey", fg="black") 71 | self.italian_label.pack(side=tk.TOP, fill=tk.BOTH, expand=1) 72 | 73 | def test(self): 74 | pass 75 | 76 | 77 | if __name__ == "__main__": 78 | timer = Timer() 79 | 80 | if not os.path.isfile("docker.db"): 81 | db.create() 82 | 83 | timer.mainloop() 84 | -------------------------------------------------------------------------------- /window/create_container.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter.filedialog import askdirectory 3 | import docker 4 | 5 | 6 | class create(tk.Tk): 7 | w = 1000 8 | h = 600 9 | submit = {} 10 | port_row = 0 11 | dir_row = 0 12 | 13 | def __init__(self, image): 14 | super(create, self).__init__() 15 | self.image_name = self.get_tag(image) 16 | self.title("创建容器实例 on " + self.image_name) 17 | ws = self.winfo_screenwidth() 18 | hs = self.winfo_screenheight() 19 | self.x = (ws / 2) - (self.w / 2) 20 | self.y = (hs / 2) - (self.h / 2) 21 | self.geometry('%dx%d+%d+%d' % (self.w, self.h, self.x, self.y)) 22 | self.handel() 23 | self.mainloop() 24 | 25 | def handel(self): 26 | tk.Label(self, text='基础镜像').grid(row=0, column=0, ipadx=20, ipady=20) 27 | self.submit["image"] = tk.Entry(self) 28 | self.submit["image"].insert(0, self.image_name) 29 | self.submit["image"].grid(row=0, column=1, sticky=tk.W) 30 | 31 | tk.Label(self, text='容器名称').grid(row=1, column=0, ipadx=20, ipady=20) 32 | self.submit["--name"] = tk.Entry(self) 33 | self.submit["--name"].grid(row=1, column=1, sticky=tk.W) 34 | 35 | # 工作目录 36 | tk.Label(self, text='工作目录').grid(row=2, column=0, ipadx=20, ipady=20) 37 | self.submit["--workdir"] = tk.Entry(self) 38 | self.submit["--workdir"].insert(0, "/") 39 | self.submit["--workdir"].grid(row=2, column=1, sticky=tk.W) 40 | 41 | # 映射端口 42 | self.entry_port = tk.LabelFrame(self, text="映射端口") 43 | self.entry_port.grid(row=3, column=1, sticky=tk.N) 44 | tk.Label(self.entry_port, text="本机", width=10).grid(row=0, column=0, ipadx=0, ipady=0) 45 | tk.Label(self.entry_port, text="容器", width=10).grid(row=0, column=1, ipadx=0, ipady=0) 46 | self.submit["--publish"] = {} 47 | self.add_entry_port() 48 | tk.Button(self, text="新增", command=self.add_entry_port).grid(row=4, column=1, ipadx=20, pady=20) 49 | 50 | # 映射目录 51 | self.entry_dir = tk.LabelFrame(self, text="映射目录") 52 | self.entry_dir.grid(row=3, column=2, sticky=tk.N) 53 | tk.Label(self.entry_dir, text="本机", width=10).grid(row=0, column=0, ipadx=0, ipady=0) 54 | tk.Label(self.entry_dir, text="容器", width=10).grid(row=0, column=1, ipadx=0, ipady=0) 55 | self.submit["--volume"] = {} 56 | self.add_entry_dir() 57 | tk.Button(self, text="新增", command=self.add_entry_dir).grid(row=4, column=2, ipadx=20, pady=20) 58 | 59 | # 提交目录 60 | tk.Button(self, text='提交', command=self.submit_data).grid(row=5, column=1, ipadx=20, pady=20, sticky=tk.W) 61 | 62 | # 新增目录 63 | def add_entry_dir(self): 64 | self.dir_row = self.dir_row + 1 65 | from_key = "source_" + str(self.dir_row) 66 | to_key = "to_" + str(self.dir_row) 67 | temp_entry = tk.Label(self.entry_dir) 68 | temp_entry.grid(row=self.dir_row, column=0) 69 | 70 | self.submit["--volume"][from_key] = tk.Entry(temp_entry) 71 | self.submit["--volume"][to_key] = tk.Entry(self.entry_dir) 72 | self.submit["--volume"][from_key].grid(row=0, column=0) 73 | tk.Button(temp_entry, text="选择目录", command=lambda: self.select_path(from_key)).grid(row=0, column=1) 74 | self.submit["--volume"][to_key].grid(row=self.dir_row, column=1) 75 | 76 | # 动态新增端口 77 | def add_entry_port(self): 78 | self.port_row = self.port_row + 1 79 | from_key = "source_" + str(self.port_row) 80 | to_key = "to_" + str(self.port_row) 81 | self.submit["--publish"][from_key] = tk.Entry(self.entry_port) 82 | self.submit["--publish"][to_key] = tk.Entry(self.entry_port) 83 | self.submit["--publish"][from_key].grid(row=self.port_row, column=0) 84 | self.submit["--publish"][to_key].grid(row=self.port_row, column=1) 85 | 86 | # 获取路径 87 | def select_path(self, from_key): 88 | # 第一步取消置顶 89 | self.wm_attributes('-topmost', 0) 90 | path = askdirectory() 91 | # 选择目录后置顶,显示值 92 | self.wm_attributes('-topmost', 1) 93 | self.submit["--volume"][from_key].insert(1, path) 94 | 95 | # 提交数据 96 | def submit_data(self): 97 | str_command = '' 98 | for key in self.submit: 99 | if isinstance(self.submit[key], dict) == False: 100 | data = self.submit[key].get() 101 | if data!='': 102 | if key=='image': 103 | continue 104 | else: 105 | str_command = str_command + " " + key + " " + self.submit[key].get() 106 | else: 107 | temp = '' 108 | for key_1 in self.submit[key]: 109 | data = self.submit[key][key_1].get() 110 | if data == "": 111 | temp = '' 112 | elif key_1.find('source_') != -1: 113 | temp = " " + key + ' ' + data 114 | else: 115 | temp = temp + ':' + data 116 | if temp != '': 117 | str_command = str_command + " " + temp 118 | 119 | obj_docker = docker.images() 120 | obj_docker.docker_run(str_command, self.submit['image'].get()) 121 | self.destroy() 122 | 123 | # 获取image版本 124 | def get_tag(self, image): 125 | data = docker.images().image_info(image) 126 | return data['RepoTags'][0] 127 | 128 | 129 | if __name__ == '__main__': 130 | create() 131 | -------------------------------------------------------------------------------- /docker/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | 4 | class images(object): 5 | @staticmethod 6 | def list(): 7 | titleList = [] 8 | newList = [] 9 | images = os.popen("docker images") 10 | listData = images.readlines() 11 | firstStr = True 12 | listData[0] = listData[0].replace('IMAGE ID', 'IMAGE_ID') 13 | tempList = list(listData[0]) 14 | for key in range(len(tempList)): 15 | if tempList[key] == ' ': 16 | firstStr = True 17 | elif firstStr == True: 18 | firstStr = False 19 | titleList.append({'start': key}) 20 | 21 | endKey = len(titleList) - 1 22 | for key in range(len(titleList)): 23 | if key == endKey: 24 | titleList[key]['end'] = -1 25 | titleList[key]['value'] = listData[0][titleList[key]['start']:titleList[key]['end']].rstrip() 26 | else: 27 | titleList[key]['end'] = titleList[key + 1]['start'] - 1 28 | titleList[key]['value'] = listData[0][titleList[key]['start']:titleList[key]['end']].rstrip() 29 | 30 | del listData[0] 31 | for str in listData: 32 | dictData = {} 33 | for title in titleList: 34 | dictData[title['value']] = str[title['start']:title['end']].strip() 35 | newList.append(dictData) 36 | del dictData 37 | 38 | return newList 39 | 40 | def del_image(self,images_id): 41 | os.popen("docker rmi "+images_id) 42 | 43 | def image_info(self, id): 44 | str = "docker inspect "+id 45 | str = os.popen(str).read() 46 | dict = json.loads(str) 47 | return dict[0] 48 | 49 | def docker_run(self, param, image, cmd=''): 50 | str = "docker run -d "+param+" "+image+" "+cmd 51 | print(str) 52 | result = os.popen(str).read() 53 | print(str,result) 54 | return dict 55 | 56 | class container(): 57 | @staticmethod 58 | def all(): 59 | titleList = [] 60 | newList = [] 61 | images = os.popen("docker ps -a") 62 | listData = images.readlines() 63 | firstStr = True 64 | listData[0] = listData[0].replace('CONTAINER ID', 'CONTAINER_ID') 65 | tempList = list(listData[0]) 66 | for key in range(len(tempList)): 67 | if tempList[key] == ' ': 68 | firstStr = True 69 | elif firstStr == True: 70 | firstStr = False 71 | titleList.append({'start': key}) 72 | 73 | endKey = len(titleList) - 1 74 | for key in range(len(titleList)): 75 | if key == endKey: 76 | titleList[key]['end'] = -1 77 | titleList[key]['value'] = listData[0][titleList[key]['start']:titleList[key]['end']].rstrip() 78 | else: 79 | titleList[key]['end'] = titleList[key + 1]['start'] - 1 80 | titleList[key]['value'] = listData[0][titleList[key]['start']:titleList[key]['end']].rstrip() 81 | 82 | del listData[0] 83 | for str in listData: 84 | dictData = {} 85 | for title in titleList: 86 | dictData[title['value']] = str[title['start']:title['end']].strip() 87 | newList.append(dictData) 88 | del dictData 89 | 90 | return newList 91 | 92 | @staticmethod 93 | def running(): 94 | titleList = [] 95 | newList = [] 96 | images = os.popen("docker ps -a --filter \"status=running\"") 97 | listData = images.readlines() 98 | firstStr = True 99 | listData[0] = listData[0].replace('CONTAINER ID', 'CONTAINER_ID') 100 | tempList = list(listData[0]) 101 | for key in range(len(tempList)): 102 | if tempList[key] == ' ': 103 | firstStr = True 104 | elif firstStr == True: 105 | firstStr = False 106 | titleList.append({'start': key}) 107 | 108 | endKey = len(titleList) - 1 109 | for key in range(len(titleList)): 110 | if key == endKey: 111 | titleList[key]['end'] = -1 112 | titleList[key]['value'] = listData[0][titleList[key]['start']:titleList[key]['end']].rstrip() 113 | else: 114 | titleList[key]['end'] = titleList[key + 1]['start'] - 1 115 | titleList[key]['value'] = listData[0][titleList[key]['start']:titleList[key]['end']].rstrip() 116 | 117 | del listData[0] 118 | for str in listData: 119 | dictData = {} 120 | for title in titleList: 121 | dictData[title['value']] = str[title['start']:title['end']].strip() 122 | newList.append(dictData) 123 | del dictData 124 | 125 | running = [] 126 | for data in newList: 127 | status = data['STATUS'].find('Up') 128 | if status != -1: 129 | running.append(data) 130 | return running 131 | 132 | @staticmethod 133 | def title(): 134 | images = os.popen("docker ps -a") 135 | listData = images.readlines() 136 | listData = listData[0].replace('CONTAINER ID', 'CONTAINER_ID') 137 | return tuple(listData.split()) 138 | 139 | @staticmethod 140 | def titleKeyToName(key): 141 | if key == "CONTAINER_ID": 142 | return "容器id" 143 | elif key == "IMAGE": 144 | return "镜像" 145 | elif key == "COMMAND": 146 | return "命令" 147 | elif key == "CREATED": 148 | return "创建时间" 149 | elif key == "STATUS": 150 | return "状态" 151 | elif key == "PORTS": 152 | return "端口" 153 | elif key == "NAMES": 154 | return "名称" 155 | return key 156 | 157 | 158 | @staticmethod 159 | def getIp(CONTAINER_ID): 160 | str = "docker inspect "+CONTAINER_ID 161 | str = os.popen(str).read() 162 | dict = json.loads(str) 163 | for data in dict[0]['NetworkSettings']['Networks'].values(): 164 | return data['IPAddress'] 165 | 166 | def stop(name): 167 | str = "docker stop "+name 168 | os.popen(str).read() 169 | 170 | def start(name): 171 | str = "docker start " + name 172 | os.popen(str).read() 173 | 174 | def delete(name): 175 | str = "docker rm " + name 176 | os.popen(str).read() --------------------------------------------------------------------------------