Docker Swarm is one of the easiest container clusters to deploy. In minutes, your cluster can be up and running for high availability, failover, and scalability. Once up and running, you can then deploy containers on the swarm to take advantage of the cluster. For example, you can deploy a service that can scale to meet any demand that arises.
SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)
That’s exactly what I’m going to show you. Here we will first install Docker Swarm and then deploy a service to the new cluster so that it can be scaled to any degree that meets your business needs.
What you will need
I will demonstrate on a cluster consisting of one controller and two nodes, all of which will run on Ubuntu Server 20.04. If you’re using another Linux distribution, you may need to modify the Docker installation steps (but nothing more).
That said, let’s get to the spin-off.
How to install Docker
The first thing to do is to install Docker. Be sure to follow these same steps on your controller and regardless of how many nodes you plan to deploy.
Connect to your server and update apt with:
sudo apt-get update
Then, install the necessary dependencies with the command:
sudo apt-get install ca-certificates curl gnupg lsb-release -y
Add the official Docker GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add the stable Docker repository with:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker Engine with:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
Start and activate Docker with:
sudo systemctl enable --now docker
Add your user to the docker group with the command:
sudo usermod -aG docker $USER
Make the system aware of the new group with:
newgrp docker
Repeat the above steps for all your nodes.
Back at the Docker controller, initialize the swarm with:
docker swarm init --advertise-addr SERVER
Where SERVER is the IP address of the Docker controller.
You will then be presented with the join command which will look like this:
docker swarm join --token SWMTKN-1-46uxtlbe3wrelly1fe5e65p1wdvg95bcjo48izvptpwof62rdo-42yl4jprovhng56sgxmyv7arv 192.168.1.13:2377
Copy this command and run it from all your nodes. Once you’ve done that, you can verify the join by running the following command on the controller:
docker info
You should see output similar to this:
Swarm: active
NodeID: wb44efzwy68x9gek45ee1nbnb
Is Manager: true
ClusterID: vjec4hz1sjj535x9w0mspox87
Managers: 1
Nodes: 3
Default Address Pool: 10.0.0.0/8
SubnetSize: 24
Data Path Port: 4789
Orchestration:
Task History Retention Limit: 5
How to deploy a service on the swarm
Now we can deploy a service to our swarm. Let’s keep it simple at first and deploy an NGINX container service that we can’t interact with. To do this, run the following command on the controller:
docker service create --name nginx_test nginx
To check the status of the service, issue the command:
docker service ls
You should see that our NGINX service has replicated with output similar to this:
zie1n4nm5es3 nginx_test replicated 1/1 nginx:latest
Our example above only takes advantage of one of our nodes. What if we wanted to deploy this service on all three nodes? For that, our command would look like this:
docker service create --replicas 3 --name nginx3nodes nginx
Issue the command:
docker service ls
You should see that our nginx3nodes deployment has replicated to 3 of the 3 nodes with the following result:
y1yu8fq27aab nginx3nodes replicated 3/3 nginx:latest
The service now takes advantage of all three nodes in our cluster. You can reduce this service to 2 nodes with the command:
docker service scale nginx3nodes=2
Check the status with:
docker service ls
You should now see the nginx service on nodes 2/2.
Let’s say you have five nodes in your swarm. If you want to scale the service to all five nodes, the command would be:
docker service scale nginx3nodes=5
To remove the service, run the command:
docker service rm nginx3nodes
Suppose you want to update the container image in your deployment. It’s been a few days since its initial deployment, and you want to make sure you’re using the latest container image available. We will assume that nginx:latest is an updated container image and to update this service with the new image you will issue the command:
docker service update --image nginx:latest nginx3nodes
One last note, if you want to manage the swarm more easily, deploy Portainer on the controller with the command:
docker run -d -p 8000:8000 -p 9443:9443 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
Once deployed, navigate to http://SERVER:9443 (Where SERVER is the IP address of the server). After creating an admin user and logging in, you should see Swarm listed in the left navigation. Click it to view your cluster (Figure A).
Figure A

And that’s pretty much the basis of creating a Docker Swarm and deploying a service to the nodes.
Subscribe to TechRepublic How to make technology work on YouTube for all the latest tech tips for professionals from Jack Wallen.