Getting Started with Docker Swarm

Tutorial 1 of 5

Getting Started with Docker Swarm

1. Introduction

1.1 Tutorial Goal

The goal of this tutorial is to introduce you to Docker Swarm, a native clustering and scheduling tool for Docker. Docker Swarm transforms a group of Docker hosts into a single, virtual host.

1.2 Learning Objectives

By the end of this tutorial, you will be able to set up Docker Swarm, initialize a swarm, and start deploying services.

1.3 Prerequisites

  • Basic knowledge of Docker
  • Docker installed on your machine

2. Step-by-Step Guide

2.1 Docker Swarm Concepts

Docker Swarm uses standard Docker API and networking, making it easy to drop into an environment where you’re already working with Docker containers. Here are some key concepts:

  • Swarm: A swarm is a group of machines running Docker and joined into a cluster.
  • Node: A node is an instance of Docker. It can be a Docker daemon running on a physical machine or in a virtual machine.
  • Manager Nodes: Manager nodes perform the orchestration and cluster management functions required to maintain the desired state of the swarm.
  • Worker Nodes: Worker nodes are the default node type when you add a node to a swarm. Worker nodes receive and execute tasks dispatched from manager nodes.

2.2 Initializing a Swarm

To start a swarm, you need to run docker swarm init on the manager node with the IP address of that machine:

docker swarm init --advertise-addr <MANAGER-IP>

2.3 Deploying a Service

To deploy a service, you run docker service create:

docker service create --replicas 1 --name helloworld alpine ping docker.com

3. Code Examples

3.1 Initializing a Swarm

Here's an example of initializing a Docker Swarm:

docker swarm init --advertise-addr 192.168.1.100

This command initializes a new swarm where the manager node is running. The --advertise-addr flag configures the manager node to publish its address as 192.168.1.100.

3.2 Deploying a Service

Here's an example of deploying a service in Docker Swarm:

docker service create --replicas 3 --name my-web nginx

This command creates a service named my-web using the nginx image. The --replicas 3 flag tells the swarm to run three instances of the service at all times.

4. Summary

In this tutorial, you've learned how to set up Docker Swarm, initialize a swarm, and deploy services. Now you can create your own swarms and manage them with ease.

5. Practice Exercises

5.1 Exercise 1

Create a Docker Swarm and deploy a service with 5 replicas using the httpd image.

5.2 Exercise 2

Deploy a service with 2 replicas using the alpine image. The service should run the ping 8.8.8.8 command.

5.3 Solutions

  1. docker swarm init --advertise-addr <Your-IP>
    docker service create --replicas 5 --name my-web httpd
  2. docker service create --replicas 2 --name my-pinger alpine ping 8.8.8.8

6. Further Reading

Remember, practice is the key to mastering any skill, so keep experimenting with different scenarios and configurations. Happy Dockering!