Home Machine Learning Setting A Dockerized Python Surroundings — The Elegant Approach | by Rami Krispin | Apr, 2024

Setting A Dockerized Python Surroundings — The Elegant Approach | by Rami Krispin | Apr, 2024

0
Setting A Dockerized Python Surroundings — The Elegant Approach | by Rami Krispin | Apr, 2024

[ad_1]

This put up supplies a step-by-step information for establishing a Python dockerized improvement setting with VScode and the Dev Containers extension.

Within the earlier put up on this subject, Setting A Dockerized Python Surroundings—The Arduous Approach, we noticed easy methods to arrange a dockerized Python improvement setting through the command line interface (CLI). On this put up, we’ll evaluate a extra elegant and sturdy method for establishing a dockerized Python improvement setting 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 setting with VScode and the Dev Containers extension.

VScode illustration ( created by the writer with Midjourney)

To observe together with this tutorial, you’ll need 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 drag 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 put up can be found right here:

Earlier than getting began, let’s clarify what the Dev Containers extension is and when it is 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:

  • Surroundings
  • 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’ll need the next two parts:

  • Set up the Dev Containers extension
  • In your mission folder, create a folder named .devcontainer and set a devcontainer.json file

The beneath diagram describes the Dev Containers common structure:

The Dev Containers extension structure (credit score Rami Krispin)

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 allows us to maintain the code persistent and sync with our native folder. You may mount extra folders, however that is exterior the scope of this tutorial.

Within the subsequent part, we’ll see easy methods to arrange a Python setting with the devcontainer.json file.

Earlier than getting began with the devcontainer.json settings, let’s first outline the scope of the event setting. It ought to embrace 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 setting and display easy methods to customise it by including totally different customization layers.

Construct vs. Picture

The principle 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 the docker 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 instances, every methodology has its personal professionals and cons. It’s best to think about using the picture argument when you may have a picture that totally meets the setting necessities. Likewise, a superb use case for the construct argument is when you may have 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 setting utilizing the picture argument to import the official Python picture (python:3.10).

Fundamental Dockerized Python Surroundings

The beneath devcontainer.json file supplies a easy instance for establishing a Python setting. It makes use of the picture argument to outline the python:3.10 picture because the session setting:

devcontainer.json

{
"identify": "Python Improvement Surroundings",
"picture": "python:3.10"
}

The identify argument defines the setting identify. On this case, we set it as Python Improvement Surroundings.

Earlier than launching the setting, please be sure that:

  • Your Docker Desktop (or equal) is open
  • You might be logged in to Docker Hub (or pull prematurely the Python picture)
  • The devcontainer.json file is ready within the mission folder beneath the .devcontainer folder:
.
└── .devcontainer
└── devcontainer.json

The code for this instance is accessible 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:

Launching the session inside a container with the Dev Containers extension (screenshot by the writer)

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 just isn’t accessible regionally, it should pull it from Docker Hub, and it’d take a couple of minutes. Afterward, it ought to take just a few seconds to launch the session.

The VScode session inside a container (screenshot by the writer)

Within the above screenshot, you may 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’ll 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 Surroundings with a Dockerfile

Let’s now customise the above setting by modifying the devcontainer.json. We are going to exchange the picture argument with theconstruct argument. The construct argument allows 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 put up to set the Python setting:

  • Import the python:3.10 as the bottom picture
  • Set a digital setting
  • Set up the required libraries

We are going to use the next Dockerfile to set the Python setting:

Dockerfile

FROM python:3.10

ARG 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 ENVarguments to set the digital setting as an argument and setting variable. As well as, we use the next two helper information to set a digital setting and set up the required libraries:

  • necessities.txt — a setting file with a listing 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 dependencies
  • set_python_env.sh — a helper bash script that units a digital setting and installs the required libraries utilizing the necessities.txt file

necessities.txt

wheel==0.40.0
pandas==2.0.3
ipykernel
ipywidgets
jupyter

set_python_env.sh

#!/usr/bin/env bash

PYTHON_ENV=$1

python3 -m venv /choose/$PYTHON_ENV
&& export PATH=/choose/$PYTHON_ENV/bin:$PATH
&& echo "supply /choose/$PYTHON_ENV/bin/activate" >> ~/.bashrc

supply /choose/$PYTHON_ENV/bin/activate

pip3 set up -r ./necessities/necessities.txt

Final however not least, we’ll use the next check file to guage if the Pandas library was put in correctly and print Hey World! message:

test1.py

import pandas as pd

print("Hey World!")

Let’s make the modifications within the devcontainer.json file, and exchange the picture argument with the construct argument:

devcontainer.json

{
"identify": "Python Improvement Surroundings",
"construct": {
"dockerfile": "Dockerfile",
"context": ".",
"args": {
"PYTHON_ENV": "my_python_dev"
}
}
}

The information 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 Dockerfile
  • context — set the trail of the native file system to allow entry for information with the COPY argument in the course of the construct time. On this case, we use the present folder of the devcontainer.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 the PYTHON_ENV argument to set the digital setting and identify it as my_python_dev

It’s best to have the three information — Dockerfile, necessities.txt, and set_python_env.sh saved beneath 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 check it with the test1.py file:

Working a Python script to check the setting (screenshot by the writer)

As you may discover within the above screenshot, we had been in a position to efficiently run the check script from the terminal (marked in purple), and it printed the Hey World! message as anticipated (marked in inexperienced). As well as, the digital setting we set within the picture (my_python_dev) is loaded by default (marked in yellow).

Within the subsequent part, we’ll see easy methods 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 principle VScode settings. This implies you may totally customise your VScode settings on the mission stage. It extends the event setting’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 easy methods 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 setting default Python interpreter and the required extensions:

devcontainer.json

{
"identify": "Python Improvement Surroundings",
"construct": {
"dockerfile": "Dockerfile",
"context": ".",
"args": {
"PYTHON_ENV": "my_python_dev"
}
},
"customizations": {
"vscode": {
"settings": {
"python.defaultInterpreterPath": "/choose/my_python_dev/bin/python3",
"python.selectInterpreter": "/choose/my_python_dev/bin/python3"
},
"extensions": [
"ms-python.python",
"ms-toolsai.jupyter"
]
}
}
}

The information for this instance can be found right here.

We use the settings argument to outline the Python digital setting 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 setting outlined by the kind of applicationas that was used to set the setting. As we use venv and named it as my_python_dev, the trail is choose/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 easy methods to set a dockerized Python setting 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 just a few easy steps, we will set and customise a dockerized Python setting 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 lined on this tutorial, and I like to recommend checking them:

  • Outline setting variables
  • Mount extra volumes
  • Set arguments to the docker run command
  • Run post-launch command

If you’re taken with diving into extra particulars, I like to recommend checking this tutorial:

[ad_2]