Step by step guide on how to set up Docker Swarm using VSCode.

Alyson Coppola
7 min readSep 10, 2023

--

What is a Docker Swarm? Docker Swarm is a clustering and scheduling tool for Docker containers. With Swarm, IT administrators and developers can establish and manage a cluster of Docker nodes as a single virtual system. Swarm mode also exists natively for Docker Engine, the layer between the OS and container images.

What is a Docker Swarm Node? Nodes in a Docker Swarm communicate with each other to maintain the cluster's state and ensure that containers are deployed and managed according to the desired configurations. This allows you to scale your applications horizontally by adding more worker nodes and ensures that your services remain available even if some nodes fail.

Manager Node: Manager nodes are responsible for managing the overall swarm cluster. They control the orchestration and scheduling of services and tasks. Manager nodes store the cluster’s state and configuration and can perform tasks such as service creation and scaling.

Worker Node: Worker nodes are responsible for running the containers that make up your applications. They receive and execute tasks (containers) assigned by the manager nodes. Worker nodes do not participate in the management of the swarm cluster but focus on running containers efficiently.

To start we need to launch an instance.

Go to AWS, EC2 and launch an instance. Name the instance, choose Ubuntu for the AMI and t2.micro.

Now you need to create a key pair or choose one you already have.

Next you need to create a security group or choose one you already have.

Next you will go to advanced details and click the drop arrow, then scroll down to User data and enter the following to install docker.

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Now you will go to the right side of the screen where it says Summary. You will change the Number of instances to 3. Then click on “Launch instance”.

You will see a green banner at the top of your screen that says success.

Now we need to change the names of the 3 instances we just launched. Go back to instances and you will see 3 running that all have the same name.

Click on the name to edit it to what you want, then click save. Do this for all three.

Lets confirm that we can ssh into the terminal on our computer to make sure we don’t have any issues. Go to the NODE1 instance (this will be the “manger node”) go to the ssh client, copy the chmod command and paste in the terminal on your computer and hit enter. Then go back to the instance and copy the ssh and paste that into your terminal and hit enter. Type “yes” when it asks. You should then see Ubuntu on the terminal. This confirms that so far everything is set up correct.

Now we need to verify that Docker is active.

First you will need to go to Visual Studio code, open the terminal and ssh. You will need copy the set of information on the top of the config screen and paste it in the section below it. Do that one more time to show a total of 3. Then you will need to update all three sections to match your information from the AWS instance for each node.

  • Update the Host.
  • Update the HostName with the IP address. I used the public IP address.
  • Update the IdentityFile with the correct key pair.

Once all 3 nodes are reflective of the information in AWS instances, save the information. Hit command S.

Now we need to work in the terminal of VS code and ssh in. But first, lets split the terminal into 3 sections because we have 3 nodes. You will then see 3 sections that all say the home of your terminal.

Now we need to ssh into each node.

For the first box of the terminal, we are working with the “manager node”. This is the first node you created (My first node is NODE1). You will run the following command.

ssh <name>

Now you will go to the second terminal and we will ssh into the second node you created (My second node is NODE2). You will run the following command.

ssh <name>

It will then ask you if you want to continue, type “yes” then hit enter.

Now you will go to the third terminal and we will ssh into the third node you created (My third node is NODE3). You will run the following command.

ssh <name>

It will then ask you if you want to continue, type “yes” then hit enter.

After you ssh into all three nodes your terminal should look like this:

You can run the following command to change the host name in the terminal to reflect the node names. You will want to do this for the second and third node.

sudo hostnamectl set-hostname <name>

After you run that command, you will want to run this command next.

exit

Once you hit enter after you run the previous command, you will need to ssh back in to then reflect the node name. You will need to do this for each node.

ssh <name>

Your terminal will then look like this:

Now lets check for updates. Run the following command for each node.

sudo apt update

Next we will create the Swarm.

In the first terminal (NODE1) run the following command.

sudo docker swarm init

You will then see the following response:

Now you will need to copy the docker swarm join token and paste that into the second terminal (NODE2) and run the following command.

sudo docker swarm join --token <paste token here>

Repeat this for the third terminal (NODE3). Your screen should look like this after.

View status of Docker Nodes

Go to the first terminal (NODE1) and run the following command.

sudo docker node ls

This shows you all three nodes, there status and that NODE1 is the manager.

Now run the following command under the first terminal (NODE1) to leave your swarm.

sudo docker swarm leave --force

Repeat that for the other nodes.

Next run the following command to confirm you have left the swarm

sudo docker service ls

Repeat that for the other nodes.

Now we need to exit the ubuntu in the terminal, run the following command. It will take you back to your name in your terminal.

exit

Repeat this for the other two terminals.

Lastly, go back to AWS, instance and stop the three node instances that are running.

You are now done. Great Job!

--

--