Skip to content

Instantly share code, notes, and snippets.

@npodonnell
Last active December 29, 2021 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npodonnell/1e2966f00c891e308c1236ee0d32631d to your computer and use it in GitHub Desktop.
Save npodonnell/1e2966f00c891e308c1236ee0d32631d to your computer and use it in GitHub Desktop.
Docker API Cheatsheet

Docker Python API Cheatsheet

N. P. O'Donnell, 2021

Installing and Importing

Installing:

pip install docker

Importing:

import docker

Getting a Client

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.

Swarm

Nodes

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment