← Back to homedocker

Docker Swarm: Orchestration for Docker

← All writing

Introduction

Docker Swarm is Docker’s native clustering and orchestration solution. It allows you to manage multiple Docker containers deployed across multiple machines, making it easier to deploy, scale, and manage containerized applications. Docker Swarm ensures high availability, load balancing, and scaling for services running on a cluster of Docker nodes.

In this article, we’ll cover the fundamentals of Docker Swarm, including how to set up a Swarm cluster, deploy services, and scale applications. Additionally, I’ll provide a GitHub repository with a fully functional Docker Swarm example, featuring a sample docker-compose.yml, Dockerfile, and nginx.conf file.

What is Docker Swarm?

Docker Swarm is a native clustering tool that helps you manage Docker containers across a cluster of machines. With Swarm, you can group multiple Docker hosts into a single virtual Docker host. This cluster of machines can be managed as one, and Swarm ensures that the applications running on these containers are highly available, fault-tolerant, and easy to scale.

Some key features of Docker Swarm include:

  • Cluster Management: Easy management of a group of Docker engines.
  • Load Balancing: Automatically distributes traffic across services.
  • Service Scaling: Effortless scaling of containers.
  • High Availability: Automatic service recovery in case of node failure.

GitHub Repository

You can find all the code examples used in this guide in my GitHub repository:

🔗 GitHub Repo

Setting up Docker Swarm Cluster

To set up a Docker Swarm cluster, you need to have at least two machines (or two virtual machines) running Docker. One machine will be the “Manager” node, and the others will be “Worker” nodes.

Here’s how you can initialize Docker Swarm and set up a cluster:

1. Initialize the Docker Swarm Cluster on the Manager Node:

On your manager node, run the following command to initialize the Swarm:

docker swarm init

This command will give you a join-token for worker nodes.

2. Join Worker Nodes to the Cluster:

On each worker node, use the join-token provided by the manager node:

docker swarm join --token <worker_token> <manager_ip>:2377

3. Verify the Cluster Setup:

Once the worker nodes have joined, you can check the status of the nodes in the cluster by running:

docker node ls

Now you have a basic Docker Swarm cluster with one manager node and multiple worker nodes.

Deploying Services in Docker Swarm

In Docker Swarm, you deploy services, not individual containers. A service is a set of containers running the same image. Docker Swarm ensures that the desired number of replicas of each service is always running.

Example: Deploying a Simple Web Application

In the following example, we’ll deploy an Nginx-based web server in Docker Swarm using both a custom Dockerfile and docker-compose.yml. We’ll also explore how to use a custom nginx.conf for configuring the Nginx server.

1. Dockerfile Explanation

Here’s a sample Dockerfile used to create an image for the Nginx web server:

# Use the official Nginx image from Docker Hub
FROM nginx:latest

# Copy custom nginx configuration if needed (optional)
COPY nginx.conf /etc/nginx/nginx.conf

# Expose the port that Nginx will run on
EXPOSE 80

FROM nginx:latest: This uses the official Nginx image from Docker Hub as the base image.

COPY nginx.conf /etc/nginx/nginx.conf: This copies a custom Nginx configuration file into the container. The file controls how the Nginx server behaves, such as routing requests or handling error pages.

EXPOSE 80: This exposes port 80 so that the web server can serve content on that port.

2. Docker Compose File Explanation

The docker-compose.yml file defines the configuration for deploying services in Docker Swarm. Here’s an example:

version: '3'

services:
webserver:
image: nginx:latest
deploy:
replicas: 3
resources:
limits:
cpus: "0.5"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "8080:80"

image: nginx:latest: This specifies that the nginx:latest image will be used to deploy the service.

deploy: Defines how the service will be deployed in Swarm mode.

replicas: 3: This ensures that 3 replicas (containers) of the webserver service will be running.

resources: Defines resource limits for the containers:

cpus: “0.5”: This limits each replica to use 0.5 CPUs.

memory: 50M: This limits each replica’s memory to 50MB.

restart_policy: Ensures that the service is restarted if it fails.

condition: on-failure: Restart the service only if it fails.

ports: This maps port 8080 on the host machine to port 80 inside the container, making the web service accessible on port 8080.

3. Nginx Configuration File Explanation

Here’s an example of the nginx.conf file, which customizes the behavior of the Nginx server:

server {
listen 80;

server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

# Custom error page
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
}

listen 80: This tells Nginx to listen on port 80 for incoming HTTP requests.

server_name localhost: This sets the server name to localhost for local development.

location /: This defines how to handle requests to the root (/) path. The content is served from the /usr/share/nginx/html directory.

error_page 404 /404.html: Customizes the error page for HTTP 404 (not found) errors.

location = /404.html: This tells Nginx to serve a custom 404 error page if a requested page cannot be found.

Scaling Services in Docker Swarm

One of the main features of Docker Swarm is its ability to scale services up or down. This can be done easily by adjusting the number of replicas of a service.

Example: Scaling the Web Application Service

Let’s scale the previously deployed webserver service to 5 replicas:

docker service scale webserver=5

This command will add 2 more replicas to the service. You can check the status again by running:

docker service ps webserver

The number of replicas should now be 5.

Managing Swarm Services with docker stack

Docker Swarm also supports docker stack, a higher-level management tool for deploying and managing multi-container applications. It allows you to define a stack of services, networks, and volumes using a docker-compose.yml file.

Example: Docker Stack Deployment

You can deploy a stack of services with Docker Compose and Docker Swarm.

1. Create a docker-compose.yml file for the stack (as shown above).

2. Deploy the stack to Docker Swarm:

docker stack deploy -c docker-compose.yml my_stack

This will deploy both services (webserver in our case) in a Swarm mode and manage them as a part of a single stack.

3. Check the stack status:

docker stack ps my_stack

This will show the running services in the stack and their respective statuses.

Conclusion

Docker Swarm is a powerful and easy-to-use orchestration tool for Docker containers. It allows you to manage clusters of Docker nodes and deploy highly available, scalable applications with ease. By leveraging Docker Swarm, you can ensure high availability and fault tolerance for your containerized applications.

Key Takeaways:

  • Docker Swarm provides a simple way to orchestrate and manage Docker containers.
  • Service Deployment allows easy management of container replicas across multiple nodes.
  • Scaling your services is as simple as adjusting the number of replicas.
  • Docker Swarm integrates well with Docker Compose, enabling you to manage multi-container applications more efficiently.

Additional Resources

Docker Swarm Documentation

Docker Official Site

Docker Swarm Overview

🚀 Let’s Connect!

If you found this guide helpful, follow me for more DevOps and Cloud Engineering content:

🔗 GitHubgithub.com/Dhanika-Kumarasiri

🔗 Mediummedium.com/@dhanika-kumarasiri

Have questions? Drop them in the comments! Let’s automate AWS the smart way! 🚀

Originally published on Medium.

Read on Medium