How to manage a Docker Swarm Cluster

How to manage a Docker Swarm Cluster

Service is the definition of the tasks to execute on the worker nodes. It is the central structure of the swarm system and the primary root of user interaction with the swarm. When you create a service, you specify which container image to use and which commands to execute inside running containers.

Service is the definition of the tasks to execute on the worker nodes. It is the central structure of the swarm system and the primary root of user interaction with the swarm. When you create a service, you specify which container image to use and which commands to execute inside running containers.

In this article, I am listing the scripts to install Docker and setup Docker Swarm commands. Some of the commands may help you manage your Docker Swarm nodes, services and containers.

Docker-CE Installation

On Ubuntu 16.04

Using these commands, you can install Docker CE on Ubuntu 16.04.

# Install docker-ce supporting packages
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

# Add Docker’s official GPG key
curl -fsSL | sudo apt-key add -

# Verify that you now have the key with the fingerprint
sudo apt-key fingerprint 0EBFCD88

#Use the following command to set up the stable repository
sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"

sudo apt-get update

# List the versions available in your repo
apt-cache madison docker-ce

# Install a specific version using the version string from the second column, for example, 
sudo apt-get install docker-ce=18.03.0~ce-0~ubuntu

# Verify that Docker CE is installed correctly by running the hello-world image.
sudo docker run hello-world

On RHEL/CentOS 7

These commands will help you to install Docker CE on RHEL/CentOS 7.

# Install updates
sudo yum update

# Remove old or existing docker packages
sudo yum remove docker docker-common docker-selinux docker-engine-selinux docker-engine docker-ce -y

# Install dependencies and utilities
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 -y

# If you get below error, execute the next command. it installs latest container-selinux.
# You can also change url to latest version
# Error: Package: docker-ce-xx.xx.xx.xx.x86_64 (docker-ce-stable)
#           Requires: container-selinux >= 2.9
sudo yum install -y

# Add docker repositories to the yum repositories.
sudo yum-config-manager --add-repo -y

# Now you are ready to install docker-ce
sudo yum install docker-ce -y

# After successful installation, you need to enable and start docker service.
sudo systemctl enable docker.service
sudo systemctl start docker

Manage Docker Swarm

sudo docker swarm init

Swarm initialized: current node (a35hhzdzf4g95w0op85tqlow1) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join \
--token SWMTKN-1-3k7ighcfs9352hmdfzh31t297fd8tdskg6x6oi8kpzzszznffx-6kovxm3akca2qe3uaxtu07fj3

Now you are ready to set up your Docker Swarm cluster.

Some of the most useful commands are given below. The purpose of each command is described in the comments.

# Run either of the below commands on Master node to get Worker or Manager token
sudo docker swarm join-token manager # return a Token to join as a master
sudo docker swarm join-token worker # return a Token to join as a worker

# To create slave nodes After running above command, you will get an output something like this
sudo docker swarm join --token SWMTKN-1-4fpn76698rvexdvfftnt37rx9hqwwwadeuup05zxxuqf2ihfcw-bg44jj84tmpclw4me1yr0oqep

# To check list of nodes
sudo docker node ls

# To leave a swarm mode
sudo docker swarm leave

#To create a docker service e.g. Nginx
sudo docker service create -p 8080:80 --name webserver nginx

#To list services
sudo docker service ls
sudo docker service rm 

#Deploying image from docker hub to docker container
sudo docker run -d -p 80:8761 --name eureka-server arunsharma/eureka-server

#Deploying same image on docker swarm
sudo docker service create -p 80:8761 --name eureka-server arunsharma/eureka-server

#Promote or demote a node
sudo docker node demote node-3 node-2
sudo docker node promote node-3 node-2

Finally, here's how to create a Portainer UI container to monitor the docker swarm cluster from a user interface.

#Create a volume for Portainer data. 
sudo docker volume create portainer_volume

# Creating and running Portainer as a service on Docker Swarm
sudo docker service create --name portainer --publish 9000:9000 --replicas=1 --constraint 'node.role == manager' --mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock --mount type=volume,src=portainer_volume,dst=/data portainer/portainer -H unix:///var/run/docker.sock

Learn More

Building Scaling Machine Learning Models With Docker Swarm


Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Docker Explained: Docker Architecture | Docker Registries

Following the second video about Docker basics, in this video, I explain Docker architecture and explain the different building blocks of the docker engine; docker client, API, Docker Daemon. I also explain what a docker registry is and I finish the video with a demo explaining and illustrating how to use Docker hub.

Docker Tutorial for Beginners 8 - Build and Run C++ Applications in a Docker Container

Welcome to this on Docker Tutorial for Beginners. In this video provides an Introduction on C++ development with Docker containers. So we will see How to ship C++ Programs in Docker.

WordPress in Docker. Part 1: Dockerization

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

Docker manifest - A peek into image's manifest.json files

The docker manifest command does not work independently to perform any action. In order to work with the docker manifest or manifest list, we use sub-commands along with it. This manifest sub-command can enable us to interact with the image manifests. Furthermore, it also gives information about the OS and the architecture, that a particular image was built for. The image manifest provides a configuration and a set of layers for a container image. This is an experimenta

List all containers in Docker(Docker command)

We can get a list of all containers in docker using `docker container list` or `docker ps` commands.