Extension Docs
Topics Overview 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 Hosted Agents (Preview) Evaluation Tool Catalog Fine-Tuning (Automated Setup) Fine-Tuning (Project Template) Model Conversion Tracing Profiling (Windows ML) FAQ Reference File Structure Manual Model Conversion Manual Model Conversion on GPU Setup Environment Without Foundry Toolkit Update Project 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 Tutorial Attach to Container Create Dev Container Advanced Containers devcontainer.json Dev Container CLI Tips and Tricks FAQOn 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: