Skip to content

Instantly share code, notes, and snippets.

@jonepl
Last active May 10, 2024 14:52
Show Gist options
  • Save jonepl/89d070afe74feb9b733d4e4b5f504cf3 to your computer and use it in GitHub Desktop.
Save jonepl/89d070afe74feb9b733d4e4b5f504cf3 to your computer and use it in GitHub Desktop.

Docker Swarm Cheat Sheet

Starting/Stopping a Docker Swarm

# Turns your docker server into a manager of the Docker Swarm
$ docker swarm init

# Turns off docker swarm
$ docker swarm leave -f

Deploying application

connect cli to manager run commands on control plane.

# Creates a docker stack. Deploys compose file to your cluster
$ docker stack deploy -c docker-compose.yml csv-merger

# Removes a created docker stack
$ docker stack rm csv-merger

Networking

# Create network
$ docker network create --driver overlay portfolio-network

# View all networks
$ docker network ls

Storing Configurations

Creating Configuration

# Create a config file within the cluster database
$ docker config create <config-name> <path-to-config-file> 

$ docker config inspect --pretty <config-name>

$ docker config ls

Creating Secrets (Configuration)

# Create a config file within the cluster database
$ docker secret create <secret-name> <path-to-secret-file>

$ docker secret inspect --pretty <secret-name>

$ docker secret ls

Using Secrets & Configurations

version: "3.7"

services:
  products-db:
    image: ...
    ports:
      - ...
    environment:
      - ...
    config:
      - source: <config-name>
        target: <path-to-config-file>
    secrets:
      - source: <secret-name>
        target: <path-to-secret-file>
        mode: 0400

configs:
    <config-name>:
        external: true
    
secrets:    
    <secret-name>:
        external: true

Helpful Commands

# List nodes in the swarm
$ docker node ls

# Add worker to cluster
$ docker swarm join-token worker

# Add manager to control plane
$ docker swarm join-token manager

# list services
$ docker service ls

# list stacks
$ docker stack ls

# list networks
$ docker network ls

# list containers
$ docker ps

Terminology

  • Node - a single physical or virtual machine that is part of the swarm cluster. A node can either me a manager or a worker node.

  • Manger Node - These nodes are responsible for managing the state of the swarm, scheduling tasks, and maintaining the desired state of the cluster. There is usually one or more manager nodes in a swarm for high availability and fault tolerance. Manager nodes are also responsible for coordinating the worker nodes.

  • Worker Node - These nodes are responsible for executing the tasks that are assigned to them by the manager nodes. Worker nodes do not participate in the decision-making process; instead, they run the services and tasks as instructed by the manager nodes

  • Docker Swarm Cluster - a group of interconnected nodes (physical or virtual machines) that work together to manage and run containerized applications

  • Control Plane - primary consisting of manager nodes, this is responsible for managing the overal state and behavior of the cluser. Scheduling, monitoring and orchestrating task and services across the cluster.

Abstractions

  • Services - a higher-level abstraction that represents a group of containers running the same application or performing the same function. First class objects

  • Stacks - a higher-level abstraction that allows you to define and manage a group of interrelated services, networks, and volumes as a single unit. A stack is essentially a blueprint for deploying a multi-service application in a Docker Swarm cluster.

When in swarm connect CLI to manager and run commands on the control plane

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment