Running Vagrant in your VPS


vagrantVagrant is often used to set up development environments in a standardized way, so that your software project deployed via vagrant has a standard environment with all the operating system components and set-up it needs to work properly. It creates a virtual machine to provide this environment, so that different developers can work on it without needing to reconfigure their workstations to the requirements of the application.

If you want to use vagrant on a rimu server, you’ll have no problems with a rimuhosting dedicated server. However, if the project budget doesn’t stretch that far yet, what other options do you have? Normally you can’t create virtual machines on your ri.mu VPS, because it’s a virtual machine itself, and currently virtual machines can’t be nested inside other virtual machines. However, you can use Vagrant’s docker provider, which uses linux containers to provide the virtual machine. This works because linux containers, unlike virtual machines, will run inside the XEN virtual machines that are used for ri.mu VPSs.

In this post, I show how to set up a Debian docker container with systemd and an ssh server, so we can ssh into it and it behaves like a regular VM. (This is not the “Docker way” of doing things, but it works well with Vagrant.)

Here’s how to do it:

  1. Install Vagrant and Docker

    Use one of the following install methods, depending on what OS you are running. You’ll need to be running as root, or using sudo for the first part; later parts of the setup are done as your regular user.

    • Debian 8 (Jessie)

      First, we need to enable backports, because docker doesn’t come as part of the main distribution of Jessie

      Enable jessie backports

      echo "deb http://http.debian.net/debian jessie-backports main" > /etc/apt/sources.list.d/jessie-backports.list
      apt-get update
      

      Install vagrant and docker on your Debian 8 VPS

      apt-get install vagrant docker.io
      
    • Ubuntu 14.04 (trusty)

      First, we need get an updated version of vagrant because trusty’s version is too old and does not contain the docker provider. There is no backport available, and I had perl dependency troubles when using Ubuntu’s later version (for vivid) on trusty, so the answer is to download the installer from Vagrant’s download site.

      Download Vagrant

      Go to https://www.vagrantup.com/downloads.html Select the latest Vagrant Debian (deb) 64 bit or 32 bit as appropriate for your VPS. (If you’re not sure, you can check whether you’re on 64 bit or 32 bit with the command uname -m. x86_64 means 64 bit, and i686 means 32 bit.) At the time of writing the latest version is 1.7.4, and I’m on a 64bit machine, so the download link I want is https://releases.hashicorp.com/vagrant/1.7.4/vagrant_1.7.4_x86_64.deb. (I right click on the link and select “Copy Link Location”, then I can paste the link into the wget command below.)

      wget https://releases.hashicorp.com/vagrant/1.7.4/vagrant_1.7.4_x86_64.deb
      

      Install vagrant and docker on your Ubuntu 14.04 VPS

      dpkg -i vagrant_1.7.4_x86_64.deb # make sure the version is the same as the one you downloaded above.
      apt-get install docker.io
      
    • CentOS 7

      For CentOS, download the package supplied by Vagrant.

      Download Vagrant

      Download the appropriate package from https://www.vagrantup.com/downloads.html Select the latest Vagrant Linux (RPM) 64 bit or 32 bit as appropriate for your VPS. (If you’re not sure, you can check whether you’re on 64 bit or 32 bit with the command uname -m. x86_64 means 64 bit, and i686 means 32 bit.) At the time of writing the latest version is 1.7.4, and I’m on a 64bit machine, so the download link I want is https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.4_x86_64.rpm. (I right click on the link and select “Copy Link Location”, then I can paste the link into the wget command below.)

      wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.4_x86_64.rpm
      

      Install Vagrant

      Install with:

      rpm -i vagrant_1.7.4_x86_64.rpm
      

      Install Docker

      Install docker from CentOS’s repository with:

      yum install docker
      groupadd -r docker
      systemctl enable docker.service
      systemctl start docker.service
      
  2. Set up Docker

    The docker group which lists users allowed to run Docker. This essentially gives anyone in this group root access, so if there are multiple users on your system, only give it to users you trust. We need to make sure our user is in the group, so run the following command as root with your own username replacing “user”:

    adduser user docker # for debian and ubuntu
    

    or

    usermod -a -G docker user # for CentOS
    

    Now log out and log back in as your regular user. The rest of the instructions here don’t need root access. Let’s create a workspace:

    mkdir first-vagrant
    cd first-vagrant
    

    We need a docker image for vagrant to use. I’m going to set up a standard Debian environment, regardless of whether you have CentOS, Ubuntu or Debian installed in your VPS.
    The default debian docker image doesn’t have an ssh server installed, and vagrant needs this to operate properly. Create a file called Dockerfile with the following contents:

    FROM debian
    ENV container docker
    RUN apt-get update
    RUN rm /lib/systemd/system/getty.target.wants/getty-static.service
    CMD ["/lib/systemd/systemd"]
    RUN apt-get --no-install-recommends -y install openssh-server
    RUN apt-get --no-install-recommends -y install sudo
    RUN adduser --disabled-password --gecos "Vagrant User" vagrant ; \
    echo "vagrant:vagrant"|chpasswd ; \
    su vagrant -c "mkdir /home/vagrant/.ssh" ; \
    chmod 700 /home/vagrant/.ssh ; \
    echo "vagrant ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/vagrant
    COPY vagrant_ssh_id.pub /home/vagrant/.ssh/authorized_keys
    RUN chmod 600 /home/vagrant/.ssh/authorized_keys ; \
    chown vagrant.vagrant /home/vagrant/.ssh/authorized_keys
    EXPOSE 22
    

    This file tells docker how to build an image with sshd, starting with the standard docker supplied debian image. Get Docker’s initial Debian image with :

    docker pull debian
    
  3. Set up Vagrant

    Now create a vagrant configuration file, named Vagrantfile with the following contents:

    VAGRANTFILE_API_VERSION = "2"
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.provider "docker" do |d|
        d.build_dir = "."
        d.has_ssh = true
        d.create_args = ["--privileged"]
      end
      config.ssh.private_key_path = "vagrant_ssh_id"
    end
    

    We also need an ssh key, so that vagrant can ssh in to the Docker instance, once it’s started. Create this with:

    ssh-keygen -N "" -f vagrant_ssh_id
  4. Create and boot the new VM environment

    Now you’re ready to start the new VM. To to this type:

    vagrant up --provider=docker

    This will work away for some minutes and come back to:

    Machine booted and ready!

    You can now log in to your new machine with:

    vagrant ssh
    
  5. Next steps

    Now you have a base Debian system that you can use to develop a service, and by building it with Vagrant it can be deployed anywhere without worry that the environment is different, since vagrant isolates the host it is running on and provides a uniform environment.

    Files shared between the first-vagrant dierctory on the host, and the /vagrant directory on the VM. You can install further software or do other setup tasks using vagrant provisioning. You can make services running on the VM available with vagrant’s port forwarding.

,