0Pricing
Docker & DevOps Fundamentals · Lesson

Setting Up a Swarm Cluster

Initialize a Docker Swarm, add manager and worker nodes, and inspect the cluster state.

Setting Up a Swarm Cluster is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome to Docker Swarm Setup!

Ready to orchestrate your containers? In this lesson, we'll learn how to transform a single Docker host into a powerful Swarm cluster.

You'll discover how to initialize a Swarm, add new machines as manager and worker nodes, and verify your cluster's health.

Setting Up a Swarm Cluster — illustration 1

Swarm Setup Prerequisites

Before we dive in, make sure you have a few things ready:

  • Docker Desktop/Engine: Installed on all machines you plan to use.
  • Network Access: All machines must be able to communicate with each other over the network.
  • Open Ports: Docker Swarm uses specific ports for communication. Ensure they are open:
    • TCP port 2377 for cluster management communication.
    • TCP & UDP port 7946 for container network overlay.
    • UDP port 4789 for overlay network traffic.

Initializing Your First Swarm

To start a new Swarm, you pick one machine to be the first manager node. This node will handle all the orchestration tasks.

You use the docker swarm init command to kick things off. It turns your current Docker Engine into a Swarm manager.

By default, it uses the IP address of the current machine. You can specify a different IP with --advertise-addr if needed.

Demo: Initialize Swarm

Let's initialize a new Swarm. Run this command on your chosen manager machine. After initialization, we'll check its status.

Note: You might need to replace YOUR_MANAGER_IP with your actual machine's IP address if Docker can't auto-detect it correctly.

docker swarm init --advertise-addr YOUR_MANAGER_IP
docker node ls

Understanding Swarm Node Roles

In a Docker Swarm, nodes have distinct roles:

  • Manager Nodes: These are the brains of the Swarm. They handle cluster management, orchestrate services, and maintain the desired state of your applications. For high availability, you typically have multiple managers.
  • Worker Nodes: These nodes are where your actual containerized applications run. They receive tasks from manager nodes and execute them.

A single node can be both a manager and a worker.

Adding Worker Nodes to Swarm

Once your Swarm is initialized, you can add more machines as worker nodes. This expands your cluster's capacity to run containers.

When you run docker swarm init, it outputs a command like docker swarm join --token :2377. You use this command on other machines to join them as workers.

The token is crucial for authentication and security.

Demo: Worker Join Command

This is an example of the command you would run on a separate machine (your intended worker) to join the Swarm. Remember to replace SWMTKN-... with your actual token and MANAGER_IP with your manager's IP.

Note: This command cannot be run directly in this environment as it requires a multi-machine setup. It's for demonstration purposes.

docker swarm join \
  --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
  192.168.1.100:2377

Inspecting Swarm Cluster State

After adding nodes, it's good practice to verify that they have successfully joined the Swarm.

You can use the docker node ls command on any manager node to view all nodes in the Swarm, their status, availability, and roles.

A node's STATUS should be Ready and AVAILABILITY should be Active for it to be fully operational.

Quick Check: Swarm Initialization

You've decided to set up a new Docker Swarm. Which command would you use on your first host machine to make it the initial manager node?

Recap: Setting Up a Swarm

Great job! You've learned the essentials of setting up a Docker Swarm cluster:

  • docker swarm init: Used to create a new Swarm and designate the current machine as the first manager.
  • docker swarm join: Used to add other machines as worker or manager nodes to an existing Swarm.
  • docker node ls: Allows you to inspect the status and roles of all nodes within your Swarm cluster.

With these commands, you can build a robust foundation for orchestrating your containerized applications!

Frequently asked questions

Is the “Setting Up a Swarm Cluster” lesson free?

Yes — the full text of “Setting Up a Swarm Cluster” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.

What will I learn in “Setting Up a Swarm Cluster”?

Initialize a Docker Swarm, add manager and worker nodes, and inspect the cluster state. You practise Docker & DevOps Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Docker & DevOps Fundamentals?

No prior experience is required. Docker & DevOps Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Setting Up a Swarm Cluster” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Docker & DevOps Fundamentals lesson?

Yes. Every Docker & DevOps Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Docker Swarm
  2. Setting Up a Swarm Cluster
  3. Deploying Services to Swarm
  4. Scaling and Rolling Updates in Swarm
← Back to Docker & DevOps Fundamentals