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:
- https://docs.microsoft.com/en-us/windows/wsl/install-win10#manual-installation-steps
- Docker - https://docs.docker.com/install/ - with WSL2 feature enabled https://docs.docker.com/docker-for-windows/wsl/
- Visual Studio Code - https://code.visualstudio.com/
Prepare Visual Studio Code
Open Visual Studio Code and install extension 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
.
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.
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.
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.
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.
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
.
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.
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. 🥳
Also you can check that you have one Docker container running by executing command docker ps
. 👍
Happy experiments! 🦺
[eof]