Deploying a Docker Sandbox using Vagrant and VirtualBox

Even with the addition of more client-side tools for running Docker, it still requires installing development tools that may impact your local environment. Plus, I’m a fan of using VirtualBox to make sandbox environments that I can spin up and tear down as needed. This means that I never have to worry about version conflicts with development languages and dealing with package management on my Macbook or on my Windows machine. Every machine will use the same recipe.

Next, change directory into the folder cd virtualbox-docker-sandbox and then run the vagrant up command:

That’s going to run for a while, and you will see after about 10 minutes that you’re back at the prompt. Now we can use vagrant ssh dockersandbox to get onto the console and confirm our environment:

You’ve got the nested instance running, and as you may have read in the build.sh file from the code we cloned, we have installed docker as well as docker-compose and docker-machine which will help us test the waters on a few different Docker features:

Now you can start up some Docker containers without having touched your local machine at all, and this ensures a clean sandbox deployment for you that is able to be quiesced using the vagrant suspend dockersandbox command:

To bring the environment back up, just run a vagrant resume dockersandbox

Running Docker as Sudo

One thing to note is that we have deployed the Docker runtime as sudo, and because of that we must always run the Docker commands as sudo. This is a best practice in general, so when you try to run docker pull or any docker commands, you will see errors about the daemon not being available:

Run the sudo docker pull nginx instead of docker pull nginx and you will see a marked difference in the results: