Docker nodes
Nodes in Docker swarm
Don't use the constructor directly. Instead use
from python_on_whales import docker
my_docker_image = docker.node.inspect("my-node-name")
list_of_nodes = docker.node.list()
For type hints, use this
from python_on_whales import Node
def print_state(node: Node):
print(node.status.state)
Attributes
It attributes are the same that you get with the command line:
docker node inspect ...
To get a complete description of those attributes, you can take a look at the daemon api reference page and click on "200 No error".
An example is worth many lines of descriptions.
In [1]: from python_on_whales import docker
In [2]: docker.swarm.init()
In [3]: docker.node.list()[0]
In [4]: def super_print(obj):
...: print(f"type = {type(obj)}, value = {obj}")
...:
In [4]: super_print(node.id)
type = <class 'str'>, value = 4dn1t4n3pm9y5g4oyeljiobxe
In [5]: super_print(node.version.index)
type = <class 'int'>, value = 9
In [6]: super_print(node.created_at)
type = <class 'datetime.datetime'>, value = 2024-09-06 10:23:44.965540+00:00
In [7]: super_print(node.updated_at)
type = <class 'datetime.datetime'>, value = 2024-09-06 10:23:45.570170+00:00
In [8]: super_print(node.spec.name)
type = <class 'NoneType'>, value = None
In [9]: super_print(node.spec.labels)
type = <class 'dict'>, value = {}
In [10]: super_print(node.spec.role)
type = <class 'str'>, value = manager
In [11]: super_print(node.spec.availability)
type = <class 'str'>, value = active
In [12]: super_print(node.description.hostname)
type = <class 'str'>, value = fv-az1778-269
In [13]: super_print(node.description.platform.architecture)
type = <class 'str'>, value = x86_64
In [14]: super_print(node.description.platform.os)
type = <class 'str'>, value = linux
In [15]: super_print(node.description.resources.nano_cpus)
type = <class 'int'>, value = 4000000000
In [16]: super_print(node.description.resources.memory_bytes)
type = <class 'int'>, value = 16757342208
In [17]: super_print(node.description.resources.generic_resources)
type = <class 'NoneType'>, value = None
In [18]: super_print(node.description.engine.engine_version)
type = <class 'str'>, value = 26.1.3
In [19]: super_print(node.description.engine.labels)
type = <class 'NoneType'>, value = None
In [20]: super_print(node.description.engine.plugins)
type = <class 'list'>, value = [EnginePlugin(type='Log', name='awslogs'), EnginePlugin(type='Log', name='fluentd'), EnginePlugin(type='Log', name='gcplogs'), EnginePlugin(type='Log', name='gelf'), EnginePlugin(type='Log', name='journald'), EnginePlugin(type='Log', name='json-file'), EnginePlugin(type='Log', name='local'), EnginePlugin(type='Log', name='splunk'), EnginePlugin(type='Log', name='syslog'), EnginePlugin(type='Network', name='bridge'), EnginePlugin(type='Network', name='host'), EnginePlugin(type='Network', name='ipvlan'), EnginePlugin(type='Network', name='macvlan'), EnginePlugin(type='Network', name='null'), EnginePlugin(type='Network', name='overlay'), EnginePlugin(type='Volume', name='local'), EnginePlugin(type='Volume', name='mochoa/s3fs-volume-plugin:latest')]
In [21]: super_print(node.description.tls_info.trust_root)
type = <class 'str'>, value = -----BEGIN CERTIFICATE-----
MIIBaTCCARCgAwIBAgIULCcCmJ2q+nY9VFGAgY5M+QzLXv4wCgYIKoZIzj0EAwIw
EzERMA8GA1UEAxMIc3dhcm0tY2EwHhcNMjQwOTA2MTAxOTAwWhcNNDQwOTAxMTAx
OTAwWjATMREwDwYDVQQDEwhzd2FybS1jYTBZMBMGByqGSM49AgEGCCqGSM49AwEH
A0IABNCsjCy8rSrJqD9jY/GPQJndu4URGSKg9JxtiZlw9wsVrNwwu9mH+WxgRqYR
OJORasZGPc2eQCMF5NFecWtyvIWjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMB
Af8EBTADAQH/MB0GA1UdDgQWBBTeUwx4jqMw83ntvmPlrretSieCUzAKBggqhkjO
PQQDAgNHADBEAiAXgSE1LfaY9yVZvHGV1c/9BvoJwZAsEHS4NIEMSajmqQIgHLc6
daWQ9vojza6mWxgF2SpjwZ8+nsWzxYj7WAWHoQk=
-----END CERTIFICATE-----
In [22]: super_print(node.description.tls_info.cert_issuer_subject)
type = <class 'str'>, value = MBMxETAPBgNVBAMTCHN3YXJtLWNh
In [23]: super_print(node.description.tls_info.cert_issuer_public_key)
type = <class 'str'>, value = MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE0KyMLLytKsmoP2Nj8Y9Amd27hREZIqD0nG2JmXD3CxWs3DC72Yf5bGBGphE4k5FqxkY9zZ5AIwXk0V5xa3K8hQ==
In [24]: super_print(node.status.state)
type = <class 'str'>, value = ready
In [25]: super_print(node.status.message)
type = <class 'NoneType'>, value = None
In [26]: super_print(node.status.addr)
type = <class 'str'>, value = 10.1.0.57
In [27]: super_print(node.manager_status.leader)
type = <class 'bool'>, value = True
In [28]: super_print(node.manager_status.reachability)
type = <class 'str'>, value = reachable
In [29]: super_print(node.manager_status.addr)
type = <class 'str'>, value = 10.1.0.57:2377
Methods
Node
ps
ps()
Returns the list of tasks running on this node
Returns
A `List[python_on_whales.Task]` object.
update
update(availability=None, labels_add={}, rm_labels=[], role=None)
Updates this Swarm node.
See docker.node.update
for more information
about the arguments.