<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="www.w3.org/2005/Atom" xmlns:sy="purl.org/rss/1.0/modules/syndication/" xmlns:dc="purl.org/dc/elements/1.1/" xmlns:content="purl.org/rss/1.0/modules/content/" xmlns:wfw="wellformedweb.org/CommentAPI/" xmlns:slash="purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Drew Lewis</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/drewlewis/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/drewlewis/feed.xml</link>
    <description />
    <lastBuildDate>Tue, 16 Apr 2019 05:04:41 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Running Django on Azure Web Apps for Containers with Docker</title>
      <link>https://docs.microsoft.com/archive/blogs/drewlewis/running-django-on-azure-web-apps-for-containers-with-docker</link>
      <pubDate>Thu, 29 Mar 2018 19:31:25 GMT</pubDate>
      <dc:creator><![CDATA[DrewLewis]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/drewlewis/?p=65</guid>
      <description><![CDATA[In this post I will walk you through a very simple approach to developing and hosting a simple...]]></description>
      <content:encoded><![CDATA[In this post I will walk you through a very simple approach to developing and hosting a simple Python/Django application in Azure App Service.  For this demo we will target <a target="_blank" href="https://azure.microsoft.com/en-us/services/app-service/containers/" rel="noopener">Azure Web Apps for Containers</a> which gives us the flexibility to customize our application dependencies by wrapping those needed libraries in a Docker container. Currently, App Service on Linux doesn't support Python, but we will be able to containerize our web app and include any dependencies as needed.  If you find your application's runtimes aren't supported by App Service by default, this is the approach you will take to run your application in Azure.
<h2>The Application</h2>
For this application I will create a simple blog powered by Django/Python.   To make it easy to follow the application structure, I have started the application development based on the excellent tutorial <a target="_blank" href="https://docs.djangoproject.com/en/2.0/intro/" rel="noopener">here</a>.  This tutorial walks you through creating a Poll application, but I have changed that slightly to match a simple blog.  For now the blog is based on an open source bootstrap theme, and has just a few working pages, but in the future I will add a proper database, configure debugging, configure telemetry, and setup CI/CD to make deployments much easier.
<h2>Dependencies</h2>
<a target="_blank" href="https://git-scm.com/downloads" rel="noopener">Install Git</a>

<a target="_blank" href="https://www.docker.com" rel="noopener">Install Docker</a>

<a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/?view=azure-cli-latest" rel="noopener">Install Azure CLI</a>

<a target="_blank" href="https://code.visualstudio.com/" rel="noopener">Install Visual Studio Code (optional)</a>

<a target="_blank" href="https://azure.microsoft.com/en-us/free/" rel="noopener">Create a free Azure Account</a>

<a target="_blank" href="https://hub.docker.com/" rel="noopener">Create a Docker Hub Account</a>

<a target="_blank" href="https://azure.microsoft.com/en-us/services/container-registry/" rel="noopener">Create an Azure Container Registry Account for Private Registration of your container</a>
<h2>Download the Application</h2>
To get started first clone the pre-built repository with the following commands.  Open a command prompt, navigate to a place where the application will live on the file system, and type the following.
<pre class="f5"><span class="user-select-contain">mkdir DjangoApps
cd DjangoApps
git clone https://github.com/drewelewis/azure-django-gunicorn-docker.git 
cd azure-django-gunicorn-docker</span>
</pre>
<div></div>
Now that the skeleton of the application has been setup, the next step is to open it in Visual Studio Code. In the command prompt type the following.
<pre class="f5"><span class="user-select-contain">code</span></pre>
<img src="https://msdnshared.blob.core.windows.net/media/2018/03/azure-django-gunicorn-docker-project-layout.png" alt="azure-django-gunicorn-docker project layout" width="958" height="560" class="padding15 alignnone wp-image-105 size-full" />
<h2><strong>Anatomy of the Application</strong></h2>
The code repository contains both the application code, and the image details all in the same repository.  Any changes with our application or the details of our image will be done in the same place.

Here is the rundown of the most important sections.

<em><strong>/mysite</strong></em>

Under the <em>mysite</em> directory is our main Django project, and contains all the settings required for our project to run.

<em><strong>/blog</strong></em>

The <em>blog</em> directory is an application within our project and contains all the bits required for our blog functionality.

<em><strong>init.sh</strong></em>

<em>Init.sh</em> is a script that will run when our Docker container starts.  For our purposes, this file will just boot up Gunicorn pointing to the location of our web application.
<h3>Docker File</h3>
At the root of this project is our <strong><em>dockerfile</em></strong>, which contains the details required for Docker to build an image.

The dockerfile is nothing more than a few commands needed to allow docker to build an image, and contains details on specific file locations and dependencies.
<div>

Notice the first line in our <em>dockerfile </em><strong><em class="monaco">FROM python:3.6</em>.  </strong>This is a reference to a base image that our image will be built from.  You can choose a base image that closely matches your requirements and then customize as needed later by adding in additional libraries.

</div>
For a list of base images please check out the Official Repositories list.  <a target="_blank" href="https://hub.docker.com/explore/" rel="noopener">https://hub.docker.com/explore/</a>
<h3>Application Dependencies</h3>
Notice the line that starts with <em class="monaco"><strong>RUN pip install</strong></em>.  This line installs the specific application dependencies as follows:

<strong>Gunicorn</strong>: A web server that will serve our dynamic Django content. <a target="_blank" href="docs.gunicorn.org/en/stable/index.html" rel="noopener">docs.gunicorn.org/en/stable/index.html</a>

<strong>Django</strong>: Our Python web application framework. <a target="_blank" href="https://www.djangoproject.com/" rel="noopener">https://www.djangoproject.com/</a>

<strong>psycopg2</strong>: A database driver that we will use later to connect to an Azure-hosted PostgreSQL database.

<strong>Whitenoise</strong>: A library that will allow Gunicorn to serve static content as well as dynamic content.  See more here. <a target="_blank" href="whitenoise.evans.io/en/stable/django.html" rel="noopener">whitenoise.evans.io/en/stable/django.html
</a><em><strong>
</strong></em>
<h2>Hosting the Docker Container on Docker Hub</h2>
With the basic application and everything configured to run a Docker container ready (see above), the next step is to get the container and the application that is hosted within it, into a Docker repository. This is an important step because Azure will use this container when creating and launching our application.

For this you can host the Docker container on Docker Hub or Azure Container Registry, but for this demo I chose Docker Hub.  Once we have a Docker account, we can create a repository and upload our image source code.  If you don't have a Docker Hub account, you can set one up <a target="_blank" href="https://hub.docker.com/" rel="noopener">here</a>.

To upload the image, open a command prompt, navigate to the application, and type the following.
<pre class="f5"><span class="user-select-contain">cd azure-django-gunicorn-docker
docker login --username your-user-name
docker build -t your-user-name/azure-django-gunicorn-docker:v1.0.0 .
docker push your-user-name/azure-django-gunicorn-docker:v1.0.0</span></pre>
Now if you log back into Docker Hub, you should see your repository uploaded in a similar fashion.

Note: If the repository doesn't exist, a new one will be created.  By default, this will be created as a public repository.

<img src="https://msdnshared.blob.core.windows.net/media/2018/03/DockerHubRepoList.png" alt="" width="951" height="867" class="padding15 alignnone wp-image-135 size-full" />
<h2>Configure Azure</h2>
The final step in running our Docker container on Azure App Service is to create our App Service.  For this, we will need to create a Resource Group and an App Service Plan, and then we can create our application.

Back in the command prompt, lets first login to Azure.  You will be prompted to complete the login process in a web browser.
<pre class="f5"><span class="user-select-contain">az login</span></pre>
<h3>Resource Groups</h3>
Next let's create our Resource Group.  This resource group is a container that will allow us to manage a group of resources a bit easier in the future.  Be sure to choose a location that is closest to you.
<pre class="f5"><span class="user-select-contain">az group create --name azure-django-gunicorn-dockerRG --location "East US"</span></pre>
<h3>App Service Plans</h3>
Let's create our App Service Plan.  A service plan will allow us to group our App Services to make it easier to add or remove additional services.  For more details on App Service plans, please have a look at the following resource.  <a target="_blank" href="https://docs.microsoft.com/en-us/azure/app-service/azure-web-sites-web-hosting-plans-in-depth-overview" rel="noopener">Azure App Service Plans Overview</a>
<pre class="f5"><span class="user-select-contain">az appservice plan create --name azure-django-gunicorn-dockerSP --resource-group azure-django-gunicorn-dockerRG --sku S1 --is-linux</span></pre>
<h3>Azure App Service</h3>
The following command will create our Azure App Service.  This is where we will reference our Resource Group, App Service, and Docker Hub repository.  Please ensure that you have your Docker Hub repository spelled correctly as our application will not load if there is a typo, and you will not be provided an error message if you misspell it.
<pre class="f5"><span class="user-select-contain">az webapp create --resource-group azure-django-gunicorn-dockerRG --plan azure-django-gunicorn-dockerSP --name django-on-azure-app --deployment-container-image-name drewl/azure-django-gunicorn-docker:v1.0.0</span></pre>
<h3>Application Settings</h3>
We will modify our App Service to be aware that our Gunicorn web server is listening on port 8000 for all traffic.
<pre class="f5"><span class="user-select-contain">az webapp config appsettings set --resource-group azure-django-gunicorn-dockerRG --name django-on-azure-app --settings WEBSITES_PORT=8000</span></pre>
<h2>Our Running Application</h2>
We now have everything configured and we should be able to see our application hosted within Azure App Service. To have a look, your URL should look something like this.

https://django-on-azure-app.azurewebsites.net

If all went well, here is what you will see!

<img src="https://msdnshared.blob.core.windows.net/media/2018/03/BlogPreview.png" alt="" width="951" height="870" class="padding15 alignnone size-full wp-image-125" />
<h2>Next Steps</h2>
I will follow this post up by showing you how to make changes to our application and automate the release using Visual Studio Team Services (VSTS).]]></content:encoded>
    </item>
  </channel>
</rss>