Skip to content

Instantly share code, notes, and snippets.

@AlexJakeGreen

AlexJakeGreen/Vagrantfile

Last active Jul 30, 2017
Embed
What would you like to do?
Vagrantfile for Docker Swarm
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure('2') do |config|
config.vm.box = 'centos7_1706.02_with_guest_additions'
config.vm.provider :virtualbox do |vb|
vb.memory = '512'
vb.cpus = 1
end
install_docker_provision = <<-SHELL
test -f /etc/yum.repos.d/docker-ce.repo || ( \
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo; \
yum-config-manager --enable docker-ce-edge; \
yum makecache fast \
)
rpm -aq | grep -q docker || ( \
yum install -y docker-ce; \
perl -i -pe 's;^ExecStart=/usr/bin/dockerd$;ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock;' /lib/systemd/system/docker.service; \
systemctl enable docker; \
systemctl start docker \
)
groups vagrant | grep -q docker || ( \
gpasswd -a vagrant docker
)
test -f /etc/sysconfig/docker || \
echo "OPTIONS=--selinux-enabled -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375" > /etc/sysconfig/docker
SHELL
init_swarm_provision = <<-SHELL
docker swarm init --advertise-addr 10.10.10.10
docker swarm join-token --quiet worker > /vagrant/worker_token
SHELL
enter_swarm_provision = <<-SHELL
docker swarm join --token $(cat /vagrant/worker_token) 10.10.10.10
SHELL
config.vm.define :manager, primary: true do |m|
m.vm.network :private_network, ip: "10.10.10.10"
m.vm.hostname = 'manager'
m.vm.provision :shell, inline: install_docker_provision
m.vm.provision :shell, inline: init_swarm_provision
end
(1..2).each do |i|
config.vm.define "worker#{i}" do |m|
m.vm.network :private_network, ip: "10.10.10.2#{i}"
m.vm.hostname = "worker#{i}"
m.vm.provision :shell, inline: install_docker_provision
m.vm.provision :shell, inline: enter_swarm_provision
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment