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

Sam Caldwell Atlassian Team Mar 22, 2015

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
Accepted answer
Sam Caldwell Atlassian Team Mar 22, 2015


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

# Docker VM Builder
# <Sam Caldwell>
require 'json'
curr_dir     = File.dirname(File.expand_path(__FILE__))
json         ="#{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 = global['box']
	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']]
	docker_host="tcp://#{global['docker_host']}" "private_network", virtualbox__intnet: global['network'], ip:global['ip_addr'] "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:// --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
		sudo su -l -c "echo deb 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 -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']}\\"" > ~/
		sudo mv ~/ /etc/profile.d/
		sudo chmod +x /etc/profile.d/
		sudo bash -c "echo 'DOCKER_OPTS=\\"--host #{global['docker_host']}\\"'>>/etc/default/docker"
		service docker restart

(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,
    "network": "private_network",
    "ssh_port": "22",
    "boot_timeout": 600

Suggest an answer

Log in or Sign up to answer
Community showcase

Scrum Roles Explained: the Do's and the Don'ts

Hello Community,  Today we are going to talk about the three Scrum Roles. There is the Development Team, the Scrum Master and the Product Owner. In my opinion these three are all really impo...

46 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you