Skip to main content

Docker - Even easier setup of Python

Streamline your Python development. This guide shows you how to use Docker and Microsoft Devcontainers for an incredibly easy and reproducible Python environment setup.

· 3 min read
View raw
Christophe
Markdown, WSL and Docker lover ~ Insatiably curious.
Cet article existe aussi en français — lire la version française.

Changelog

Latest
Updated Python version example from 3.12-bullseye to 3.13-bookworm (current stable; Debian 12 Bookworm is now the default base).

Docker - Even easier setup of Python

⚡
TL;DR

This article shows the fastest way yet to get a Python devcontainer: with Docker and the VSCode Docker extension installed, use the Command Palette's built-in "Dev Containers: Add Dev Container Configuration files..." wizard to auto-generate .devcontainer/devcontainer.json for Python, then click "Reopen in Container" — no manual file creation needed at all.

Nothing to install, nothing to configure, nothing to create first.

In a previous article (and its Windows-specific follow-up), I built the Dockerfile, compose.yaml and devcontainer.json files by hand. Turns out VSCode can generate all of that for you.

Here is how to use VSCode and Docker to create a ready-to-use Python environment, whether you're under Windows, Linux, or Mac.

The wizard is the fastest route, but writing the files yourself is what lets you control exactly what goes into the image — and keep development tooling out of production, as explained in One Docker Image for Production and Devcontainers - The Clean Way.

The only prerequisites are:

  • You should have Docker installed on your system
  • You should have Visual Studio Code
  • You've installed the Docker for Visual Studio Code addon in VSCode

Here's the payoff. Once the devcontainer is up, create a new file called main.py with this line print("Hello from your Python Devcontainer!").

Save the file.

Click in the terminal and type python main.py to execute the script and tadaaa:

Running the script

A working Python environment, terminal and interpreter ready, and not a single file created by hand. Here's how VSCode builds it for you.

If you have these three things, just do this:

  • Start Visual Studio Code
  • Press CTRL+SHIFT+P to open the Command Palette.
  • Select Dev Containers: Add Dev Container Configuration files...
  • And follow the wizard:
    • Search for Python
    • Select the most recent version of Python, right now it's 3.13-bookworm
    • No need to install additional features, just press Enter
    • Same for optional files; not needed, just press Enter

This done, VSCode will create a file called .devcontainer/devcontainer.json.

VSCode has created the .devcontainer/devcontainer.json file

See bottom right, please click on the Reopen in Container button.

Depending on the speed of your computer and if things were already downloaded, you'll get this screen:

VSCode and its terminal

See the bottom part, a terminal window has been displayed and you've a prompt showing vscode -> /workspaces/python $.

Click in the terminal and type python --version:

Version


Was this article helpful?
Did you try the steps in this article?