Rapidly spinning up a VM with Ubuntu and Docker–on my Windows machine using Vagrant and VirtualBox image 2

Rapidly spinning up a VM with Ubuntu and Docker–on my Windows machine using Vagrant and VirtualBox

imageI have a Windows laptop. And of course I want to work with Docker containers. Using the Docker Quickstart Terminal is one way of doing so, and to some extent that works fine. But whenever I want to have more control over the Linux environment that runs the Docker host, or I want to run multiple such envionments in parallel, I like to just run VMs under my own control and use them to run Docker inside.

The easiest way for me to create and run Docker enabled VMs is using combination of Vagrant and VirtualBox. VirtualBox runs the VM and takes care of network from and to the VM as well as mapping local directories on the Windows host machine into the VM. Vagrant runs on the Windows machine as a command line tool. It interacts with the VirtualBox APIs, to create, start, pause and resume and stop the VMs. Based on simple declarative definitions – text files – it will configure the VM and take care of it.

In this article, I share the very simple Vagrant script that I am using to spin up and manage VMs in which I run Docker containers. Vagrant takes care of installing Docker into the VM, of configuring the Network, for mapping a local host directory into the VM and for creating a larger-than-normal disk for the Ubuntu VM. I will briefly show how to create/start the VM, SSH into it to create a terminal session, run a Docker file from the Windows host to run a container and to halt and restart.

The prerequisites for following along: have a recent version of Vagrant and VirtualBox installed.

To create and run the VM, write the following Vagrantfile to a directory: https://gist.github.com/lucasjellema/7593677f6d03285236c8f0391f1a78c2 , for example using

git clone https://gist.github.com/7593677f6d03285236c8f0391f1a78c2.git

Open a Windows command line and cd to that directory. image

Then type

vagrant up

This will run Vagrant and have it process the local vagrantfile. The base VM image is downloaded – if it does not already exist on your Windows host and subsequently Vagrant engages with VirtualBox to create the VM according to the configuration settings.

image

When the VM is running, Vagrant will do the next processing steps: provisioning Docker and Docker Compose in the VM.

SNAGHTMLd51f82Finally, if there is a docker-compose.yml file in the current directory, it will be run by docker compose inside the VM; if there is none, an ugly error message is shown – but the VM will still be created and end up running.

 

When vagrant up is complete, the VM is running, Docker is running and if any containers were created and started by Docker Compose, then they will be running as well.

Using

vagrant ssh

(from the command line interface and still from the same directory) we can create a terminal session into the VM.

SNAGHTML3ed88f

Using

docker ps

we can check if any containers were started. And we can start a(nother) container if we feel like it, such as:

docker run busybox echo “hello from busybox”

image

The directory on the Windows host from which we ran vagrant up and vagrant ssh is mapped into the vm, to /vagrant. Using

ls /vagrant

we can check on the files in that directory that are available from within the VM.

 

We can for example build a Docker container from a Docker file in that directory.

Using

exit

we can leave the Vagrant SSH session. The VM keeps on running. We can return into the VM using vagrant ssh again. We can have multiple sessions into the same VM – by just starting multiple command line sessions in Windows, navigating to the same directory and running vagrant ssh in each session.

Using

vagrant halt

we stop the VM. Its state is saved and we can continue from that state at a later point, simply by running vagrant up again.

With vagrant pause and vagrant resume we can create a snapshot of the VM in mid flight and at a later moment (which can be after a restart of the host system) continue where we left off.

Using

vagrant destroy

you can completely remove the VM, releasing the host (disk) resources that were consumed by it.

image

Resources

Vagrant Documentation: https://www.vagrantup.com/docs/

Download Vagrant: https://www.vagrantup.com/downloads.html

Download Oracle VirtualBox: https://www.virtualbox.org/wiki/Downloads

3 Comments

  1. Andrew December 4, 2019
  2. jan4711 June 7, 2019
  3. Jose Cardenas August 1, 2018