DEV Community

Navneet Karnani
Navneet Karnani

Posted on • Originally published at blog.mandraketech.in on

VSCode DevContainer for Python Programmers

In today's article, we will explore how to set up a VSCode devcontainer for Python programmers. We will cover the basics of what is a devcontainer and why it's useful, how to create one, and some tips on how to use it effectively.

What is a DevContainer?

A devcontainer is a lightweight, reproducible, container that provides a consistent development environment for your projects. It allows you to develop, run, and debug your code in an isolated environment, which can help you focus on writing code rather than setting up your development environment.

Why Use a DevContainer?

There are several reasons why using a devcontainer is beneficial for Python programmers:

  1. Consistency : A DevContainer ensures that everyone on your team has the same development environment, which can make it easier to collaborate and work together.

  2. Isolation : A devcontainer provides an isolated environment for your code, which can help prevent conflicts with other dependencies or tools you may be using.

  3. Reproducibility : A devcontainer makes it easy to reproduce your development environment on any machine, which can be useful if you need to work remotely or share your project with others.

  4. Efficiency : By setting up a devcontainer once and then simply opening it whenever you want to work on your Python project, you can save time and effort by avoiding the hassle of setting up your development environment every time.

This article delves into getting a VS Code DevContainer Development environment based setup for early Python programmers. The environment runs on Debian, and hence is a good place to start for all School / College students too.

As part of my investigations for my college teaching environments, I was in a situation where I needed to teach Python. And, as some of my readers know, I am compulsively obsessed with not installing any compiler, or programming environment, on my local machine. It has to run in a throwaway-able environment.

So, I looked at the default DevContainer setup that Microsoft ships as part of their images. The environment created a 2GB image on my machine. That was really not something I like working with. So, here are the things you to be more efficient, and nimble.

Prerequisites

This article assumes you have the following already installed and configured:

Getting started

Create an empty project folder in a location of your choice on the machine. And then:

mkdir .devcontainer

This will create a .devcontainer folder inside that. The below files will go inside this folder.

The Dockerfile

I used the following Dockerfile for the environment:

FROM debian:stable-slimRUN apt-get update \ && apt-get install -y git python3 python3-pip \ && apt-get cleanWORKDIR /rootCMD ["sleep infinity"]
Enter fullscreen mode Exit fullscreen mode

This gets us in a good place. It has support for the tools mentioned in the install line, and generates an image the size of approximately 820MB. A far cry from the 2GB+ from the Microsoft Container repository. Plus, I have control over the linux version, and more.

The DevContainer

The Dockerfile is never enough. It has to be supplemented with an appropriate devcontainer.json to be effective. So, here is the my version of that file.

{ "name": "python-dev", "build": { "dockerfile": "Dockerfile" }, "customizations": { "vscode": { "settings": { "remote.downloadExtensionsLocally": true, "telemetry.enableTelemetry": false, "extensions.ignoreRecommendations": false, "workbench.remoteIndicator.showExtensionRecommendations": false }, "extensions": ["mhutchie.git-graph", "ms-python.python", "VisualStudioExptTeam.vscodeintellicode", "samuelcolvin.jinjahtml", "donjayamanne.python-environment-manager", "ecmel.vscode-html-css", "twixes.pypi-assistant", "redhat.vscode-yaml"] } }}
Enter fullscreen mode Exit fullscreen mode

There you go. Now you are ready to Ctrl+P or Cmd+P and Reopen in Container .

Have fun coding in Python !!

About the Author

The Author, Navneet Karnani, began coding with Java in 1997 and has been a dedicated enthusiast ever since. He strongly believes in the "Keep It Simple and Stupid" principle, incorporating this design philosophy into all the products he has developed.

Navneet works as a freelancer and is available for contracts, mentoring, and advisory roles related to technology and its application in software product development.

Additionally, Navneet serves as a visiting faculty member at FLAME University.

Driven software engineer (Java since 1997) with a hands-on passion for building impactful tech products. Possesses over 25 years of experience crafting solutions to complex business and technical challenges.

]]>

Top comments (0)

👋 Kindness is contagious

If you found this post useful, please drop a ❤️ or leave a kind comment!

Okay