-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadInspect.py
More file actions
55 lines (45 loc) · 1.42 KB
/
Copy pathreadInspect.py
File metadata and controls
55 lines (45 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
import os
import sys
def readInspect(container_name):
base_shell='docker inspect'
total_shell=base_shell+' '+container_name
result=os.popen(total_shell).read()
status=os.wait()[1]
if status == 0:
#判断执行结果
result_dict = json.loads(result)[0]
#镜像
c_image=result_dict['Config']['Image']
#print(c_image)
#容器名称,单一
c_name=result_dict['Name'][1:]
#print(c_name)
#绑定信息,多条
c_bind=''
if result_dict['HostConfig']['Binds'] != None:
for bind in result_dict['HostConfig']['Binds']:
c_bind=c_bind+' -v '+bind
#print(c_bind)
#网络信息
c_network=result_dict['HostConfig']['NetworkMode']
#print(c_network)
#Cmd
c_cmd=''
if result_dict['Config']['Cmd'] != None:
for cmd in result_dict['Config']['Cmd']:
c_cmd=c_cmd+' '+cmd
#print(c_cmd)
#docker command
print('docker run -d --name=%s --network==%s %s %s %s' % (c_name,c_network,c_bind,c_image,c_cmd))
else:
print('脚本执行异常')
def usage(filename):
print('用法:')
print(' python %s container_name' % filename)
if __name__ == '__main__':
py_name=os.path.basename(sys.argv[0])
if len(sys.argv) != 2:
usage(py_name)
else:
readInspect(sys.argv[1])