Documentation
Topics Overview Overview Linux macOS Windows VS Code for the Web Raspberry Pi Network Additional Components Uninstall VS Code Tutorial Copilot Quickstart User Interface Personalize VS Code Install Extensions Tips and Tricks Intro Videos Overview Setup Quickstart Overview Language Models Context Tools Agents Customization Trust & Safety Overview Agents Tutorial Agents Window Planning Memory Tools Subagents Local Agents Copilot CLI Cloud Agents Third-Party Agents Overview Chat Sessions Add Context Inline Chat Review Edits Checkpoints Artifacts Panel Debug Chat Interactions Prompt Examples Overview Instructions Prompt Files Custom Agents Agent Skills Language Models MCP Hooks Plugins Context Engineering Customize AI Test-Driven Development Edit Notebooks with AI Test with AI Test Web Apps with Browser Tools Debug with AI MCP Dev Guide OpenTelemetry Monitoring Inline Suggestions Smart Actions Best Practices Security Troubleshooting FAQ Cheat Sheet Settings Reference MCP Configuration Workspace Context Display Language Layout Keyboard Shortcuts Settings Settings Sync Extension Marketplace Extension Runtime Security Themes Profiles Overview Voice Interactions Command Line Interface Telemetry Basic Editing IntelliSense Code Navigation Refactoring Snippets Overview Multi-Root Workspaces Workspace Trust Tasks Debugging Debug Configuration Testing Port Forwarding Integrated Browser Overview Quickstart Staging & Committing Branches & Worktrees Repositories & Remotes Merge Conflicts Collaborate on GitHub Troubleshooting FAQ Getting Started Tutorial Terminal Basics Terminal Profiles Shell Integration Appearance Advanced Overview Enterprise Policies AI Settings Extensions Telemetry Updates Overview JavaScript JSON HTML Emmet CSS, SCSS and Less TypeScript Markdown PowerShell C++ Java PHP Python Julia R Ruby Rust Go T-SQL C# .NET Swift Working with JavaScript Node.js Tutorial Node.js Debugging Deploy Node.js Apps Browser Debugging Angular Tutorial React Tutorial Vue Tutorial Debugging Recipes Performance Profiling Extensions Tutorial Transpiling Editing Refactoring Debugging Quick Start Tutorial Run Python Code Editing Linting Formatting Debugging Environments Testing Python Interactive Django Tutorial FastAPI Tutorial Flask Tutorial Create Containers Deploy Python Apps Python in the Web Settings Reference Getting Started Navigate and Edit Refactoring Formatting and Linting Project Management Build Tools Run and Debug Testing Spring Boot Modernizing Java Apps Application Servers Deploy Java Apps GUI Applications Extensions FAQ Intro Videos GCC on Linux GCC on Windows GCC on Windows Subsystem for Linux Clang on macOS Microsoft C++ on Windows Build with CMake CMake Tools on Linux CMake Quick Start C++ Dev Tools for Copilot Editing and Navigating Debugging Configure Debugging Refactoring Settings Reference Configure IntelliSense Configure IntelliSense for Cross-Compiling FAQ Intro Videos Get Started Navigate and Edit IntelliCode Refactoring Formatting and Linting Project Management Build Tools Package Management Run and Debug Testing FAQ Overview Node.js Python ASP.NET Core Debug Docker Compose Registries Deploy to Azure Choose a Dev Environment Customize Develop with Kubernetes Tips and Tricks Overview Jupyter Notebooks Data Science Tutorial Python Interactive Data Wrangler Quick Start Data Wrangler PyTorch Support Azure Machine Learning Manage Jupyter Kernels Jupyter Notebooks on the Web Data Science in Microsoft Fabric Foundry Toolkit Overview Foundry Toolkit Copilot Tools Create Agents Models Playground Agent Builder Agent Inspector Evaluation Tool Catalog Fine-Tuning (Automated Setup) Fine-Tuning (Project Template) Model Conversion Tracing Profiling (Windows ML) FAQ File Structure Manual Model Conversion Manual Model Conversion on GPU Setup Environment Without Foundry Toolkit Template Project Migrating from Visualizer to Agent Inspector Overview Getting Started Resources View Deployment VS Code for the Web - Azure Containers Azure Kubernetes Service Kubernetes MongoDB Remote Debugging for Node.js Overview SSH Dev Containers Windows Subsystem for Linux GitHub Codespaces VS Code Server Tunnels SSH Tutorial WSL Tutorial Tips and Tricks FAQ Overview Tutorial Attach to Container Create Dev Container Advanced Containers devcontainer.json Dev Container CLI Tips and Tricks FAQ Default Keyboard Shortcuts Default Settings Substitution Variables Tasks SchemaOn this page there are 9 sections
Python in a container
In this tutorial, you will learn how to:
- Create a Dockerfile file describing a simple Python container.
- Build, run, and verify the functionality of a Django, Flask, or General Python app.
- Debug the app running in a container.
Prerequisites
-
Install Docker on your machine and add it to the system path.
-
On Linux, you should also enable Docker CLI for the non-root user account that will be used to run VS Code.
-
The Container Tools extension. To install the extension, open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)), search for container tools to filter results and select the Container Tools extension authored by Microsoft.
Create a Python project
If you don't have a Python project already, follow the tutorial Getting started with Python.
Note: If you want to containerize a complete Django or Flask web app, you can start with one of the following samples:
-
python-sample-vscode-django-tutorial, which is the result of following the Django Tutorial
-
python-sample-vscode-flask-tutorial, which is the result of following the Flask Tutorial
Note: For this tutorial, be sure to use the tutorial branch of the sample repos.
After verifying your app runs properly, you can now containerize your application.
Add Docker files to the project
-
Open the project folder in VS Code.
-
Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and choose Containers: Add Docker Files to Workspace...:
-
When prompted for the app type, select Python: Django, Python: Flask, or Python: General as the app type. For this tutorial, we'll focus on the Python: General case, but will also include notes for Django and Flask.
-
Enter the relative path to the app's entry point. This excludes the workspace folder you start from. If you created a python app with hello.py according to the Getting Started with Python tutorial, choose that.
Django: Choose manage.py (root folder) or subfolder_name/manage.py. See the official Django documentation.
Flask: Choose the path to where you create your Flask instance. See the official Flask documentation.
Tip: You may also enter the path to a folder name as long as this folder includes a __main__.py file.
-
Select the port number. We recommend selecting port 1024 or above to mitigate security concerns from running as a root user. Any unused will port, but Django and Flask use standard default ports.
Django: The default port 8000.
Flask: The default port is 5000.
-
When prompted to include Docker Compose, select No if you do not want a Docker Compose file. If you select Yes, you will need to verify the path to your wsgi.py file in the Dockerfile to run the Compose Up command successfully. Compose is typically used when running multiple containers at once.
-
With all this information, the Container Tools extension creates the following files:
-
A Dockerfile. To learn more about IntelliSense in this file, refer to the overview.
-
A .dockerignore file to reduce the image size by excluding files and folders that aren't needed such as .git, .vscode, and __pycache__.
-
If you're using Docker Compose, a docker-compose.yml and docker-compose.debug.yml file.
-
If one does not already exist, a requirements.txt file for capturing all app dependencies.
Important Note: To use our setup, the Python framework (Django/Flask) and Gunicorn must be included in the requirements.txt file. If the virtual environment/host machine already has these prerequisites installed and is supposed to be identical to the container environment, ensure app dependencies are ported over by running pip freeze > requirements.txt in the terminal. This will overwrite your current requirements.txt file.
-
(Optional) Add an environment variable to the image
This step is not required, but it is included to help you understand how to add environment variables that need to be set in the container's environment.
The Container Tools extension helps you author Dockerfiles by using IntelliSense to provide auto-completions and contextual help. To see this feature in action:
-
Open the Dockerfile.
-
Underneath the EXPOSE statement, type ⌃Space (Windows, Linux Ctrl+Space) to trigger IntelliSense and scroll to ENV.
-
Press Tab or Enter to complete the statement, then set the key to the name of the variable, and set the value.
For more information about setting and using environment variables in the Dockerfile, see the ENV instruction and Environment replacement section in the Docker documentation.
Gunicorn modifications for Django and Flask apps
To give Python web developers a great starting point, we chose to use Gunicorn as the default web server. Since it is referenced in the default Dockerfile, it is included as a dependency in the requirements.txt file. If you don't see it in requirements.txt, run pip install gunicorn and then run pip freeze > requirements.txt to regenerate the requirements.txt file.
-
Django: To use Gunicorn, it must bind to an application callable (what the application server uses to communicate with your code) as an entry point. This callable is declared in the wsgi.py file of a Django application. To accomplish this binding, the final line in the Dockerfile says:
from flask import Flask app = Flask(__name__) # Flask instance named appTo accomplish this binding, the final line in the Dockerfile says: