Home

Awesome

A Dockerized Python Development Environment Template

This repository provides a template for a dockerized Python development environment with VScode and the Dev Containers extension. By default, the template launches a dockerized Python environment and installs add-ins like Quarto and Jupyter. The template is highly customizable with the use of environment variables.

See also:

<br> <figure> <img src="images/python-template.gif" width="100%" align="center"/></a> <figcaption> Figure 1 - Launch the template with VScode</figcaption> </figure> <br/>

Scope

This VScode template includes the following features:

The template includes the following files:

.
ā”œā”€ā”€ .devcontainer
ā”‚Ā Ā  ā”œā”€ā”€ Dockerfile
ā”‚Ā Ā  ā”œā”€ā”€ devcontainer.env
ā”‚Ā Ā  ā”œā”€ā”€ devcontainer.json
ā”‚Ā Ā  ā”œā”€ā”€ install_dependencies.sh
ā”‚Ā Ā  ā”œā”€ā”€ install_quarto.sh
ā”‚Ā Ā  ā”œā”€ā”€ install_requirements.sh
ā”‚Ā Ā  ā””ā”€ā”€ requirements.txt
ā”œā”€ā”€ tests
ā”‚Ā    ā”œā”€ā”€ test1.py
ā”‚Ā    ā”œā”€ā”€ test2.ipynb
ā”‚Ā    ā”œā”€ā”€ test3.html
ā”‚Ā    ā”œā”€ā”€ test3.qmd
ā”‚Ā    ā””ā”€ā”€ test3_files
ā””ā”€ā”€ README.md

Where the .devcontainer folder contains the environment settings and the tests folder contains different tests.

General Requirements

To use this template out of the box, you will need on your local machine the following settings:

A step-by-step guide for setting the above prerequisites is available here: https://github.com/RamiKrispin/vscode-python/tree/main#prerequisites

Clone the Template

It is straightforward to use this template and clone it to a different account. Click the Use this template green button on the top right and select the Create a new repository option. The steps from there are similar for creating a new repository on your account. At the end of this process, it generates a new repository with the template. Figure 2 below demonstrates the full process.

<br> <figure> <img src="images/use-template.gif" width="100%" align="center"/></a> <figcaption> Figure 2 - Create a clone of the template </figcaption> </figure> <br/>

This template is ready to use out of the box with Github Codespace:

<br> <figure> <img src="images/codespace.gif" width="100%" align="center"/></a> <figcaption> Figure 3 - Launching the template with Github Codespace </figcaption> </figure> <br/>

The Dev Containers Settings

The template was created to enable seamless customization and modification of the Python environment with the use of environment variables. That includes the Python version, the virtual environment name, installation libraries, setting environment variables, etc. The template can be used as a baseline for setting a dockerized Python environment or as a baseline for a more customized template using the devcontainer.json file:

.devcontainer/devcontainer.json

{
    "name": "${localEnv:PROJECT_A_NAME:my_project_name}",
    // "image": "python:3.10",
    "build": {
        "dockerfile": "Dockerfile",
        "args": {
            "ENV_NAME": "${localEnv:PROJECT_A_NAME:my_project_name}",
            "PYTHON_VER": "${localEnv:PYTHON_VER:3.10}",
            "QUARTO_VER": "${localEnv:QUARTO_VER:1.3.450}"
        }
    },
    "customizations": {
        "settings": {
            "python.defaultInterpreterPath": "/opt/conda/envs/${localEnv:PROJECT_A_NAME:my_project_name}/bin/python3",
            "python.selectInterpreter": "/opt/conda/envs/${localEnv:PROJECT_A_NAME:my_project_name}/bin/python3"
        },
        "vscode": {
            "extensions": [
                // Documentation Extensions
                "quarto.quarto",
                "purocean.drawio-preview",
                "redhat.vscode-yaml",
                "yzhang.markdown-all-in-one",
                // Docker Supporting Extensions
                "ms-azuretools.vscode-docker",
                "ms-vscode-remote.remote-containers",
                // Python Extensions
                "ms-python.python",
                "ms-toolsai.jupyter",
                // Github Actions
                "github.vscode-github-actions"
            ]
        }
    },
    // Optional, mount local volume:
    // "mounts": [
    //     "source=${localEnv:DATA_FOLDER},target=/home/csv,type=bind,consistency=cache"
    // ],
    "remoteEnv": {
        "MY_VAR": "${localEnv:MY_VAR:test_var}"
    },
    "runArgs": [
        "--env-file",
        ".devcontainer/devcontainer.env"
    ],
    "postCreateCommand": "python3 tests/test1.py"
}

Note: The default setting uses the build argument with the Dockerfile and some bash helper files. Alternatively, you can use the image argument to use any other container.

The devcontainer.json main arguments:

Customization

The template enables you to customize the environment settings with the use of environment variables. Below is the list of environment variables and their functionality:

Other settings: