54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
# @author yutent<yutent.io@gmail.com>
|
|
# @date 2024/01/15 15:38:34
|
|
|
|
import subprocess, json
|
|
from datetime import datetime
|
|
|
|
def toISOTime(time_str):
|
|
_time = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S %z %Z')
|
|
return _time.isoformat()
|
|
|
|
|
|
|
|
class Docker:
|
|
|
|
def containers(self, all = True):
|
|
if all:
|
|
cmd = 'docker container ls -a --format=json'
|
|
else:
|
|
cmd = 'docker container ls --format=json'
|
|
|
|
out = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
|
|
|
|
out = out.stdout.decode().strip().split("\n")
|
|
out = [json.loads(it) for it in out]
|
|
|
|
return [{
|
|
"id": it['ID'],
|
|
"name": it['Names'],
|
|
"image": it['Image'],
|
|
"cmd":it['Command'],
|
|
"state": it['State'],
|
|
'status': it['Status'],
|
|
'port': it['Ports'].split(', ')[0],
|
|
'created': toISOTime(it['CreatedAt']),
|
|
'last': it['RunningFor'],
|
|
} for it in out]
|
|
|
|
|
|
def images(self):
|
|
cmd = 'docker images --format=json'
|
|
|
|
out = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
|
|
|
|
out = out.stdout.decode().strip().split("\n")
|
|
out = [json.loads(it) for it in out]
|
|
|
|
return [{
|
|
"id": it['ID'],
|
|
"name": it['Repository'],
|
|
"tag": it['Tag'],
|
|
'size': it['Size'],
|
|
'created': toISOTime(it['CreatedAt']),
|
|
} for it in out] |