N. P. O'Donnell, 2021
Installing:
pip install docker
Importing:
import docker
Can be done either locally through UNIX domain socket, or remotely over a TCP socket.
Locally:
client = docker.from_env()
Remotely:
client = docker.DockerClient("http://10.0.0.1:2375")
Default port is 2375.
List all nodes in the cluster:
from docker.models.nodes import Node
nodes: list[Node] = client.nodes.list()
Get attributes of a node:
node.attrs
Useful attributes (note capitalization):
node.attrs["ID"] # ID of the node.
node.attrs["Status"] # Status including IP address.
node.attrs["Description"]["Hostname"] # Hostname.
node.attrs["ManagerStatus"] # Status of swarm manager node.