Cloud Native Ecosystem / Containers

Tutorial: Manage Docker Swarm with Portainer

23 Apr 2022 7:00am, by

The Docker Swarm container orchestration engine is a great way to take advantage of a cluster for your container deployments. Although it might not be nearly as popular as Kubernetes, it’s still a great option if you’re looking for features like:

  • Cluster management integrated with Docker Engine
  • Decentralized design
  • A declarative service model
  • Scaling container deployments
  • Desired state reconciliation
  • Multi-host networking
  • Service discovery
  • Load balancing
  • Secure deployments
  • Rolling 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 even though Docker Swarm is quite easy to manage, there’s an even easier way… thanks to 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 of 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’ve deployed.

The first thing we need to do, however, is spin up a Docker Swarm. I’ll be demonstrating this with three instances of Ubuntu Server 20.04. You can deploy this setup on any Linux machine, but you’ll need to modify the installation process to fit your distribution of choice.

Let’s get down to work.

Deploying Docker Swarm

Log into your first instance of Ubuntu and install the necessary dependencies with the command:

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

Once that installation completes, add the official Docker GPG key with the 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 completes, you’ll want to start and enable the Docker service with:

sudo systemctl enable --now docker

To be able to issue the 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 log back in for the changes to take effect (or just issue the command newgrp docker and you’re good to go).

It’s important that you then repeat the above steps on every Docker node you plan to join to the Swarm.

Initializing the Swarm and Joining Nodes

Go back to the controller (the first machine you installed Docker on) and discover the IP address of that machine 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 that command completes, it’ll print out a join command that looks like this:

docker swarm join --token TOKEN

Where TOKEN is a long string of random characters. Copy that command and run it on every node you want to join the swarm. Once you’ve joined all the nodes, go back to the Controller and issue the command:

docker info

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

Nodes: 3

All three nodes have successfully joined the Swarm.

Deploying Portainer

The next trick is to deploy Portainer with the command (run on the controller):

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 spin up and then point a web browser to http://SERVER:9443 (Where SERVER is the IP address of the Controller). Create an admin user and log in. Because Portainer has been deployed to a controller in a Swarm, you’ll see an entry for Swarm in the left sidebar (Figure 1).

: Portainer adds a Swarm entry when it's part of a cluster.

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

Now the fun begins.

Let’s deploy a service to our Swarm. Click Services in the left sidebar and then click Add service. In the resulting window (Figure 2), you should see a Replicas entry.

Replica entry

Figure 2: Creating a new service via Portainer.

Because we’re working with a Swarm that involves three total nodes, we can bump the replicas entry up to three, so the service we’re deploying will replicate to every node on the swarm (for high availability and failover).

Let’s deploy an NGINX service to our Swarm. Give the service a name and then type nginx:latest in the Image field. Bump Replicas up to three and click Create the service (Figure 3).

Deploying a service to Docker Swarm

Figure 3: A very basic service to be deployed to our Docker Swarm.

Once the service has successfully deployed to the Swarm, it’ll be listed 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 via Portainer is to scale the service up and down. If you click the Scale button in the listing, the field will change (Figure 5) such that you can scale the service up or down as needed.

Scaling a service with Portainer.

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

And that’s how easy it is to manage your Docker Swarm with the help of Portainer. If you have any intention of working with Docker Swarm for your business, I highly recommend giving this user-friendly, web-based GUI a try. You’ll find your work to be 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.