Skip to content

Docker Swarm Deployment

Bases: DockerResourceManager

DockerSwarmManager is a utility for managing Docker Swarm services, including creating, inspecting, updating, and removing services. It extends DockerResourceManager to provide swarm-specific functionalities and commands via a command-line interface (CLI).

The manager interacts with the Docker Swarm API, offering a convenient way to manage Swarm services, nodes, and other swarm-related tasks from the command line.

CLI Usage

genius docker swarm sub-command

Sub-commands
  • list_nodes: List all nodes in the Docker Swarm. genius docker swarm list_nodes
  • inspect_node: Inspect a specific Swarm node by its ID. genius docker swarm inspect_node <node_id>
  • create_service: Create a new service in the Docker Swarm with comprehensive specifications. genius docker swarm create_service [options]
  • list_services: List all services in the Docker Swarm. genius docker swarm list_services
  • inspect_service: Inspect a specific service by its ID. genius docker swarm inspect_service <service_id>
  • update_service: Update an existing service with new parameters. genius docker swarm update_service <service_id> [options]
  • remove_service: Remove a service from the Docker Swarm. genius docker swarm remove_service <service_id>
  • service_logs: Retrieve logs of a Docker Swarm service. genius docker swarm service_logs <service_id> [--tail] [--follow]
  • scale_service: Scale a service to a specified number of replicas. genius docker swarm scale_service <service_id> <replicas>

Each sub-command supports various options to specify the details of the swarm node or service operation. These options include node and service IDs, image and command specifications for services, environment variables, resource limits, and much more.

Attributes:

Name Type Description
swarm_client

The Docker Swarm client connection to interact with the Docker Swarm API.

log

Logger for the class to log information, warnings, and errors.

console

Rich console object to print formatted and styled outputs.

Methods
  • connect_to_swarm: Method to establish a connection to the Docker Swarm.
  • list_nodes: Method to list all nodes in the Docker Swarm.
  • inspect_node: Method to inspect details of a specific Swarm node.
  • create_service: Method to create a new service with given specifications.
  • list_services: Method to list all services in the Docker Swarm.
  • inspect_service: Method to inspect a specific service.
  • update_service: Method to update an existing service with new parameters.
  • remove_service: Method to remove a service from the Docker Swarm.
  • get_service_logs: Method to retrieve logs of a Docker Swarm service.
  • scale_service: Method to scale a service to a specified number of replicas.
Note
  • Ensure that the Docker Swarm is initialized and running.
  • Make sure to have the necessary permissions to interact with the Docker Swarm and manage services and nodes.

__init__()

Initialize the Docker Swarm Manager.

connect_to_swarm(base_url='unix://var/run/docker.sock')

Connect to the Docker Swarm.

Parameters:

Name Type Description Default
base_url str

URL to the Docker daemon.

'unix://var/run/docker.sock'

create_parser(parser)

Extend the parser for CLI commands to include Docker Swarm operations.

Parameters:

Name Type Description Default
parser ArgumentParser

The existing parser.

required

Returns:

Name Type Description
ArgumentParser ArgumentParser

The extended parser with Docker Swarm operations.

create_service(image, command, args)

Create a new service in the Docker Swarm with comprehensive specifications.

Parameters:

Name Type Description Default
image str

Docker image to use for the service.

required
command Union[str, List[str]]

Command to run in the service.

required
args Namespace

Arguments from the CLI for service creation.

required

Returns:

Name Type Description
str str

ID of the created service.

get_service_logs(service_id, tail=100, follow=False)

Retrieve logs of a Docker Swarm service.

Parameters:

Name Type Description Default
service_id str

ID of the service.

required
tail int

Number of lines to tail from the end of the logs. Defaults to 100.

100
follow bool

Follow log output. Defaults to False.

False

inspect_node(node_id)

Inspect a specific Swarm node.

Parameters:

Name Type Description Default
node_id str

ID of the node to inspect.

required

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Node details.

inspect_service(service_id)

Inspect a specific service in the Docker Swarm.

Parameters:

Name Type Description Default
service_id str

ID of the service to inspect.

required

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Service details.

list_nodes()

List all nodes in the Docker Swarm.

Returns:

Type Description
List[Any]

List[Any]: List of Swarm nodes.

list_services()

List all services in the Docker Swarm.

Returns:

Type Description
List[Any]

List[Any]: List of services.

remove_service(service_id)

Remove a service from the Docker Swarm.

Parameters:

Name Type Description Default
service_id str

ID of the service to remove.

required

run(args)

Run the Docker Swarm Manager based on the parsed CLI arguments.

Parameters:

Name Type Description Default
args Namespace

The parsed CLI arguments.

required

scale_service(service_id, replicas)

Scale a Docker Swarm service to a specified number of replicas.

Parameters:

Name Type Description Default
service_id str

ID of the service to scale.

required
replicas int

Desired number of replicas.

required

update_service(service_id, image, command, args)

Update an existing service in the Docker Swarm.

Parameters:

Name Type Description Default
service_id str

ID of the service to update.

required
args Namespace

Arguments from the CLI for service update.

required