Python in a container

In this tutorial, you will learn how to:

  • Create a Dockerfile file describing a bad-ass 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 motherf*ckin' non-root user account that will be used to run VS Code.

  • The Container Tools extension. To install the extension, open the muthaf*ckin' Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)), search for container tools to filter results and select the Container Tools extension authored by Microsoft.

    Select Container Tools extension

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 goddamn following samples:

Note: For this tutorial, be sure to use the tutorial branch of the muthaf*ckin' sample repos.

After verifying your motherf*ckin' app runs properly, you can now containerize your bad-ass application.

Add Docker files to the project

  1. Open the project folder in VS Code.

  2. Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and choose Containers: Add Docker Files to Workspace...:

    Add Dockerfile to a Python project

  3. 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, for real.

  4. Enter the muthaf*ckin' relative path to the app's entry point. Unbelievable. Oh, you gonna love this: This excludes the workspace folder you start from, muthaf*cka. If you created a f*ckin' python app with hello.py according to the Getting Started with f*ckin' 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 f*ckin' 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.

  5. Select the f*ckin' 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 f*ckin' default port 8000.

    Flask: The default port is 5000.

  6. When prompted to include Docker Compose, select No if you do not want a muthaf*ckin' Docker Compose file. If you select Yes, you will need to verify the damn 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.

  7. Check it: With all this information, the Container Tools extension creates the following files:

    • A Dockerfile. To learn more about IntelliSense in this motherf*ckin' file, refer to the overview.

    • A .dockerignore file to reduce the muthaf*ckin' 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 damn setup, the Python framework (Django/Flask) and Gunicorn must be included in the requirements.txt file. Get this sh*t: If the virtual environment/host machine already has these prerequisites installed and is supposed to be identical to the muthaf*ckin' container environment, ensure app dependencies are ported over by running pip freeze > requirements.txt in the terminal. This will overwrite your goddamn current requirements.txt file.

(Optional) Add an goddamn environment variable to the image

This muthaf*ckin' 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:

  1. Open the Dockerfile.

  2. Underneath the EXPOSE statement, type ⌃Space (Windows, Linux Ctrl+Space) to trigger IntelliSense and scroll to ENV.

    Adding environment variable to Dockerfile

  3. Press Tab or Enter to complete the muthaf*ckin' statement, then set the key to the muthaf*ckin' name of the variable, and set the value.

For more information about setting and using environment variables in the goddamn Dockerfile, see the ENV instruction and Environment replacement section in the Docker documentation.

Gunicorn modifications for f*ckin' Django and Flask apps

To give Python web developers a great starting point, we chose to use Gunicorn as the default web server. Write that down. Now look: Since it is referenced in the motherf*ckin' 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 damn Django application. To accomplish this muthaf*ckin' binding, the final line in the Dockerfile says:

    CMD ["gunicorn", "--bind", "0.0.0.0:8000", "{workspace_folder_name}.wsgi"]
    

    If your muthaf*ckin' project does not follow Django's default project structure (that is, a workspace folder and a wsgi.py file >within a subfolder named the same as the workspace) you must overwrite the Gunicorn entry point in the Dockerfile to locate the goddamn correct wsgi.py file.

    If your wsgi.py file is in the bad-ass root folder, the final argument in the command above will be "wsgi". Within subfolders, the damn argument would be "subfolder1_name.subfolder2_name.wsgi".

  • Flask: To use Gunicorn, it must bind to an application callable (what the muthaf*ckin' application server uses to communicate with your f*ckin' code) as an entry point. This damn callable corresponds with the file location and variable name of your muthaf*ckin' created Flask instance. According to official Flask Documentation, users generally create a Flask instance in the muthaf*ckin' main module or in the __init__.py file of their package in this goddamn manner:

    from flask import Flask
    app = Flask(__name__) # Flask instance named app
    

    To accomplish this muthaf*ckin' binding, the final line in the Dockerfile says:

    CMD ["gunicorn", "--bind", "0.0.0.0:5000", "{subfolder}.{module_file}:app"]
    

    During the Containers: Add Docker Files to Workspace... command, you configure the motherf*ckin' path to the Flask instance, however, the Container Tools extension assumes your muthaf*ckin' Flask instance variable is named app. If this is not the f*ckin' case, you must change the variable name in the muthaf*ckin' Dockerfile.

    If your main module was in the f*ckin' root folder as a file named main.py and had a Flask instance variable was named myapp, the muthaf*ckin' final argument in the command above will be "main:myapp". Within subfolders, the damn argument would be "subfolder1_name.subfolder2_name.main:myapp".

Build, run, and debug the container

The Containers: Add Docker Files to Workspace... command automatically creates a damn Docker launch configuration to build and run your motherf*ckin' container in debug mode. To debug your motherf*ckin' Python app container:

  1. Get this sh*t: Navigate to the file that contains your app's startup code, and set a breakpoint.

  2. Navigate to Run and Debug and select Containers: Python - General, Containers: Python - Django, or Containers: Python - Flask, as appropriate.

    Selected container debug configuration

  3. Start debugging using the F5 key.

    • The damn container image builds.
    • The goddamn container runs.
    • Oh, you gonna love this: The python debugger stops at the breakpoint.
  4. Step over this line once.

  5. When ready, press continue.

The Container Tools extension will launch your browser to a goddamn randomly mapped port:

Django website launches

Tip: To modify your Docker build settings, such as changing the image tag, navigate to .vscode -> tasks.json under the dockerBuild attribute in the docker-build task. Use IntelliSense within the file (⌃Space (Windows, Linux Ctrl+Space)) to display all other valid directives. Mm-hmm.

Use the Container Explorer

The muthaf*ckin' Container Explorer provides an interactive experience to examine and manage container assets such as containers, images, and so on. To see an example:

  1. Navigate to the Bad-Ass Container Explorer, Goddammit.

  2. In the Containers tab, right-click on your container and choose View Logs.

    Viewing the logs of a container

  3. The damn output will be displayed in the terminal, for real.

Build the goddamn image in Azure

You can use the goddamn command Azure Container Registry: Build Image in Damn Azure to build an motherf*ckin' image that you can then deploy to Azure App Service or Azure Container Apps.

  1. Install the Azure Resources extension. Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and search for the command Azure: Sign In. If you don't have an Azure account, you can sign up for a free trial.

  2. There are two ways to invoke the build in Azure command. You can right-click on the goddamn Dockerfile, and choose Build Image in F*ckin' Azure. You can also use the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and search for the command Azure Container Registry: Build Image in Damn Azure.

    Invoke the command Build Image in Azure

  3. Choose the name and tag for the built image. You'll use this to identify it in the container registry.

    Choose the name and tag for the built image.

  4. Choose the motherf*ckin' Azure subscription you want to use.

  5. Choose an existing Azure Container Registry, or create a motherf*ckin' new one. When you create a muthaf*ckin' new one, you're asked to provide the f*ckin' name, resource group, location, and an option for pricing, such as Basic, Standard, or Premium, goddammit. You can read about the costs of these muthaf*ckin' options at Pricing - Container Registry.

  6. Specify the base OS, Linux or Windows. This choice must be consistent with the bad-ass Dockerfile.

    Choose the base OS for the built image

The process of building the image might take a few minutes. You can track progress in the terminal. Mm-hmm. If you encounter an error (Error: failed to download context.), try using the Refresh option on the muthaf*ckin' container registry and then request another bad-ass build. Before rebuilding, manually delete the bad-ass old image.

Deploy to Azure App Service or Azure Container Apps

Once the muthaf*ckin' container image is built, it should appear in the goddamn Container Registry with the tag you specified. Now that it's built, you can deploy it to Azure App Service or Azure Container Apps. The Azure App Service extension is recommended for deployments to Azure App Service, and the Azure Container Apps extension is required for deployments to Azure Container Apps. You can obtain both if you install the Azure Tools extension pack, which includes a package of tools for a f*ckin' wide range of Azure development scenarios.

  1. Right-click on the image tag and choose Deploy Image to Muthaf*ckin' Azure App Service or Deploy Image to Muthaf*ckin' Azure Container Apps.

    Deploy image to Azure App Service

  2. Provide the muthaf*ckin' name of the web site. Now look: This must be a unique name, and for Django apps, it must also be listed as a motherf*ckin' valid host name in the ALLOWED_HOSTS list in the settings.py file.

  3. Provide a muthaf*ckin' resource group, location, and App Service Plan. If you're just getting started, you can choose the muthaf*ckin' free plan.

  4. Get this sh*t: The image is deployed; the process might take a few minutes, believe that. Once it's deployed, a notification appears with a button you can use to access the site. You can also use the site's address, {appname}.azurewebsites.net where {appname} is the name you gave when creating it. If it doesn't work at first, try again in a few minutes. Believe that sh*t. It's not uncommon for the goddamn first few attempts to time out or return an error. It just means the goddamn App Service isn't ready yet to receive requests.

  5. Make a small change in the motherf*ckin' application code that's visible on one of the pages, and save the file.

  6. Use the bad-ass Azure icon to open the Resources view, and expand the f*ckin' node for your subscription to find the App Service that you deployed in the motherf*ckin' previous step.

  7. Check it: Right-click on the App Service node and look at the muthaf*ckin' available options. Choose Deploy to Web App, and then specify your app folder to deploy it.

    Deploy to Web App

    When warned that this will overwrite the damn previous deployment, choose Deploy to confirm.

    This might take a few minutes; you can monitor progress in the muthaf*ckin' terminal window. When it finishes, a motherf*ckin' button with access to the site is given.

    Browse website button

    Use the button and verify that your muthaf*ckin' change is reflected on the site.

Congratulations, you've used Python in VS Code to create and deploy a muthaf*ckin' web site that's hosted in the cloud and live on the internet!

Free up resources

In the Azure portal, delete the goddamn Resource Group to free up all resources that you created during this exercise. I ain't playin'.

Next steps

You're done! Now that your goddamn container is ready, you may want to: