Skip to content

Instantly share code, notes, and snippets.

@mattbell87
Last active July 9, 2021 01:11
Show Gist options
  • Save mattbell87/ccacccd220249a4f8917dfc04e3b2a89 to your computer and use it in GitHub Desktop.
Save mattbell87/ccacccd220249a4f8917dfc04e3b2a89 to your computer and use it in GitHub Desktop.
Set up a Vagrant VM for docker-based development

Why use this method?

Use this method when:

  • Docker Desktop doesnt work on your system, or you don't prefer to use it
  • You prefer to keep your development in a VM

Install Vagrant and VirtualBox

Vagrant is a VM (Virtual Machine) management tool that runs on Mac, Windows and Linux. By installing this you'll be up and running much faster.

VirtualBox is the VM platform that Vagrant uses.

Install these on your PC/Mac:

Set up the VM

Open a terminal and run:

vagrant init ubuntu/focal64
vagrant up

Wait a moment for Ubuntu 20.04 to be installed then run:

vagrant ssh

If all went well you'll be logged in to your vagrant VM.

Now lets install Docker here:

sudo snap install docker

Lastly you'll want to make sure the "vagrant" user can run docker commands. Use these commands:

sudo addgroup docker
sudo adduser vagrant docker

Then apply everything by rebooting the VM:

exit
vagrant reload

After that's complete you can connect again with:

vagrant ssh

Once you're back in the vagrant@ubuntu-focal prompt you can run this to test if Docker is working:

docker run hello-world

You can exit from the VM now with:

exit

Connect your VM to Visual Studio Code

First you'll need to set up a normal SSH connection to the VM. Run this to set up the SSH connection:

vagrant ssh-config --host VagrantVM >> ~/.ssh/config

Now install the Remote SSH extension in VSCode if it's not installed yet.

After it installs click the Remote Explorer button, right click VagrantVM and connect to it (you may need to change the view to SSH Targets if you don't see VagrantVM).

Visual Studio Code will now connect to the VM and do some initial setup.

At this stage you can now use Visual Studio Code to perform all of your tasks in the VM.

Install the Docker extension

Lastly you should install the Docker extension in Visual Studio Code.

If all goes well under "Containers" you'll see the "hello-world" container from earlier. If you like you can right click this and select "Remove".

How to use it on a daily basis

After a reboot all you need to do is open a terminal and run:

vagrant up

You can now start Visual Studio Code and open your projects inside the Vagrant VM.

Tips:

Visual Studio Code:

  • Open a terminal in VSCode with Control+`
  • Create a new folder for a project mkdir ~/project-name-here. You can then go to File -> Open and see it here.
  • You can clone a project by opening the Command Palette, type Clone and press enter
  • You can copy files in and out of your projects by dragging files in and out of Visual Studio Code.

Vagrant:

  • You can restart the VM by opening a terminal and running vagrant reload.
  • Your home directory is mounted to /vagrant inside the VM

SSH Keys for GIT:

If you havent set up an SSH key you can generate one with the ssh-keygen command in a fresh terminal window.

After that (or if you already have one) you can now connect to the Vagrant VM with:

vagrant ssh

To use the SSH key in the VM you can install keychain:

sudo apt update
sudo apt install keychain

Then configure it to use your main SSH key with:

echo "eval \`keychain --eval --agents ssh /vagrant/.ssh/id_rsa\`" >> ~/.bashrc
source ~/.bashrc

If all goes well it'll ask you for your ssh password (if you set one) and show some output from the keychain app.

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