Search code examples
debuggingvisual-studio-codevagrantpyramid

How do I configure VS Code to debug code in a vagrant container?


I am working on a new python/pyramid web application and I am having difficulty setting up VS Code to debug python code in a vagrant container. Apparently, this is possible with pycharm, but I would prefer to use VS Code. The remote debugging docs haven't been too helpful. If anyone has experience or pointers on how to do this, I would appreciate it.


Solution

  • My Environment

    Windows 10, and I am running Ubuntu 16.04 inside Virtualbox with Vagrant. After I do vagrant up to start Virtualbox/Ubuntu, among the messages Vagrant provides is the SSH username and SSH address (in my case vagrant and 127.0.0.1:2222 respectively). Ensure that you have an SSH client other than putty set up (I have Openssh - it's available on the Windows store and is typically installed at C:\Windows\System32\OpenSSH\ssh.exe).

    First, install the Remote Development extension into VS Code. Then push F1, type remote to bring up the remote development choices, and choose Remote-SSH: Connect to Host...

    A Problem

    Vagrant defaults to forwarding port 22 to 2222, and this seems to be a problem for VS Code. I can ssh into Virtualbox/Vagrant fine with both git-bash and at the Windows command line (which uses Openssh) with the SSH Username and Address (above) by typing ssh vagrant@127.0.0.1 -p 2222 (note that ssh vagrant@127.0.0.1:2222 will NOT work). However, if you type this into VS Code to connect to the host, it will not work - it won't connect when a port is specified with -p.

    Solution

    If you are using Virtualbox with Vagrant, open the VirtualBox GUI and open the Settings for your running VM. Click the Network item and then the appropriate Adapter. Click Advanced and then click the Port Forwarding button. Change the 2222 to 22 (i.e. you're just forwarding port 22 to 22 - you can make this change permanent in the Vagrantfile with config.vm.network "forwarded_port", guest: 22, host: 22) and then exit the Settings.

    Now connect...

    After you've changed the port forwarding so that Virtualbox is listening on the default SSH port of 22, then you can connect from VS Code with just ssh vagrant@127.0.0.1.

    One more thing...

    In VS Code hit F1, type remote and choose Remote-SSH: Settings. At the bottom of remote settings, turn on Show Login Terminal. (This is where you will type the password to complete the SSH login [default password is vagrant]). I have not yet set things up for key-based login. Maybe I'll update this answer when I do.

    Postscript

    Looks like it's still pretty involved to set up remote debugging at this point.