Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How Do I Setup Docker In A Local Virtual Machine Using Vagrant?

Problem Statement / Requirements:

(1) I need to establish a docker environment in a VirtualBox VM for testing a project.

(2) Boot2Docker and other pre-baked docker environments are great but they do not allow me to specialize the virtual machine for my specific needs.

(3) Sometimes testing can damage the virtual machines and snapshots have a heavy performance cost.  Therefore, any solution I build needs to be reproducible through automation.

 

1 answer

1 accepted

2 votes
Answer accepted

Solution:

(1) The following Vagrantfile will create a fresh vm and network

# Docker VM Builder
# scaldwell@atlassian.com <Sam Caldwell>
#
require 'json'
curr_dir     = File.dirname(File.expand_path(__FILE__))
json         = File.read("#{curr_dir}/Vagrantfile.json")
global       = JSON.parse(json)
Vagrant.require_version global["api_require_version"]
Vagrant.configure(global["api_version"]) do |config|
	config.vm.provider :virtualbox do |vb|
  		vb.gui = global['vb_gui']
	end
	config.vm.box = global['box']
	config.vm.hostname="charlie-docker"
	config.ssh.guest_port=22
	config.vm.boot_timeout = global['boot_timeout']
	config.vm.provider :virtualbox do |v|
		v.customize ["modifyvm", :id, "--memory", global['ram']]
		v.customize ["modifyvm", :id, "--cpus", global['cpu']]
		v.customize ["modifyvm", :id, "--natdnshostresolver1", global['natdnshostresolver1']]
		v.customize ["modifyvm", :id, "--natdnsproxy1", global['natdnsproxy1']]
	end	
	docker_host="tcp://#{global['docker_host']}"
	config.vm.network "private_network", virtualbox__intnet: global['network'], ip:global['ip_addr']
	config.vm.network "forwarded_port", guest: 2376, host: 2376
	config.vm.provision "shell", inline: <<-SHELL
		#
		# Configure hostfiles to use local squid proxy.
		#
		export DEBIAN_FRONTEND=noninteractive
		sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
		sudo su -l -c "echo deb https://get.docker.com/ubuntu docker main >> /etc/apt/sources.list.d/docker.list"
		sudo apt-get update --fix-missing -y
		sudo apt-get upgrade -y
		sudo apt-get install docker.io -y
		sudo apt-get install lxc-docker-1.5.0 -y
		sudo apt-get install lxc-docker -y
		echo export DOCKER_HOST="\\"#{global['docker_host']}\\"" > ~/docker.sh
		sudo mv ~/docker.sh /etc/profile.d/docker.sh
		sudo chmod +x /etc/profile.d/docker.sh
		sudo bash -c "echo 'DOCKER_OPTS=\\"--host #{global['docker_host']}\\"'>>/etc/default/docker"
		service docker restart
	SHELL
end

(2) Note that this Vagrantfile works to abstract code from data by importing a JSON file called Vagrantfile.json.  This keeps things clean and portable.  The contents of the JSON file are--

{
	"api_version": "2",
	"api_require_version":">= 1.6.0",
    "box": "ubuntu/trusty64",
    "vb_gui": false,
    "cpu":4,
    "ram":4096,
    "docker_host":"192.168.40.2:2376",
    "ip_addr":"192.168.40.2",
    "net_addr":"192.168.40.0/24",
    "network": "private_network",
    "natdnshostresolver1":"on",
    "natdnsproxy1":"on",
    "ssh_port": "22",
    "boot_timeout": 600
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Asked in Off-topic

Inspirational Books 📚

Is there any book that inspired you and helped you better and broader perspective? Please drop their names. Would love to read 💙

79 views 3 0
View question

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you