Docker is the leading container platform and I have been working with Docker Swarm Cluster configuration in last few weeks on Ubuntu Server 18.04.3 LTS, so I thought to writeup an article to guide my followers, who would like to start the swarm cluster configuration from the scratch, so this is Docker CE on Ubuntu.
Before we start, if you have a question or requirement to clarify what Docker CE (Docker Community Edition) and Docker EE (Enterprise Edition) is, I hope it is the place to start learning Docker.
Docker EE (Enterprise Edition)
Docker EE is the certified container platform for almost all the Operating Systems and Cloud providers, Docker EE provide cooporative support for certified containers and plugins. So, it is the product for official and production use for the containerized applications.
Docker CE (Community Edition)
Docker CE, is the version for the free Docker products. Docker CE is a great full Docker platform for the developers who, actively involved with the development work in the containerized applications. This community supported Docker platform is available for free-of-cost.
Installing Docker CE On Ubuntu
I have deployed Two Virtual Machines with Ubuntu 18.04.3 LTS Operating System and updated the OS before the installation. Below required packages installed prior to the Docker installation. In my screenshots, commands displays as a single line but for the easy visibility, I have added them as lines in my code snippets below
sudo apt-get -y install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

Added the Docker GPG Key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
And also, added the repository
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

Updated the OS and packages, just to make sure we are up-to-date before the installation
sudo apt-get update

Installed the Docker CE
sudo apt-get install docker-ce

Once it completed, we need to add the current user account to the docker user group, otherwise we won’t be able to run the docker commands. Once you added the user make sure to exit and login again, otherwise user permissions will not be applied. Below screen capture shows the permission error after adding my user account to the “docker” user group
sudo usermod -aG docker YOUR_USER_ACCOUNT

Command successful after exiting and, login in to the system again

I have setup two Virtual Machines to work as “Docker Swarm Manager” and “Worker” Nodes. You need to install Docker CE following the above procedures both nodes.
Deploy Docker Swarm Cluster
Now we have successfully installed the Docker CE and, it’s time to deploy the Docker Swarm Cluster. Before that we need to identify the actual interface of the Server which we are going to treat as the Docker Swarm Manager. If you just execute the “ifconfig” command, you might be seen few interfaces and you need to identify the actual network interface to initialize the Docker Swarm Cluster.
Read More: How To Start Working With Google Kubernetes Engine (GKE)
After identifying the interface, initialize the cluster with the below command and, make sure to note down the command displaying after that, which we need to use in Worker nodes to connect them to the Docker Manager. It consists the “Join Token” for the worker node.
docker swarm init --advertise-addr IP_ADDRESS_OF_THE_NODE

Execute the displayed command in the other Virtual Machine to act it as the Worker Node, and join to the Swarm Cluster.

To check the cluster status, execute below command and you can identify the Docker Manager as the “Leader”, under “Manager Status“
docker node ls

So, we have a two node Docker Swarm Cluster configured and let’s see few other essential commands for ease of use.
Just assume that you forgot the “Join Token” for the worker node, and you need to connect another Docker Manager to the cluster. You can generate the “Join Tokens” again with below commands
#For Worker
docker swarm join-token worker
#For Manager
docker swarm join-token manager

Deploy A Sample Web Service In The Docker Swarm Cluster
Let’s see how we can deploy a simple web service on the Docker Swarm cluster.
We do not have the downloaded Docker images yet, and I have deployed a simple web service in the swarm cluster with two replicas. Theses replicas automatically distributed in these two nodes to provide the high availability to the application.
#To view the docker images
docker images
#To run a service with two replicas
docker service create --name NAME_OF_THE_SERVICE -p HOST_PORT:CONTAINER_PORT --replicas NUMBER_OF_REPLICAS IMAGE_NAME
#To view the service
docker service ps SERVICE_NAME

So now, we have successfully deployed a Docker Swarm Cluster and run a service with two replicas on an Ubuntu Servers successfully.