Docker Swarm Cluster Setup With Vagrant

Gaurav Talele
3 min readDec 3, 2019


GOAL: in this article we will start two node Docker Swarm Cluster and go over commands to manage multiple nodes. We will put a detailed glance on topics 1) starting the Swarm 2) viewing Swarm status 3) joining the Swarm 4) obtaining join-tokens 5) Run Containers with replication 6) Response to fail-over

System Requirements

Considering we will do this LAB on Windows System. On this System we will Install Chocolatey + Vagrant + Install Virtualbox.

Open PowerShell as a Administrator:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString(''))

Restart The Powershell with administrator privileges.

choco install virtualbox vagrant --yes
mkdir DockerSwarmCluster && cd DockerSwarmCluster

Open Command Prompt and navigate to DockerSwarmCluster directory.

create a new file named Vagrantfile. and paste following code.

$install_docker_script = <<SCRIPT
echo Installing Docker...
curl -sSL | sh
sudo usermod -aG docker ubuntu
$manager_script = <<SCRIPT
echo Swarm Init...
sudo docker swarm init --listen-addr --advertise-addr
sudo docker swarm join-token --quiet worker > /vagrant/worker_token
$worker_script = <<SCRIPT
echo Swarm Join...
sudo docker swarm join --token $(cat /vagrant/worker_token)
Vagrant.configure('2') do |config|vm_box = 'ubuntu/xenial64'config.vm.define :manager, primary: true do |manager| = vm_box
manager.vm.box_check_update = true :private_network, ip: "" :forwarded_port, guest: 8080, host: 8080 :forwarded_port, guest: 5000, host: 5000
manager.vm.hostname = "manager"
manager.vm.synced_folder ".", "/vagrant"
manager.vm.provision "shell", inline: $install_docker_script, privileged: true
manager.vm.provision "shell", inline: $manager_script, privileged: true
manager.vm.provider "virtualbox" do |vb| = "manager"
vb.memory = "1024"
(1..2).each do |i|
config.vm.define "worker0#{i}" do |worker| = vm_box
worker.vm.box_check_update = true :private_network, ip: "{i}"
worker.vm.hostname = "worker0#{i}"
worker.vm.synced_folder ".", "/vagrant"
worker.vm.provision "shell", inline: $install_docker_script, privileged: true
worker.vm.provision "shell", inline: $worker_script, privileged: true
worker.vm.provider "virtualbox" do |vb| = "worker0#{i}"
vb.memory = "1024"

About The Vagrantfile:

This makes two different virtualboxes, one named: manager, another named: worker01 and worker02.

With the way the Vagrantfile configuration, connects each virtualbox to a private network with IP Addresses (manager), (worker01) and (worker02).

All actions taken on manager, worker01,worker02 are equivalent to working on separate computers.

vagrant up

Now this will fetch the ubuntu/xenial64 box from vagrant cloud and make the necessary configuration including installation of docker engine and docker compose.

Once the process exited.

Command Prompt 1 > vagrant ssh manager
Command Prompt 2 > vagrant ssh worker01
Command Prompt 3 > vagrant ssh worker02

Create a service At Manager Node

docker service create --name my_web 
--replicas 7
--publish published=8080,target=80

now, See all the docker containers from the nodes.

vagrant@manager  > docker container ls
vagrant@worker01 > docker container ls
vagrant@worker02 > docker container ls

Why Should we use Vagrant To Setup Docker Swarm

Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the “works on my machine” excuse a relic of the past. Vagrant is convenient to share virtual environment setup and configurations.

Vagrant allows us very easily to share setups between team members allowing very easy spin up of a work environment.


Vagrant provides an easy way to define and share a different application or environment setup in a single text file called Vagrantfile. Vagrant uses virtualization engines like VirtualBox, VMWare or Hyper-V and builds on top of them



Gaurav Talele

An ambitious Full Stack evangelist in Angular, Typescript, Spring Boot Node JS, C#, Dot Net Core WEB API, MS SQL, Redis, MongoDB, RabbitMQ, Docker and AWS.