12

I dont know if this should be posted here or on another stack community so please let me know if its wrong posting it here.

How do I get a local (i.e. on my laptop) VM that is identical to my DO droplet (Ubuntu 14.04 - LAMP etc) running?

Does DO provide a provisioner for Vagrant that can replicate the setup of a DO droplet?

It's handy being able to develop on my machine, instead of on a Droplet in the cloud.

matthew
  • 703
  • 2
  • 9
  • 24

2 Answers2

3

It should be possible, I never tried myself (as I switch to EC2) but I saw there was a digital ocean plugin, you can refer to the following page https://www.digitalocean.com/community/tutorials/how-to-use-digitalocean-as-your-provider-in-vagrant-on-an-ubuntu-12-10-vps

Basically you would need the following:

  1. install the plugin and download the base box

    vagrant plugin install vagrant-digitalocean
    vagrant box add digital_ocean https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box
    
  2. create the SSH keys needed for authentication with DigitalOcean. Run the following command to generate your SSH key pair:

    ssh-keygen -t rsa
    

    You can accept the defaults by pressing enter. This will place the SSH private and public keys to the path we will specify below in the Vagrantfile configuration.

  3. create your Vagrantfile with following minimal configuration

    config.vm.box = "digital_ocean"
    config.ssh.private_key_path = "~/.ssh/id_rsa"
    config.vm.provider :digital_ocean do |provider|
        provider.client_id = "YOUR CLIENT ID"
        provider.api_key = "YOUR API KEY"
        provider.image = "Ubuntu 12.10 x64"
        provider.region = "New York 2"
      end
    
  4. run vagrant

    vagrant up --provider=digital_ocean
    

You can refer to above link for additional parameters and fix for some issues you could face

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • 5
    Apparently, this, inherently, would bring up a DigitalOcean droplet. I am, however, and just like the OP, looking for a way to configure, provision and test a virtual environment locally, and then deploy it to a DigitalOcean droplet. I would like this process to be repeatable, e.g. I make the changes locally, then when I'm happy I would run `vagrant up` with the `vagrant-digitalocean` provider. DO support told me that they can't advise on this subject; however, the best way to re-create an environment would be by using a snapshot as a base box. But no way to download snapshot as of yet... – XedinUnknown Sep 16 '15 at 14:21
  • @XedinUnknown, right you cannot have 100% DO base box but for local development you could use a box with the same spec (OS ...) then you can have 2 providers in your Vagrantfile (one for virtual box locally and one for DO) you would need to override the box used within the virtual box provider part and once you're happy you run with DO provider – Frederic Henri Sep 16 '15 at 14:31
  • Thanks for the reply. So, is it true then that I can just run `vagrant up --provider virtualbox` on the same vagrantfile, and a functionally identical local environment will be initialized? And then I can provision and modify it, test it locally, and then `vagrant up --provider digital_ocean`? If true, have you tried it? – XedinUnknown Sep 16 '15 at 22:13
  • hi, *functionally identical local environment will be initialized running `vagrant up --provider virtualbox`*, no vagrant will initialized a vm based on a local box you would defined (would need to be on the same OS than DO) but cannot be 100% identical. I am using AWS services not DO and at first worked with vagrant, its fine as long as you just spin up machines, stop them and ssh; shared folders is too limited and network latency does not help, for AWS I have switched to opswork – Frederic Henri Sep 17 '15 at 06:13
  • Sorry, but I still don't completely understand. I want to know: initializing the box with the same OS and same Vagrantfile and everything, but with different providers will yield the same result? The environment I provision and test with the `virtualbox` provider will work on the droplet if `up`ed with the `vagrant-digitalocean` provider, and nothing else different? And if I need to propagate changes I made locally, while using the `virtualbox` provider, will I need to run with `--provision` flag when initializing with `vagrant-digitalocean`? Will that be enough? Sorry if something sounds off – XedinUnknown Sep 17 '15 at 18:28
  • Of course, that local environment would have been set up as per instructions in the docs of `vagrant-digitalocean`. – XedinUnknown Sep 17 '15 at 18:29
  • conceptually the environment would be the same but they would use different base box, and although DO build the box on the same OS I am pretty confident they made small changes (apply security fix, some specific settings) so it is not strictly the same environment. For most people it is sufficient and works well enough. The provisioning part works fine as long as you do not need to transfer files from your host to DO, the share folder mechanism is too limited (might have been improved though with recent release) to transfer large files. If you only work with distant repo it is fine. – Frederic Henri Sep 17 '15 at 19:16
  • Thanks for the reply. Now when I run `$ vagrant up --provider=virtualbox` I get the following message: `An active machine was found with a different provider. Vagrant currently allows each machine to be brought up with only a single provider at a time. A future version will remove this limitation. Until then, please destroy the existing machine to up with a new provider.` – XedinUnknown Sep 17 '15 at 19:36
  • While I'm sure it may run after I destroy the existing machine, this kinda beats the point, no? Also, what about the IP address of the local machine? How do I SSH into it? – XedinUnknown Sep 17 '15 at 19:37
  • you can trick vagrant by renaming the `.vagrant` directory in your project folder to something like `.vagrant.do` so you do not need to destroy and you can run `vagrant up --provider=virtualbox` to create your local box. note super super handy but works fine and then you switch directory depending the provider (its easy to script the renaming + correct vagrant command if you switch a lot). For the local vm created under virtual box you can override the vm property, something like `config.vm.provider "virtualbox" do |v, override| override.vm.network :private_network, ip: "192.168.45.15" end` – Frederic Henri Sep 17 '15 at 20:32
  • I believe another option would be to use Packer rather than Vagrant (both are Hashicorp products). Although I'm still working on my Packer recipe to do what you're asking, I should be able to provision both machines (using Packer in place of Vagrant) with the same Ansible playbook. I'll have an Ubuntu running on my local host inside a VirtualBox VM and the same Ubuntu server running on Digital Ocean as a Packer-generated container image. – Jim Feb 20 '16 at 22:38
3

I was hoping to run a DigitalOcean droplet in a VM recently while researching a project based on immutable server design.

I am planning on using Packer to build properly provisioned images for each of my servers. I would then utilize Vagrant to test my environment locally in VirtualBox prior to blessing the image for use in integration, stage, and production environments.

While reading the Packer - Getting Started for Vagrant Boxes tutorial I noticed this line:

If you followed along in the previous page and setup DigitalOcean, Packer can't currently make Vagrant boxes for DigitalOcean, but will be able to soon.

This appears to have been on the documentation for some time now so who knows when "soon" will be, but I haven't found any official information on this yet.

I later came across this blog post that discusses two possible ways to work around this limitation.

  1. Manually Convert a Digital Ocean Droplet to a VMware VM
  2. Settle for simply emulating a Digital Ocean environment and work from a similar base image

I read through the instructions for (1) and it does sound legitimate although it is a rather time consuming and error prone endeavor. It sounds like emulating the setup is the best bet for now.

If anyone is aware of any recent developments here please comment below.

EDIT:

I haven't tested it yet and the last update was from several years ago, but it sounds like this blog post and referenced git repo may be a good start. It still doesn't seem to be actually building the image from the DO image, but it is a pretty good example of (2) above by closely emulating.

Oddly enough the documentation for Packer's Vagrant post-processor seems to indicate that it CAN create a vagrant box from a DigitalOcean image. If this is true then a perfectly sane flow would be to use Packer to build a provisioned vagrant box from DO to test at the same time building a DO image to spin up (on integration/stage) after verification it works as advertised in VM locally. Then you can promote the DO image through the rest of your live environments.

Community
  • 1
  • 1
Matthew Sanders
  • 4,875
  • 26
  • 45
  • Regarding your comment that Packer can create a vagrant box from a DigitalOcean image, I can confirm that it certainly can ... the big issue is that vagrant box ONLY accepts as a provider 'digital-ocean', so you can't use that box to start a local image with 'virtualbox' as a provider – tvs Oct 29 '16 at 23:40