Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Created April 18, 2016 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p120ph37/0e2dc718c9ea3400da0590792eac208f to your computer and use it in GitHub Desktop.
Save p120ph37/0e2dc718c9ea3400da0590792eac208f to your computer and use it in GitHub Desktop.
Use Vagrant's "docker-provider" to run a Dockerfile on any host. Works around issues with the current hashicorp/boot2docker image.
Vagrant.configure(2) do |config|
# Generate shared docker-host Vagrantfile
# (the default hashicorp/boot2docker image doesn't work very well at the moment)
env = Vagrant::Environment.new
vf_path = env.data_dir.join("docker-virtualbox-host", "Vagrantfile")
begin
env.lock("docker-provider-virtualbox-hostvm") do
vf_path.dirname.mkpath
File.open(vf_path, 'w') do |file|
file.write <<-EOF
Vagrant.configure(2) do |config|
config.vm.box = "dduportal/boot2docker"
# make sure Docker starts before SSH
config.vm.provision "shell", inline: <<-SCRIPT
# make sure Docker starts before SSH
sed -i \
-e '/Launch Docker/,$!{/Configure SSHD/,/^$/{H;d;};}' \
-e '/Launch Docker/,/^$/{/^$/g;}' \
/opt/bootscript.sh
# and make sure it is started now
/etc/rc.d/docker
SCRIPT
end
EOF
end
end
rescue Vagrant::Errors::EnvironmentLockedError
# Lock contention, just retry
retry
end
# Spawn a Docker container, natively if possible, or with the help of a VM.
config.vm.define :default do |box_config|
box_config.vm.provider :docker do |d|
d.build_dir = "."
d.vagrant_vagrantfile = vf_path
d.remains_running = false
end
end
end
@p120ph37
Copy link
Author

There are three issues this works around:

  1. The current hashicorp/boot2docker image has password-based authentication, which does't play very nicely with Vagrant, and it lacks VirtualBox filesystem sharing support. The fix for this was to use @dduportal's fork of boot2docker, which includes key-based authentication and the VirtualBox extensions.
  2. The dduportal/boot2docker image sometimes has the Docker daemon start a few seconds after SSH comes online, so Vagrant jumps the gun and produces an error. The fix was to re-arrange the boot scripts in the image to start SSH after Docker.
  3. There is no good way to specify a custom shared docker-host VM in a Vagrantfile, so I had to do a re-implement part of the code from the provider-docker plugin directly in the Vagrantfile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment