Manage Docker Swarm with Portainer – The New Stack


That docker swarm Container Orchestration Engine is a great way to leverage a cluster for your container deployments. While it may not be nearly as popular as Kubernetes, it’s still a great option if you’re looking for features like:

  • Cluster management integrated into Docker Engine
  • Decentralized design
  • A declarative service model
  • Scale container deployments
  • Desired state reconciliation
  • multi-host network
  • service detection
  • load balancing
  • Secure Deployments
  • Ongoing updates

And given how much easier Docker Swarm is to use (than Kubernetes), it’s a great way to introduce containers into your development lifecycle mix.

But while Docker Swarm is pretty easy to manage, there’s an even easier way… thanks to the Portainer container management system. Once you have your Docker Swarm up and running, you can deploy Portainer and it will automatically pick up your controller and all your nodes.

I want to walk you through the process of deploying Docker Swarm and adding Portainer into the mix. Once you have this up and running, you’ll find it exponentially easier to manage your clustered Docker servers and the containers/services you deploy.

However, the first thing we need to do is spin up a Docker Swarm. I will demonstrate this with three instances of Ubuntu Server 20.04. You can deploy this setup to any Linux computer, but you’ll need to adapt the installation process to the distribution of your choice.

Let’s get to work.

Deploy Docker Swarm

Login to your first instance of Ubuntu and install the required dependencies with the command:

sudo apt-get install ca-certificates curl gnupg lsb-release -y

After the installation is complete, add the official Docker gpg key with the following command:

curl -fsSL | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, we can add the stable Docker repository with the command:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update apt with the command:

sudo apt-get update

Install Docker Engine (Community Edition) with:

sudo apt-get install docker-ce docker-ce-cli -y

When the installation is complete, you should start and enable the Docker service with:

sudo systemctl enable --now docker

In order to be able to exhibit docker Command without sudo privileges (which can be a security issue), add your user to the docker group with:

sudo usermod -aG docker $USER

Log out and back in for the changes to take effect (or just issue the command newgrp docker and you’re good to go).

It is important that you then repeat the above steps on each Docker node that you want to join the swarm.

Initialize the swarm and connect nodes

Go back to the controller (the first computer you installed Docker on) and find that computer’s IP address with:

ip a

You can now initialize the swarm with the command:

docker swarm init --advertise-addr SERVER

Where SERVER is the IP address for the Docker controller.

When this command completes, it will issue a join command that looks like this:

docker swarm join --token TOKEN

Where TOKEN is a long string of random characters. Copy this command and run it on each node that you want to join the swarm. When you have joined all nodes, go back to the controller and enter the command:

docker info

You should see a line in the output that looks like this:

Nodes: 3

All three nodes have successfully joined the swarm.

provide portainer

The next trick is to mount portainer (run 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

Give the container a moment to boot up, then point a web browser to http://SERVER:9443 (where SERVER is the IP address of the controller). Create an admin user and login. Because Portainer was deployed to a controller in a Swarm, you will see an entry for Swarm in the left sidebar (illustration 1).

Figure 1: Portainer adds a Swarm entry when it’s part of a cluster.

Now the fun begins.

Let’s provide a service for our crush. Click Services in the left sidebar, then click Add Service. In the resulting window (figure 2), you should see a replica entry.

replica entry

Figure 2: Creating a new service via Portainer.

Since we are working with a swarm that has a total of three nodes, we can increase the replica entry to three, so that the service we provide is replicated to every node in the swarm (for high availability and failover).

Let’s provide an NGINX service for our swarm. Give the service a name, and then enter nginx:latest in the image field. Expand Replicas to three and click Create Service (figure 3).

Providing a service for Docker Swarm

Figure 3: A very simple service to deploy to our Docker Swarm.

Once the service has been successfully deployed to the swarm, it will appear in the service list (figure 4).

node replication

Figure 4: Our tnsDockerTest service has replicated to all nodes in the swarm.

Another cool trick you can do about Portainer is to scale the service up and down. When you click the Scale button in the list, the field changes (Figure 5) so you can scale the service up or down as needed.

Scaling a service with Portainer.

Figure 5: Scaling up or down a service is very easy with Portainer.

And that’s how easy it is to manage your Docker Swarm with the help of Portainer. If you intend to work with Docker Swarm for your business, I highly recommend trying this easy-to-use, web-based GUI. You will find that your work becomes more productive, efficient and reliable.

The New Stack is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker.


Comments are closed.