Deploy Python using Docker containers
This tutorial walks you through the full process of containerizing an existing Python application using Docker, pushing the app image to a Docker registry, then deploying the image to Azure App Service, all within Visual Studio Code. The tutorial also demonstrates how to use base container images that include production-ready web servers (uwsgi and nginx), and how to configure those servers for both Django and Flask web apps, which is helpful to know no matter what your deployment target.
If you have any problems, feel free to file an issue for this tutorial in the VS Code documentation repository.
An introduction to containers
Docker is a system that allows you to deploy and run apps using containers rather than setting up dedicated environments like virtual machines. A container is a lightweight runtime environment that shares the resources of the host operating system with other containers. Docker is the layer that sits above the operating system to manage resources on behalf of containers.
A container is specifically an instance of a Docker image, an executable package that contains everything needed to run your app: app code, configuration files, runtimes, and all of app's dependencies. A image can be used to instantiate any number of identical containers, which is especially useful when scaling out a cloud-based web app. Because container images are much smaller than virtual machine images, instances can be started and stopped much more quickly than virtual machines, enabling your app to be highly responsive to varying loads at a minimal cost. (When used to scale web apps, containers are often managed in clusters, which are then managed by an orchestration agent such as Kubernetes.)
Images, for their part, are built in multiple layers. The lowest or base layers of an image are typically common elements like the Python runtime; the higher layers the contain more specialized elements like your app code. Because of layering, it takes very little time to rebuild an image when changing only the top layer with your app code. Similarly, when you push an image to a container registry, an online repository for images from which you can deploy to cloud services like Azure, only the modified layers need be uploaded and redeployed. As a result, using containers has only a very small impact on your develop-test-deploy loop.
You will experience the basics of containers and images in the course of this tutorial. For additional background, including helpful diagrams, refer to the Docker documentation.
To complete this tutorial you need an Azure account, Docker, Visual Studio Code with the necessary extensions, a Python environment, and an app that you'd like to deploy using containers.
If you don't have an Azure account, sign up now for a free 30-day account with $200 in Azure credits to try out any combination of services.
Visual Studio Code, Docker, and Python runtime
Install the following:
- Visual Studio Code
- Docker Community Edition. To verify your installation, run the command
docker --version, which should show output like
Docker version 18.06.1-ce, build e68fc7a.
- Python and the Python extension as described on Python Tutorial - Prerequisites.
Docker and Azure App Service extensions for VS Code
The Docker extension helps you manage local Docker images, provides Docker commands, and simplifies deployment of app images to Azure. You can find an overview of the extension on the vscode-docker GitHub repository.
The Azure App Service extension helps you create, manage, and deploy Web Apps to Azure App Service on Linux. For details, explore the App Service extension tutorial or visit the vscode-azureappservice GitHub repository.
Sign in to Azure
Once the extensions are installed, sign into your Azure account by navigating to the Azure: App Service explorer, select Sign in to Azure, and follow the prompts.
After signing in, verify that you see the email account of your Azure around in the Status Bar and your subscription(s) in the Azure: App Service explorer:
Note: If you see the error "Cannot find subscription with name [subscription ID]", this may be because you are behind a proxy and unable to reach the Azure API. Configure
HTTPS_PROXYenvironment variables with your proxy information in your terminal:
# MacOS/Linux export HTTPS_PROXY=https://username:password@proxy:8080 export HTTP_PROXY=http://username:password@proxy:8080 #Windows set HTTPS_PROXY=https://username:password@proxy:8080 set HTTP_PROXY=http://username:password@proxy:8080
If you don't already have an app you'd like to work with, use one of the following samples, which already include the Docker-related files described later in this tutorial:
After verifying that your app runs properly, generate a
requirements.txt file using
pip freeze > requirements.txt (which are included in the samples) so that those dependencies can be automatically installed in the Docker image.
Create a container registry
As mentioned earlier, a container registry is an online repository for container images that allows a cloud service, like Azure App Service, to acquire the image whenever it needs to start a container instance. Because the registry manages images separate from container instances, the same image in a registry can be used to start any number of concurrent instances, as happens when scaling out a web app to handle increased loads.
Because setting up a registry is a one-time affair, you do that step now before creating images that you then push to that registry.
Registry options include the following:
- The Azure Container Registry (ACR), a private, secure, hosted registry for your images.
- Docker Hub, Docker's own hosted registry that provides a free way to share images.
- A private registry running on your own server, ad described on Docker registry in the Docker documentation.
To create an Azure Container Registry, as shown in this tutorial, do the following:
Follow the first part of Quickstart: Create a container registry using the Azure portal through the "Log in to ACR" section. You don't need to complete the sections "Push image to ACR" and later because you do those steps within VS Code as part of this tutorial.
Make sure that the registry endpoint you created is visible under Registries in the Docker explorer of VS Code:
Create a container image
A container image is a bundle of your app code and its dependencies. To create an image, Docker needs a
Dockerfile that describes how to structure the app code in the container and how to get that code running. The
Dockerfile, in other words, is the template for your image. The Docker extension helps you create these files with customization for production servers.
Note: The Python samples linked earlier in this article already contain the necessary Docker files. The instructions here help you create files for an app of your own.
Create the Docker files
In VS Code, open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and select the Docker: Add Docker files to workspace command.
When the prompt appears after a few moments, select Python as the app type.
Specify the port on which your app listens, such as 8000 (as in the Django sample) or 5000 (as in the Flask sample). The port value ends up only in the Docker compose files (see below) and have no impact on your container image.
With all this information, the Docker extension creates the following files:
Dockerfilefile describes the contents of your app's layer in the image. Your app layer is added on top of the base image indicated in the
Dockerfile.. By default, the name of the image is the name of the workspace folder in VS Code.
.dockerignorefile that reduces image size by excluding files and folders that aren't needed in the image, such as
.vscode. For Python, add another line to the file for
docker-compose.debug.ymlfiles that are used with Docker compose. For the purposes of this tutorial, you can ignore or delete these files.
Tip: VS Code provides great support for Docker files. See the Working with Docker article to learn about rich language features like smart suggestions, completions, and error detection.
Using production servers
For Python, the Docker extension by default specifies the base image
python:alpine in the
Dockerfile and includes commands to run only the Flask development server. These defaults obviously don't accommodate Django, for one, and when deploying to the cloud, as with Azure App Service, you should also use production-ready web servers instead of a development server. (If you're used Flask, you're probably accustomed to seeing the development server's warning in this regard!)
For this reason, you need to modify the
Dockerfile to use a base image with production servers, then provide the necessary configuration for your app. The following sections provide details for both Flask and Django.
Changes for Flask apps
A good base image for Flask is
tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7, which is also available for other versions of Python (see the tiangolo/uwsgi-nginx-flask respository on GitHub). This image already contains Flask and the production-ready uwsgi and nginx servers.
By default, the image assumes that (a) your app code is located in an
app folder, (b) the Flask app object is named
app, and (c) the app object is located in
main.py. Because your app may have a different structure, you can indicate the correct folders in the Dockerfile and provide the necessary parameters the the uwsgi server in a
The following steps summarize the configuration used in the python-sample-vscode-flask-tutorial app, which you can adapt for your own code.
Dockerfileindicates the location and name of the Flask app object, the location of static files for nginx, and the location of the
Dockerfilein the sample contains further explanatory comments that are omitted here.)
FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7 ENV LISTEN_PORT=5000 EXPOSE 5000 # Indicate where uwsgi.ini lives ENV UWSGI_INI uwsgi.ini # Tell nginx where static files live. ENV STATIC_URL /hello_app/static # Set the folder where uwsgi looks for the app WORKDIR /hello_app # Copy the app contents to the image COPY . /hello_app # If you have additional requirements beyond Flask (which is included in the # base image), generate a requirements.txt file with pip freeze and uncomment # the next three lines. #COPY requirements.txt / #RUN pip install --no-cache-dir -U pip #RUN pip install --no-cache-dir -r /requirements.txt
uwsgi.inifile, which is in the
hello_appfolder of the sample, provides configuration arguments for the uwsgi server. For the sample, the configuration below says that the Flask app object is found in the
hello_app/webapp.pymodule, and that it's named (that is, "callable" as)
app. The other value are additional common uwsgi settings:
[uwsgi] module = hello_app.webapp callable = app uid = 1000 master = true threads = 2 processes = 4
Changes for Django apps
A good base image for Flask is
tiangolo/uwsgi-nginx:python3.6-alpine3.7, which is also available for other versions of Python (see the tiangolo/uwsgi-nginx repository on GitHub).
This base image already contains the production-ready uwsgi and nginx servers, but does not include Django. It's also necessary to provide settings to uwsgi so it can find the app's startup code.
The following steps summarize the configuration used in the python-sample-vscode-django-tutorial app that you can adapt for your own code.
Make sure you have a
requirements.txtfile in your project that contains Django and its dependencies. You can generate
requirements.txtusing the command
pip freeze > requirements.txt.
In your Django project's
settings.pyfile, add the root URL to which you intend to deploy the app to the
ALLOWED_HOSTSlist. For example, the following code assumes deployment to an Azure App Service (azurewebsites.net) named "vsdocs-django-sample-container":
ALLOWED_HOSTS = [ # Example host name only; customize to your specific host "vsdocs-django-sample-container.azurewebsites.net" ]
Without this entry, you'll eventually get all the way through the deployment only to see a "DisallowedHost" message that instructs to you add the domain to
ALLOWED_HOSTS, which requires that you rebuild, push, and redeploy the image all over again!
uwsgi.inifile in the Django project folder (alongside
manage.py) that contains startup arguments for the uwsgi server. In the sample, the Django project is in a folder called
web_project, which is where the
[uwsgi] chdir = . module = web_project.wsgi:application env = DJANGO_SETTINGS_MODULE=web_project.settings uid = 1000 master = true threads = 2 processes = 4
Dockerfileto indicate the location of
uwsgi.ini, set the location of static files for nginx, and make sure the SQLite database file is writable. (The
Dockerfilein the sample contains further explanatory comments that are omitted here.)
FROM tiangolo/uwsgi-nginx:python3.6-alpine3.7 ENV LISTEN_PORT=8000 EXPOSE 8000 # Indicate where uwsgi.ini lives ENV UWSGI_INI uwsgi.ini # Tell nginx where static files live (as typically collected using Django's # collectstatic command. ENV STATIC_URL /app/static_collected # Copy the app files to a folder and run it from there WORKDIR /app ADD . /app # Make app folder writable for the sake of db.sqlite3, and make that file also writable. RUN chmod g+w /app RUN chmod g+w /app/db.sqlite3 # Make sure dependencies are installed RUN python3 -m pip install -r requirements.txt
Note: When building a Docker image on Windows, you typically see the message below, which is why the Dockerfile shown here includes the two
chmodcommands. If need to make other files writable, add the appropriate
chmodcommands to your Dockerfile.
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
Build and test the image
With the necessary
Dockerfile in place, you're ready to build the Docker image and run it locally:
Make sure that Docker is running on your computer.
On the VS Code Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), select Docker: Build Image.
When prompted for the Docker file, choose the
Dockerfilethat you created in the previous section. (VS Code remembers your selection so you won't need to enter it again to rebuild.)
When prompted for a name to give the image, use a name that follows the conventional form of
<registry or username>/<image name>:<tag>, where
latest. Here are some examples:
# Examples for Azure Container Registry, prefixed with the registry name vsdocsregistry.azurecr.io/python-sample-vscode-django-tutorial:latest vsdocsregistry.azurecr.io/python-sample-vscode-flask-tutorial:latest vsdocsregistry.azurecr.io/myexpressapp:latest # Examples for Docker hub, prefixed with your username vsdocs-team/python-sample-vscode-django-tutorial:latest vsdocs-team/python-sample-vscode-flask-tutorial:latest vsdocs-team/myexpressapp:latest
Each step of Docker's build process appears in the VS Code Terminal panel, including any errors that occur running the steps in the
Tip: every time you run the Docker: Build image command, the Docker extension opens another Terminal in VS Code in which to run the command. You can close each terminal once the build is complete. Alternately, you can reuse the same terminal to build the image by scrolling up in the command history using the up arrow.
When the build is complete, the image appears in the Docker explorer under Images:
Run and test your container locally by using the following command, replacing
<image_name>with your specific image, and changing the port numbers as needed. For web apps, you can then open browser to
localhost:<port>to see the running app.
# For Django sample docker run --rm -it -p 8000:8000 <image_name> # For Flask sample docker run --rm -it -p 5000:5000 <image_name>
Two useful features of the Docker extension
The Docker extension provides a simple UI to manage and even run your images rather than using the Docker CLI. Just expand the Image node in the Docker explorer, right click any image, and select any of the menu items:
In addition, on the top of the Docker explorer, next to the refresh button, is a button for System Prune. This command cleans up any dangling and otherwise unused images on your local computer. It's a good idea to periodically use the command to reclaim space on your file system.
Push the image to a registry
Once you're confident that your image works, the next step is to push it to your container registry:
On the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), select Docker: Push.
Choose the image you just built to push the image to the registry; upload progress appears in the Terminal.
Once completed, expand the Registries > Azure (or DockerHub) node in the Docker explorer, then expand the registry and image name to see the exact image. (You may need to refresh the Docker explorer.)
Tip: The first time you push an image, you see that VS Code uploads all of the different layers that make up the image. Subsequent push operations, however, upload only those layers that have changed. Because it's typically only your app code that's changes, those uploads happen much more quickly, making for a tight edit-build-deploy-test loop. To see this, make a small change to your code, rebuild the image, and then push again to the registry. The whole process typically completes in a matter of seconds.
Deploy the image to Azure
With an image built and pushed to a registry, you can use the Docker extension in VS Code to easily set up an Azure App Service running the container.
In the Docker explorer, expand Registries > Azure, then expand your registry node and the image name until you see the image with the
Right-click the image and select Deploy Image to Azure App Service.
Follow the prompts to select an Azure subscription, select or specify a resource group, specify a region, configure an App Service Plan (B1 is the least expensive), and specify a name for the site. The animation below illustrates the process.
A Resource Group is a named collection the different resources that make up an app. By assigning all the app's resources to a single group, you can easily manage those resources as a single unit. (For more information, see the Azure Resource Manager overview in the Azure documentation.)
An App Service Plan defines the physical resources (an underlying virtual machine) that hosts the running container. For this tutorial, B1 is the least expensive plan that supports Docker containers. (For more information, see App Service plan overview in the Azure documentation.)
The name of the App Service must be unique across all of Azure, so you typically use a company or personal name. For production sites, you typically configure the App Service with a separately-registered domain name.
Creating the app service takes a few minutes, and you see progress in VS Code's Output panel.
Once completed, you must also add a setting named
WEBSITES_PORTto the App Service to specify the port on which the container is listening, such as 8000 (in the Django sample) or 5000 (in the Flask sample). To do this, switch to the Azure: App Service explorer, expand the node for your new App Service (refresh if necessary), then right-click Application Settings and select Add New Setting. At the prompts enter
WEBSITES_PORTas the key and the port number for the value.
Right-click the App Service and select Restart to apply the setting.
After the service has restarted, browse the site at
http://<name>.azurewebsites.net. You can Ctrl+click the URL in the Output panel, or right-click the App Service in the Azure: App Service explorer and select Browse Website.
Make changes and redeploy
Because you inevitably make changes to your app, you end up rebuilding and redeploying your container many times. Fortunately, the process is very simple:
Make changes to your app and test locally.
Rebuild the Docker image. If you change only app code, the build should take only a few seconds.
Push your image to the registry. If again you change nothing but app code, only that small layer needs to be pushed and the process typically completes in a few seconds.
In the Azure: App Service explorer, right-click the appropriate App Service and select Restart. Restarting an app service automatically pulls the latest container image from the registry.
After about 15-20 seconds, visit the App Service URL again to check the updates.
From within VS Code, you can view (or "tail") logs from the running site on Azure App Service, which captures any output to the console as from
Find the app in the Azure: App Service explorer, right-click the app, and choose Start Streaming Logs.
Answer Yes when prompted to enable logging and restart the app. Once the app is restarted, the VS Code Output panel opens with a connection to the log stream.
After a few seconds, you see a message indicating that you are connected to the log-streaming service.
Connecting to log stream... 2018-09-27T20:14:26 Welcome, you are now connected to log-streaming service. 2018-09-27 20:14:59.269 INFO - Starting container for site 2018-09-27 20:14:59.270 INFO - docker run -d -p 24138:8000 --name vsdocs-django-sample-container_0 -e WEBSITES_PORT=8000 -e WEBSITE_SITE_NAME=vsdocs-django-sample-container -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_INSTANCE_ID=02c705ae24eaf5f298e553a9c2724b9fe4485707c2d1c36137cd02931091e561 -e HTTP_LOGGING_ENABLED=1 vsdocsregistry.azurecr.io/python-sample-vscode-django-tutorial:latest 2018-09-27 20:15:06.216 INFO - Container vsdocs-django-sample-container_0 for site vsdocs-django-sample-container initialized successfully.
Navigate within the app to see additional output for various HTTP requests.
Congratulations on completing this walkthrough of deploying a containerized Python app to Azure App Service!
As noted earlier, you can learn more about the Docker and App Service extensions by visiting their respective repositories on GitHub: vscode-docker and vscode-azureappservice. Issues and contributions are also very welcome.
To learn more about Azure services that you can use from Python, including data storage along with AI and Machine Learning services, visit Azure Python Developer Center.
There are also other Azure extensions for VS Code that you may find helpful. Just search on "Azure" in the Extensions explorer:
A few favorites include:
And again, if you've encountered any problems in the course of this tutorial, feel free to file an issue for this tutorial in the VS Code documentation repository.