This submit gives a step-by-step information for organising a Python dockerized improvement surroundings with VScode and the Dev Containers extension.
Within the earlier submit on this matter, Setting A Dockerized Python Atmosphere—The Onerous Means, we noticed the best way to arrange a dockerized Python improvement surroundings through the command line interface (CLI). On this submit, we’ll evaluation a extra elegant and strong method for organising a dockerized Python improvement surroundings utilizing VScode and the Dev Containers extension.
Associated articles:
By the tip of this tutorial, it is possible for you to to arrange a easy Python improvement surroundings with VScode and the Dev Containers extension.
To observe together with this tutorial, you will have the next:
- Docker Desktop (or equal) in case you are utilizing a macOS or Home windows OS machine, or Docker put in in case you are utilizing a Linux OS
- Docker Hub account to tug the picture from
- VScode IDE and the Dev Containers extension put in
All through this tutorial, we’ll use the official Python picture — python:3.1o
.
All of the code examples on this submit can be found right here:
Earlier than getting began, let’s clarify what the Dev Containers extension is and when it’s best to think about using it.
In a nutshell, the VScode Dev Containers extension allows you to open an remoted VScode session inside a docker container seamlessly. The extent of isolation contains the next three layers:
- Atmosphere
- VScode settings
- VScode extensions
The devcontainer.json
file defines the session settings, enabling us to set and outline the above three layers.
To set and launch your mission folder inside a container with the Dev Containers extension, you will have the next two elements:
- Set up the Dev Containers extension
- In your mission folder, create a folder named
.devcontainer
and set adevcontainer.json
file
The beneath diagram describes the Dev Containers common structure:
Upon launch, the Dev Containers extension spins a brand new VScode session inside a container. By default, it mounts the native folder to the container, which permits us to maintain the code persistent and sync with our native folder. You may mount extra folders, however that is outdoors the scope of this tutorial.
Within the subsequent part, we’ll see the best way to arrange a Python surroundings with the devcontainer.json
file.
Earlier than getting began with the devcontainer.json
settings, let’s first outline the scope of the event surroundings. It ought to embody the next options:
- Python 3.10
- Assist Jupyter notebooks
- Set up required libraries — Pandas and VScode Jupyter supporting libraries
- Set up supporting extensions — Python and Jupyter
Within the following sections, we’ll dive into the core performance of the devcontainer.json
file. We are going to begin with a minimalist Python surroundings and reveal the best way to customise it by including totally different customization layers.
Construct vs. Picture
The primary requirement for launching a containerized session with the Dev Containers extension is to outline the picture settings. There are two approaches for setting the picture:
- Construct the picture and run it in the course of the launch time of the container with the
construct
argument. This argument allows you to outline a Dockerfile for the construct and cross arguments to thedocker construct
perform. As soon as the construct course of is finished, it should launch the session contained in the container - Launch the session with an present picture utilizing the
picture
argument
Relying on the use circumstances, every technique has its personal execs and cons. It is best to think about using the picture
argument when you have got a picture that absolutely meets the surroundings necessities. Likewise, a great use case for the construct
argument is when you have got a base picture however want so as to add minor customization settings.
Within the subsequent part, we’ll begin with a easy instance of launching a Python surroundings utilizing the picture
argument to import the official Python picture (python:3.10
).
Fundamental Dockerized Python Atmosphere
The beneath devcontainer.json
file gives a easy instance for organising a Python surroundings. It makes use of the picture
argument to outline the python:3.10
picture because the session surroundings:
devcontainer.json
{
"identify": "Python Improvement Atmosphere",
"picture": "python:3.10"
}
The identify
argument defines the surroundings identify. On this case, we set it as Python Improvement Atmosphere.
Earlier than launching the surroundings, please make certain:
- Your Docker Desktop (or equal) is open
- You might be logged in to Docker Hub (or pull upfront the Python picture)
- The
devcontainer.json
file is ready within the mission folder underneath the.devcontainer
folder:
.
└── .devcontainer
└── devcontainer.json
The code for this instance is offered right here.
To launch a session, click on the Dev Container ><
image on the underside left and choose the Reopen in Container
possibility as demonstrated within the screenshot beneath:
Notice that in the course of the first launch time of the session, the Dev Containers extension will search for the picture that was outlined by the picture
argument (on this case — python:3.10
). If the picture shouldn’t be accessible regionally, it should pull it from Docker Hub, and it would take a couple of minutes. Afterward, it ought to take a number of seconds to launch the session.
Within the above screenshot, you’ll be able to see the mapping between the devcontainer.json
arguments and the session settings. The session identify is now accessible on the underside proper (marked in purple) and aligned with the worth of the identify
argument. Likewise, the session is now working contained in the python:3.10
container, and you may launch Python from the terminal.
The Python container comes with the default Python libraries. Within the following part, we’ll see how we will add extra layers on prime of the Python base picture with the construct
argument.
Customise the Python Atmosphere with a Dockerfile
Let’s now customise the above surroundings by modifying the devcontainer.json
. We are going to substitute the picture
argument with theconstruct
argument. The construct
argument permits us to construct the picture in the course of the session launch time with a Dockerfile and cross arguments to the docker construct
perform. We are going to observe the identical method as demonstrated on this submit to set the Python surroundings:
- Import the
python:3.10
as the bottom picture - Set a digital surroundings
- Set up the required libraries
We are going to use the next Dockerfile to set the Python surroundings:
Dockerfile
FROM python:3.10ARG PYTHON_ENV=my_env
ENV PYTHON_ENV=$PYTHON_ENV
RUN mkdir necessities
COPY necessities.txt set_python_env.sh /necessities/
RUN bash ./necessities/set_python_env.sh $PYTHON_ENV
We use the FROM
argument to import the Python picture, and the ARG
and ENV
arguments to set the digital surroundings as an argument and surroundings variable. As well as, we use the next two helper recordsdata to set a digital surroundings and set up the required libraries:
necessities.txt
— a setting file with an inventory of required libraries. For this demonstration, we’ll set up the Pandas library, model 2.0.3., and the Jupyter supporting libraries (ipykernel, ipywidgets, jupyter). The wheels library is a supporting library that handles C dependenciesset_python_env.sh
— a helper bash script that units a digital surroundings and installs the required libraries utilizing thenecessities.txt
file
necessities.txt
wheel==0.40.0
pandas==2.0.3
ipykernel
ipywidgets
jupyter
set_python_env.sh
#!/usr/bin/env bashPYTHON_ENV=$1
python3 -m venv /decide/$PYTHON_ENV
&& export PATH=/decide/$PYTHON_ENV/bin:$PATH
&& echo "supply /decide/$PYTHON_ENV/bin/activate" >> ~/.bashrc
supply /decide/$PYTHON_ENV/bin/activate
pip3 set up -r ./necessities/necessities.txt
Final however not least, we’ll use the next take a look at file to judge if the Pandas library was put in correctly and print Whats up World! message:
test1.py
import pandas as pdprint("Whats up World!")
Let’s make the adjustments within the devcontainer.json
file, and substitute the picture
argument with the construct
argument:
devcontainer.json
{
"identify": "Python Improvement Atmosphere",
"construct": {
"dockerfile": "Dockerfile",
"context": ".",
"args": {
"PYTHON_ENV": "my_python_dev"
}
}
}
The recordsdata for this instance can be found right here.
The construct
sub-arguments allow us to customise the picture construct by passing arguments to the docker construct
perform. We use the next arguments to construct the picture:
dockerfile
— the trail and identify of the Dockerfilecontext
— set the trail of the native file system to allow entry for recordsdata with theCOPY
argument in the course of the construct time. On this case, we use the present folder of thedevcontainer.json
file (e.g., the.devcontainer
folder).args
— set and cross arguments to the container in the course of the construct course of. We use thePYTHON_ENV
argument to set the digital surroundings and identify it asmy_python_dev
It is best to have the three recordsdata — Dockerfile
, necessities.txt
, and set_python_env.sh
saved underneath the .devcontainer
folder, together with the devcontainer.json
file:
.
├── .devcontainer
│ ├── Dockerfile
│ ├── devcontainer.json
│ ├── necessities.txt
│ └── set_python_env.sh
└── test2.py
Let’s now launch the session utilizing the brand new settings and take a look at it with the test1.py
file:
As you’ll be able to discover within the above screenshot, we had been capable of efficiently run the take a look at script from the terminal (marked in purple), and it printed the Whats up World! message as anticipated (marked in inexperienced). As well as, the digital surroundings we set within the picture (my_python_dev
) is loaded by default (marked in yellow).
Within the subsequent part, we’ll see the best way to customise the VScode settings of the Dev Containers session.
Customise VScode Settings
One of many nice options of the Dev Containers extension is that it isolates the session setting from the primary VScode settings. This implies you’ll be able to absolutely customise your VScode settings on the mission degree. It extends the event surroundings’s reproducibility past the Python or OS settings. Final however not least, it makes collaboration with others or engaged on a number of machines seamless and environment friendly.
We are going to conclude this tutorial with the following instance, the place we see the best way to customise the VScode settings with the customizations
argument. We are going to add the argument to the earlier instance and use the vscode
sub-argument to set the surroundings default Python interpreter and the required extensions:
devcontainer.json
{
"identify": "Python Improvement Atmosphere",
"construct": {
"dockerfile": "Dockerfile",
"context": ".",
"args": {
"PYTHON_ENV": "my_python_dev"
}
},
"customizations": {
"vscode": {
"settings": {
"python.defaultInterpreterPath": "/decide/my_python_dev/bin/python3",
"python.selectInterpreter": "/decide/my_python_dev/bin/python3"
},
"extensions": [
"ms-python.python",
"ms-toolsai.jupyter"
]
}
}
}
The recordsdata for this instance can be found right here.
We use the settings
argument to outline the Python digital surroundings as outlined within the picture. As well as, we use the extensions
argument for putting in the Python and Jupyter supporting extensions.
Notice: The trail of the the digital surroundings outlined by the kind of applicationas that was used to set the surroundings. As we use
venv
and named it asmy_python_dev
, the trail isdecide/my_python_dev/bin/python3
.
After we add the Python extension, we will launch Python scripts utilizing the extension plug-in, as demonstrated within the screenshot beneath. As well as, we will execute the Python code leveraging the Juptyer extension, in an interactive mode:
On this tutorial, we reviewed the best way to set a dockerized Python surroundings with VScode and the Dev Containers extension. The Dev Containers extension makes the combination of containers with the event workflow seamless and environment friendly. We noticed how, with a number of easy steps, we will set and customise a dockerized Python surroundings utilizing the devcontainer.json
file. We reviewed the 2 approaches for setting the session picture with the picture
and construct
arguments and setting extensions with the customizations
argument. There are extra customization choices that weren’t coated on this tutorial, and I like to recommend checking them:
- Outline surroundings variables
- Mount extra volumes
- Set arguments to the
docker run
command - Run post-launch command
In case you are inquisitive about diving into extra particulars, I like to recommend checking this tutorial: