Skip to content

Instantly share code, notes, and snippets.

@narate
Last active August 12, 2023 08:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save narate/a526bddf953f5dea367e64fc2995b443 to your computer and use it in GitHub Desktop.
Save narate/a526bddf953f5dea367e64fc2995b443 to your computer and use it in GitHub Desktop.
Show all running docker services details for usable Copy and Paste
import docker
import json
def to_mounts_string(mounts):
l = []
for m in mounts:
l.append('Type = {}, Source = {}, Target = {}'
.format(m.get('Type'), m.get('Source'), m.get('Target')))
return json.dumps(l, sort_keys=True, indent=4, ensure_ascii=False)
def to_networks_string(nets):
l = []
for n in nets:
l.append('Target = {}'.format(n.get('Target','')))
return json.dumps(l, sort_keys=True, indent=4, ensure_ascii=False)
def to_ports_string(ports):
l = []
for p in ports:
l.append('TargetPort = {}, PublishedPort = {}, Protocol = {}, PublishMode = {}'
.format(p.get('TargetPort'), p.get('PublishedPort'), p.get('Protocol'), p.get('PublishMode')))
return json.dumps(l, sort_keys=True, indent=4, ensure_ascii=False)
client = docker.from_env()
service_list = client.services.list()
print("Services")
print("--------")
for v in service_list:
name = v.name
service_details = client.services.get(name)
attrs = service_details.attrs
spec = attrs.get('Spec')
replicas = spec.get('Mode').get('Replicated', { 'Replicas': 'Global' }).get('Replicas')
networks = spec.get('Networks')
task = spec.get('TaskTemplate').get('ContainerSpec')
image = task.get('Image') + '@' # Ignore sha256 digest
env = task.get('Env', [])
args = task.get('Args', [])
mounts = task.get('Mounts', [])
ports = spec.get('EndpointSpec').get('Ports', [])
hostname = task.get('Hostname')
print("Name : {}".format(name))
print('- Replicas : {}'.format(replicas))
print('- Image : {}'.format(image[0:image.index('@')]))
print('- Env : {}'.format(', '.join(env)))
print('- Args : {}'.format(' '.join(args)))
print('- Mounts : {}'.format(to_mounts_string(mounts)))
print('- Network : {}'.format(to_networks_string(networks)))
print('- Ports : {}'.format(to_ports_string(ports)))
print('- Hostname : {}'.format(hostname))
print("\n")
@narate
Copy link
Author

narate commented Jun 22, 2017

Run

# Run as root
python  docker_service_report.py

Output

Services
--------
Name : etcd
- Replicas : 1
- Image : elcolio/etcd:latest
- Env :
- Args : -name etcd -discovery=https://discovery.etcd.io/786fd921c5bbd89c7ac95f9a9ab9cc87
- Mounts : []
- Network : [
    "Target = dc"
]
- Ports : [
    "TargetPort = 2379, PublishedPort = 2379, Protocol = tcp, PublishMode = ingress",
    "TargetPort = 2380, PublishedPort = 2380, Protocol = tcp, PublishMode = ingress",
    "TargetPort = 4001, PublishedPort = 4001, Protocol = tcp, PublishMode = ingress",
    "TargetPort = 7001, PublishedPort = 7001, Protocol = tcp, PublishMode = ingress"
]
- Hostname : None
...

@phondanai
Copy link

สวยงาม
ยังไม่ได้ลอง!

@narate
Copy link
Author

narate commented Jun 22, 2017

99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment