Loading...
Random Pearls Random Pearls New Menu
  • All Pearls
    • Random Pearls
  • Coding and Development - Reference …
    • Coding and Development - Reference …  (parent page)
    • Information Technology  (parent page of Coding and Development - Reference …)
  • New Menu
  • Authors
  •  
  • Contact Us
  • Sign up
  • Login
    Forgot Password?
  • Follow us on
Image Back to Top Back to top
Language Preference
This website has language specific content. Here you can set the language(s) of your preference.

This setting does not affect the current page. It is used only for filtering pages in menus, preview tiles and search results.

It can be changed any time by using the login menu (if you are logged in) or by clicking the Language button on the left bottom of the page.
Log in to save your preference permanently.



If you do not set this preference, you may see a header like This page has 'language_name' content in page preview tiles.
Search
  • Navigation
  • Similar
  • Author
  • More...
You are here:
All Content / Science and Technology / Information Technology / Coding and Development - Reference … / What's up with VS Code Remote, WSL, Docker and …
Table of Contents

Subscribe to Our Newsletter
Follow us by subscribing to our newsletter and navigate to the newly added content on this website directly from your inbox!
Login to subscribe to this page.
Categories  
Tags  
Author  
manisar
Author's Display Image

"Whenever you can, share. You never know who all will be able to see far away standing upon your shoulders!"

I write mainly on topics related to science and technology.

Sometimes, I create tools and animation.


What's up with VS Code Remote, WSL, Docker and Dev-containers?

May 28, 2021

Author - manisar

Code, develop, build, test and showcase like a Pro


Visual Studio Code has emerged as a mighty flag bearer of Microsoft in the field of free IDEs. The elegance, light footprint, flexibility - everything is awesome about it, and great thanks to Microsoft for developing such a beauty.

The icing on the cake has been the inclusion of remote development on Linux machines from Windows via WSL (or Remote-SSH for non-WSL Linux machines) and Docker extensions in VS Code, and dev-containerization.

Given below is a quick understanding and reference for setting up VS Code for remote development on WSL Linux machine, and using dev-containerization as well if we want.
On the same lines, it is possible, as explained below, to remotely develop on a real or virtual Linux machine (non-WSL) - by using the Remote-SSH extension.

Prerequisites

  1. Windows Pro or Home (the steps will be slightly different for Linux and MacOS, but the following will still give the basic understanding of step-by-step setup)
  2. VS Code - we'll install three of the following four significant extensions along the way:
    Remote-WSL extension
    Remote-SSH extension (this is not needed if you are sticking to WSL, but will replace the Remote-WSL extension if you are working with remote real or virtual Linux machines)
    Remote-Containers extension
    Docker extension
  3. Some understanding of docker. See Docker Overview for a quick one.
  4. Docker for Desktop SW

WSL2 Primer

Note: You may skip this section if you are connecting to a non-WSL Linux machine (from Windows). In that case, just focus on the Remote-SSH part.

Windows Subsystem for Linux (version 2) is a groundbreaking new feature in Windows. It basically allows Linux layers (docker-like) to run on a common Linux kernel provided by Windows. So, in effect, we can have different Linux distros on a Windows machine without having to create a virtual machine for each.

On a side note, Docker (i.e. the Docker for Desktop SW) itself can use this kernel for building and running Linux containers and we can have docker containers taking advantage of WSL2.

Given below is how I went about setting up VS Code for using WSL2 and docker containers from scratch. If you follow these, you may have to include or exclude a few extra steps depending upon your Windows version and Linux distro. I tried to be succinct for the sake of avoiding clutter. Also, WSL and WSL2 are different, and the steps below relate strictly to WSL2.

Installing and Using WSL2
  • Install WSL2 using the - Windows Subsystem for Linux Documentation.
WSL Distro in Start Menu
  • Installing a new WSL2 distro means building an image and running a container right away (the build and run are done automatically in the background by Windows).
    I installed Debian.
    Each such container gets an entry in the Start Menu. Let's install a Linux distro on your WSL2.

  • Now hop into this container by clicking on the start menu icon for your newly installed WSL machine.

  • We get the OS command prompt, and we can start refining and upgrading the OS as we would have done on any other VM or docker container, e.g. to start with, I installed git, python3, pip3, and enabled auto-completion by uncommenting in /etc/bash.bashrc (as Debian doesn't come with this feature enabled).

  • Now we can start building apps on our Linux machines. But what's coding without a capable IDE? Read on.


Fun with VS Code

Using WSL extension for remote development

Installing and Using the VS Code WSL extension (or the Remote-SSH extension)
  • Let's clone a git repo (into our Linux WSL distro) that can provide a web server - this will help in testing as a web server can be left running and connected from outside the container. I cloned (helloworld-django) in /root.

  • Run code . (in the Linux CLI). This starts VS Code on Windows.
    It's plain VS Code at this point of time running on Windows but connected to Linux filesystem. As good as it is, we'll not be able to use linting, debugging etc. in VS Code as of now. What we have is just a simple text editor. No debugging, no Git or any other plugins, no extensions.

  • If we, and we must, install the WSL extension (unless connecting to non-WSL machines), it makes VS Code a client-server thing. It starts a VS Code server running in the WSL machine, and VS Code client on Windows now connects to it.
    With VS Code and the Remote - WSL extension combined, VS Code’s UI runs on Windows, and all your commands, extensions, and even the terminal, run on Linux. You get the full VS Code experience, including autocomplete and debugging, powered by the tools and compilers installed on Linux.

Use Remote-SSH for non-WSL Linux machines

In the instructions above, we were able to run code . on the Linux CLI and it started a VS Code on the Windows host simply because WSL provides inherent connection to the host Windows machine.

If our code resided on a real Linux remote machine (or on VM that is not WSL-powered), Remote - SSH extension would have done this job for us (and we would not need the Remote-WSL extension). More detail are here.
Basically, we need to:

  • (optionally) setup and use key-based (password-less) authentication to our remote host
  • Use Remote - SSH extension to connect to the remote machine
  • if needed, use port-forwarding by starting an SSH tunnel from either within or outside VS Code

This extension will take care of both:

  • showing the code to us directly from the remote machine (for comparison, in the case of WSL, this can be done inherently by firing code . from the WSL CLI)
  • using VS Code magic on the code, such as autocompletion, linting etc. by using local extensions and libraries on the remote machine (this, for WSL, is provided the WSL extension)

Remote - SSH extension is similar to the Remote - WSL extension in that both of them start a VS Code server on the remote machine so that the coding experience you get on your local machine is not inferior to what you would have got had you been coding on remote machine itself!

Note: With Remote - SSH extension, for added security, you may create an SSH tunnel between local and remote and use that for attaching VS Code debugger to the remotely running program. Check Remote script debugging with SSH for more details.

An important thing to note is that we'll have to install the required extensions such as Python (along with Pylance and Jupyter) on the container (or remote host) specifically (through VS Code itself). But, note: do not install the extensions on the Linux (WSL) container or remote machine if you intend to further dev-containerize your code from within this container - see below.

Thus we have a full-featured VS Code at our disposal now with features such as linting, autocompletion, intellisense etc. This VS Code is connected to the code on a Linux machine (container, VM or real).

Before I knew of this extension, I used to mount the Linux directory on my Windows host, and open this mounted folder in VS Code. That's it. In order to see cross references and definitions etc. for third party libraries (such as Django and other packages), I used to download the Windows version of the libraries in Windows somewhere and point the environment in VS Code to those libraries. It was quite convoluted and painstaking. This is no longer needed.

Remote Explorer VS Code

Now, there appears an icon Remote Explorer in VS Code, and we see something like this there for 'WSL Targets'. That is, we see available WSL targets, and active folders in them, if any.
Note that in the screenshot, we also see the docker-desktop container (used by Docker for Desktop), as that is also a WSL target.


Next Level Fun with VS Code

Using remote container extension for developing within dev-containers

So far so good - we have a Linux machine (container), and we have some code residing there which we can connect to using VS Code and and we can develop or code as if that complete codebase along with its SW stack was on Windows. What else would we need?

We don't need anything else unless we want to be even more formal with development.

Yes, developing on a Linux machine makes sense if your real-world host is going to be a Linux machine - it's the only possibility mostly, and we expect to see real-world issues that we may face. But, there is a caveat.

The testing we are going to do in our Linux (WSL) container (or real or virtual machine) is still not going to truly reflect or mimic the runs executed on the production machine - for the simple inevitable reason that we installed development tools (such as so many VS Code extensions) on it. We can never be sure about anything not breaking on the production host unless we install the same development tools there and that will be a waste of resources and mostly money.

Here comes the idea of Dev Containers! With dev containers, VS Code creates a container on the fly (which is persistent though), and lets us develop our code in that container without needing to install the extensions and plugins etc. on the actual Linux container (or the real Linux host whatsoever).

Thus, we get two machines - one for development and the other for testing.

The former is the dev-container, and the latter is what we had initially set up on the WSL machine.

Thus we get a damn-real separation of roles. Development happens in an isolated machine and testing can be done on a production like machine! To make it even more ideal, while cloning the git (or setting up any source control SW) on the Linux container, we should create appropriate branch(es) and then dev-containerize those branches as per the steps given below.

Installing and Using the VS Code Remote-Containers Extension
  • Install the Remote - Containers extension in VS Code for next level sorcery.
    This extension is independent of the Remote - WSL extension, and we can use it on local code, or code residing on a real Linux host, or on code in a WSL machine, which is itself a container, (which our VS Code is currently connected to using the WSL extension). If we dev-containerize the code that resides in a WLS container (our case), what we get is container-ception!

  • The Remote Container extension basically containerizes any piece of code. If we were simply building locally, we could use this extension to build an image and a container using it (VS Code seems to identify and use the base layers to be used in the container on its own).

  • So, now, as suggested on VS Code help pages, we containerize the code that is residing on the (Debian - in my case) WSL machine.
    What will happen is that our main (Linux) container will remain intact like a production or testing machine, while the newly created VS Code's container can be used for isolated development along with having VS Code's extensions and plugins etc. That is why I asked earlier for not installing extensions and plugin on the WSL machine.
    Remember that for real testing, we should come back to the WSL machine (or to the real or virtual Linux machine), and run our app (Django web-server in our case) there outside VS Code.

The above adds the file .devcontainer/devcontainer.json file to the project. The VS Code window will reload and start building the dev container. A progress notification provides status updates. You only have to build a dev container the first time you open it; opening the folder after the first successful build will be much quicker.

Another thing to note is that our code on the Linux (WSL) container is bind-mounted into the dev-container. A quick excerpt on bind mount - While using this approach to bind mount the local filesystem into a container is convenient, it does have some performance overhead on Windows and macOS. There are some techniques that you can apply to improve disk performance, or you can open a repository in a container using a isolated container volume instead.

Dev Container in Remote Explorer
  • Now, in the Remote Explorer in VS Code, we see this.
    As should seem normal, the newly built dev-container runs inside docker (alongside other containers if there are any).

If you want to use a Docker host that is remote (i.e. not running on your local machine), see the Advanced Containers Configuration article.

Lastly, there remains another very useful extension to be installed on VS Code - VS Code Docker extension. This extension adds the functionality to build, manage, and deploy containerized applications from inside VS Code (remember that you will need the Remote-Container extension to actually use the container as your dev environment).

Advertisement

Important Links

  • Windows Subsystem for Linux Documentation

  • Get started using Visual Studio Code with Windows Subsystem for Linux
    Using Remote Containers in WSL 2
    VS Code Remote - WSL Extension
    Without this extesnion, while you can edit files in Linux using Windows-based tools, you can’t easily run or debug on Windows: you'd have to install all the same tools on Windows as you did on Linux, defeating the purpose of having an isolated Linux environment. You could view files from your \\wsl$\ share, but you wouldn't have access to features such as autocomplete, debugging, or linting.
    Remote Development using SSH and Remote - SSH extension

  • Get started with Docker remote containers on WSL 2
    VS Code Remote - Containers Extension
    VS Code Docker Extension

  • Developing inside a Container
    The Visual Studio Code Remote - Containers extension lets you use a Docker container as a full-featured development environment. It allows you to open any folder inside (or mounted into) a container and take advantage of Visual Studio Code's full feature set.
nocaption-VS Code Container Architecture


  • Advanced Container Configuration
    This article includes advanced setup scenarios for the Visual Studio Code Remote - Containers extension. See the Developing inside a Container article for additional information.

  • https://github.com/mattwojo/helloworld-django

  • A very thorough article on css-tricks.com - A Gentle Introduction to Using a Docker Container as a Dev Environment.

A Reason to Donate

The page has no or minimal advertisement. If you want to contribute towards keeping this website running, think about donating a small amount. This helps in reducing the number of ads as well. You can decide the amount on the payment page. Thanks!

Advertisement
Close ad Ad

Return to Coding and Development - Reference and Tools

Tell us what you think (select text for formatting, or click )

Copyright © randompearls.com 2020

Privacy Policy