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
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:
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
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.
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".
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.
- 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.
- You can restart the VM by opening a terminal and running
vagrant reload
. - Your home directory is mounted to /vagrant inside the VM
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.