How to Install and Use Docker Swarm for Container Orchestration on Ubuntu 22.04 LTS

Container orchestration is key for efficiently managing distributed applications, and Docker Swarm offers a simple, yet powerful way to deploy and manage containerized workloads. This guide helps DevOps engineers and system administrators set up Docker Swarm on Ubuntu 22.04 LTS, enabling robust container orchestration, scalability, and improved service reliability. By the end of this tutorial, you’ll have a fully functioning Docker Swarm cluster capable of handling multiple services with ease.


Prerequisites

Before diving into the technical steps, ensure you have:

  • Administrative access and sudo permissions on the Ubuntu 22.04 LTS server.
  • Basic familiarity with Docker and Linux commands.
  • Additional Ubuntu nodes, if possible, to join and expand the Swarm cluster.

Step 1: Install Docker Engine

Docker Swarm relies on Docker Engine for container management. First, update your system and install Docker:

sudo apt update && sudo apt install docker.io -y

This will install the Docker Engine, which is essential for running containers on your system.

Step 2: Start and Enable Docker Services

Once Docker is installed, start and enable the service so it runs on startup:

sudo systemctl start docker
sudo systemctl enable docker

To confirm that Docker is active, use:

docker ps -a

Step 3: Initialize the Docker Swarm Cluster

On the main node (manager node), initialize the Swarm cluster by running:

docker swarm init --advertise-addr <your-ip-address>

Replace <your-ip-address> with the IP address of your server. This step creates a manager node and provides a command to use for other nodes to join the cluster.

Step 4: Join Worker Nodes to the Swarm

Copy the docker swarm join command provided after initializing the Swarm and run it on additional Ubuntu nodes you wish to join as workers. The command will look like:

docker swarm join --token <swarm-token> <manager-ip>:2377

Verify the cluster status by running:

docker node ls

This will list all nodes and their roles (manager or worker).

Step 5: Deploy a Service Using Docker Swarm

Create a simple docker-compose.yml file to define the service you want to deploy:

version: '3'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure

This configuration deploys an Nginx service with three replicas, each listening on port 80. Deploy the stack using:

docker stack deploy -c docker-compose.yml my-service

Verify the deployment status with:

docker service ls

To view details of the running tasks, use:

docker service ps my-service

Best Practices

  • Load Balancing: Leverage Docker Swarm’s built-in load balancing by scaling services with docker service scale.
  • Rolling Updates: Implement rolling updates to avoid downtime by modifying the deploy section in docker-compose.yml with update_config.
  • Monitoring: Use docker logs, docker service logs, and third-party tools like Prometheus and Grafana for monitoring and alerting.

Troubleshooting

  • Swarm Initialization Issues: Ensure no firewalls are blocking port 2377, which is required for Swarm communication.
  • Container Start Failures: Check the logs of the container with:
  docker logs <container-id>
  • Node Communication: If nodes cannot communicate, verify network connectivity and Docker’s swarm ports (2377, 7946, and 4789).

For more detailed support, refer to Docker’s troubleshooting guide.


Conclusion

Setting up Docker Swarm on Ubuntu 22.04 LTS allows for scalable and effective container orchestration, simplifying the management of containerized applications. By following this guide, you have configured a Swarm cluster, added worker nodes, and deployed services, preparing you for more complex setups in the future.

Next Steps:

  • Explore Docker secrets for managing sensitive data securely within your Swarm cluster.
  • Integrate CI/CD pipelines with Jenkins or GitLab CI/CD for automated deployments.
  • Experiment with horizontal scaling and service updates to handle increasing traffic and workload demands.

This guide provides a foundation for robust container orchestration and prepares you for building resilient, scalable infrastructure.