Skip to main content

How to use Microsoft Dev Containers (Visual Studio Code, Python)

In this article, I will show you how to use Microsoft Dev Containers to build a simple "Hello World" application in Python on a Mac.

How to use Microsoft Dev Containers (Visual Studio Code, Python)

In this article, I will show you how to use Microsoft Dev Containers to build a simple "Hello World" application in Python on a Mac.

Microsoft Dev Containers simplify the development process by creating lightweight, isolated environments within containers. With Dev Containers, developers can easily set up consistent and reproducible development environments across different machines and platforms.

info

These instructions were written for and tested on a Mac.

Step 1. Install the prerequisites

Before getting started, ensure that you have the following prerequisites installed on your Mac:

  • Docker: Visit the Docker website (https://www.docker.com/) and download and install Docker Desktop for your operating system
  • Visual Studio Code: Install Visual Studio Code (VS Code) on your operating system by downloading it from the official website (https://code.visualstudio.com/)

Step 2. Create a project folder

Open up a terminal window and do the following:

mkdir -p ~/projects/python/python-container-demo
cd ~/projects/python/python-container-demo

Step 3. Set up the development environment

  • Make sure Docker is running in the background (open -a Docker on a Mac)
  • Open Visual Studio Code (note the dot at the end of the command to indicate the current directory):
code .
  • Open the extensions window (Cmd+Shift+X on a Mac)
  • Search for and install the "Dev Containers" extension – this extension allows you to work with Dev Containers seamlessly
  • Open the command palette (Cmd+Shift+P) and search for: "Dev Containers: Add Development Container Configuration Files"
  • Choose the "Python 3" configuration – this will generate the necessary configuration files for Python development within a Dev Container

I selected the following features:

  • Python 3 dev containers
  • default version
  • No other features

This will create a file in a hidden folder in your project (.devcontainer)

  • Click the button Reopen in Container when it appears
tip

The Reopen in Container button should appear when you reopen the project in VS Code. If it doesn't: make sure Docker was up first.

  • Inside VS Code, select View / Terminal
  • You should see a prompt like this:
vscode ➜ /workspaces/python-container-demo $ 
  • Run this command in that window to verify that the version of Python is correct:
python --version

To compare, try that outside of the VS Code terminal.  The version may or may not be different.  In fact, you might not even have Python installed.  This is all running in the container.

Step 4. Create and run a Hello World app

  • In VS Code, create a new file called **app.py** in the root of your project directory
  • Edit **app.py** and add the following code and save the file:
if __name__ == "__main__": 
print("Hello, World!")
  • From within the VS Code terminal window, run the program:
python app.py
  • Verify that the output is correct

Example project

You can find the example project here:

Conclusion

Microsoft Dev Containers provide a streamlined approach to creating consistent development environments across different platforms.

By following the steps outlined in this article, you can easily set up and utilize Dev Containers to build a simple "Hello World" app in Python on your Mac.

Dev Containers help maintain a clean and reproducible development environment, saving developers time and effort in the long run.

You should explore Microsoft Dev Containers as a way to enhance your development workflow and boost productivity.

References