Created
November 1, 2015 13:16
-
-
Save encodeering/c8befcc58abe7de620df to your computer and use it in GitHub Desktop.
How to setup a dockerized vm using Vagrant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
# | |
# Copyright 2015 Michael Clausen | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
# http://www.encodeering.com | |
# | |
# Configuration | |
name = "encodeering" | |
image = "debian/jessie64" | |
cpu = 2 | |
memory = 2048 | |
ip = "192.168.33.11" | |
port = 2375 | |
dockeroption = "-H=tcp://#{ip}:#{port} --bip 172.17.42.1/24" | |
forwardings = { | |
"8080/tcp" => "80", | |
"8080/udp" => { :to => "80" } | |
} | |
sharings = { | |
"." => { :to => "/vagrant", :disabled => true } | |
} | |
warning = "#auto-generated file" | |
# Vagrant | |
Vagrant.configure(2) do |config| | |
# Box | |
config.ssh.insert_key = false | |
config.vm.provider "virtualbox" do |vb| | |
vb.gui = false | |
vb.name = "#{normalize (name)}-#{normalize (image)}" | |
vb.cpus = "#{cpu}" | |
vb.memory = "#{memory}" | |
vb.customize ["modifyvm", :id, "--cpuexecutioncap", "75"] | |
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] | |
end | |
# VM | |
config.vm.define name do |docker| | |
docker.vm.box = image | |
docker.vm.hostname = normalize (name) | |
# Network | |
forwardings.each_pair { |h, g| | |
args = g = hashify g.clone, :to | |
to = args.delete (:to) | |
# e.g localhost:8080, 192.168.42.42:8080/tcp, 8080/udp, 8080, ... | |
net = /^((?<ip>[^:]+):)?(?<port>\d+)(\/(?<protocol>\w+))?$/ | |
host = net.match (h); | |
guest = net.match (to); | |
args[:host_ip] = host[:ip] | |
args[:host] = host[:port] | |
args[:protocol] = host[:protocol] | |
args[:guest_ip] = guest[:ip] | |
args[:guest] = guest[:port] | |
docker.vm.network "forwarded_port", args | |
} | |
docker.vm.network "private_network", ip: ip | |
# Folder | |
sharings.each_pair { |h, g| | |
args = g = hashify g.clone, :to | |
to = args.delete (:to) | |
config.vm.synced_folder h, to, args | |
} | |
# Docker | |
docker.vm.provision "shell", inline: <<-SHELL | |
sudo curl -sSL https://get.docker.com/ | sh | |
sudo gpasswd -a vagrant docker | |
sudo echo "#{warning}" > /etc/docker/config | |
sudo echo "OPTIONS=#{dockeroption}" >> /etc/docker/config | |
sudo cp -f /lib/systemd/system/docker.service /etc/systemd/system/docker.service | |
sudo sed -i '1s/^/#{warning}\\n/' /etc/systemd/system/docker.service | |
sudo sed -i '/ExecStart/i\EnvironmentFile=-/etc/docker/config' /etc/systemd/system/docker.service | |
sudo sed -i '/ExecStart/ s/$/ $OPTIONS/' /etc/systemd/system/docker.service | |
sudo systemctl reenable docker | |
sudo systemctl restart docker | |
SHELL | |
end | |
end | |
def normalize (text) | |
return text.gsub(/[^0-9A-Za-z]/, '-') | |
end | |
def hashify (any, symbol) | |
case any | |
when Hash then return any | |
else return { symbol => any } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment