Created
February 6, 2023 22:18
-
-
Save davydany/e6bd080c9bbd4733cca4aee9388ca5a4 to your computer and use it in GitHub Desktop.
Generate a list of docker nodes that are in the swarm, and the services running inside each of them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import json | |
# Get a list of all nodes in the Docker swarm | |
result = subprocess.run(["docker", "node", "ls", "--format", "{{json .}}"], stdout=subprocess.PIPE, text=True) | |
nodes = json.loads("[" + result.stdout.strip().replace("}\n{", "},\n{") + "]") | |
# Iterate over the nodes and print their hostname and IP address | |
for node in nodes: | |
print("Node:", node['Hostname']) | |
print("\tHostname:", node['Hostname']) | |
print("\tIP address:", node['Status']['Addr']) | |
print("\tServices:") | |
# Get a list of services running on this node | |
result = subprocess.run(["docker", "service", "ls", "--filter", "node={}".format(node['Hostname']), "--format", "{{json .}}"], stdout=subprocess.PIPE, text=True) | |
services = json.loads("[" + result.stdout.strip().replace("}\n{", "},\n{") + "]") | |
# Iterate over the services and print their names | |
for service in services: | |
print("\t\t-", service['Name'].strip("/").split("_")[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment