Docker is a containerization tool that enables the hosting of container-based workloads. Within Docker, Swarm functions as a grouping mechanism, allowing multiple containers to operate collectively in a coordinated manner. To external observers, the workload service appears as a single container, yet it efficiently manages and load balances requests across the multiple containers operating behind the scenes.
In this article, we will discuss docker swarm and learn to host our containerized applications on a swarm instance. We will make use of different servers that are running as a part of a docker swarm to run our workloads.
Swarm is a valuable stepping stone for those considering a transition to Kubernetes. While Kubernetes offers greater capabilities, the core principles and behaviour between the two are similar. Thus, skills acquired in Docker Swarm are transferable, making it an ideal platform for acquainting yourself with the Kubernetes ecosystem.
Basics of Swarm Mode
First, let us enable swarm mode on our docker container.
docker swarm init --advertise-addr ,<127.0.0.1> or private network ip
Once executed, a token will be provided in the output. That token can be used to connect other swarm instances with this swarm instance. This Swarm will be the main node, while the other nodes will act as secondaries.
To connect another server with this swarm network, simply run the following command:
docker swarm join --address <token>
Basic Commands of Swarm
docker stack ls = list swarm stacks
docker service = work with services
docker stack up/deploy = deploy/update workloads
docker stack rm = remove workloads
docker stack config = validate configuration
Anatomy of a Swarm Declarative Configuration
Swarm uses a declarative configuration, i.e. a YAML file. Similar to a docker-compose file, the swarm config is not much different. Only differing in a few parts where the features from compose don't carry over to Swarm.
Listed below are the config syntaxes unique to docker swarm.
Deploying Workloads In Our Docker Swarm Network
Here is an example swarm configuration that we will use for this demo. Go ahead and save it into a file.
To summarize, what we're doing above is creating a few resources on a custom docker network, separating volumes and injecting environment variables into our docker container.
To deploy the stack, run the command,
docker stack up --compose-file stack.yml demo-stack1
Updating Services and Handling Rollbacks In Docker Swarm
To update services in the docker swarm, run the commands below:
docker service update service_name --image image:tag
docker service rollback service_name <version_number>
Secrets In Docker Swarm
To create docker secrets, run the command given below:
docker secret create secret_name secret_file
The secret can be mounted into a docker container and used by said docker container. The default path is
/run/secrets. It can be changed by changing the config a bit like follows:
Configs In Docker Swarm
To create and import config in Swarm for applications like nginx, apache, etc, where you don't want to bind mount the file but rather use the config feature provided by Swarm, do as follows.
In this article, we learned about docker swarm, learned to set up a swarm network, deployed containerized workloads to docker swarm, played with secrets and utilized rollbacks and other important features provided to us by docker swarm.
Please comment below if you have any queries, I try to regularly update my articles to ensure legibility.