To work with any programming language you need tools 🧰 to install before starting a journey 🧭 (compilers, code highlights, IntelliSense, etc.). Sometimes when we would like to experiment with a new programming language or new framework version that is in alfa or beta we do not want to impact 💥 your current working environment, you just want to easy set-up, make experiments, and easy clean up all tools. Development environment isolation would solve this.

Microsoft has a solution for this. It is Remote - Container extension for Visual Studio Code that is part of Remote Development Extensions pack. Remote - Container helps to run the development environment using a Docker container in WSL2 and interacts with that using Visual Studio Code. All your environment will be isolated without any impact on your current environment.

Let`s try to create Python development environment using Remote - Container extension!

Steps:

  • Install prerequisite tools
  • Prepare Visual Studio Code
  • Create development environment in a container

Prerequisites 🔨

Tools you have to install before create development environments:

Prepare Visual Studio Code

Open Visual Studio Code and install extension Remote - Containers.

Visual Studio Code extensions Remote - Containers.

After installation is completed, reload Visual Studio Code and check if you have available new commands. Press shortcut Ctrl+Shift+P and type Remote-containers.

Remote - Container extension available commands.

Create development environment

Now we are ready to create a new isolated Python development environment. First, we will create a separate directory where we make Python experiments. The directory will be mapped into a container.

> mkdir python-experiment
> cd python-experiment
> code .
Create directory for experiment.

In Visual Studio Code open command palette Ctrl+Shift+P and choose command Remote-Containers: Add Development Container Configuration Files... or press the green WSL icon on the left bottom corner.

WSL icon in Visual Studio Code.

The next step is to select a predefined container configuration definition. Microsoft has a bunch of predefined environment configuration definitions available on GitHub. As we decided to set-up Python environment our preference goes to Python 3.

Select predefined container configuration definition.

After configuration selection, you will be asked to choose few options. I selected Python 3 version and unchecked Node.js installation. Our Python development environment definition structure was created. If we review created files we will see one main file devcontainer.json which contains all instructions that describe the environment. More details about each configuration property can be found here. And the second file is Docker container build instructions.

C:\...\python-experiment
└───.devcontainer
        devcontainer.json
        Dockerfile
Container based environment definition.

When Visual Studio Code creates all files Remote - Container extension detects container definition and shows notification dialog on the bottom right side appears to start environment and connect with Visual Studio Code to our environment. Click Reopen in Container. If you miss that notification open command palette Ctrl+Shift+P and choose Reopen in Container.

Notification dialog to run environment in container and connect.

Remote - Container extension will do several steps for us:

  • Build Docker container defined in file Dockerfile.
  • Runs Docker image with volume that references the current directory.
  • Configure and run Visual Studio Code Server on the container.
  • Installs extensions listed in environment definition.
  • Connects Visual Studio Code to create a container using forwarded ports.

In diagram result, will looks like the following.

Developing inside a Container - https://code.visualstudio.com/docs/remote/containers#_getting-started

When all steps are done you are ready to start developing on Python. Create a new file hello.py and add Python code.

print('hello world')

Press F5 and your code is running. 🥳

Run Python code from Visual Studio Code in Remote Docker container.

Also you can check that you have one Docker container running by executing command docker ps. 👍

List of running Docker containers.

Happy experiments! 🦺

[eof]