In a previous post I showed how to deploy a Python Flask app to the Azure App Service. In summary, once you’ve created a Flask app with a corresponding requirements.txt, it’s just a few lines of Azure CLI code to launch it.
The Azure App Service for Linux also supports directly deploying a Docker image based app. This is great for avoiding vendor lock-in. Maintain your app as a Docker image and deploy it locally or to the cloud platform of your choice. If you already have a working Docker image, it’s incredibly simple to deploy to the Azure App Service without having to care about the underlying virtual machine infrastructure. You can reference any image in the Docker Hub and deploy it as a container, changing the listening port and other environment variables as needed, with just two or three Azure CLI commands.
Let’s walk through it..
1. Create a Docker image
This step is not required if you have an existing Docker image ready to use.
Here’s the source code for the example Docker image I used: https://github.com/gbowerman/wfgdocker – it’s the same “Word Finder” Python Flask app used before to deploy directly to the App Service. The only change I made to the Flask code was to bind the app to any host “0.0.0.0”:
if __name__ == '__main__':
Here’s the Dockerfile I used to create the Docker image – in this case it takes an Ubuntu 18.04 image and adds Python pip3, Flask etc. With hindsight it may have been more efficient to start with a Python 3.x Docker image instead of Ubuntu, but as a proof of concept this is fine:
RUN apt-get update -y &&
apt-get install -y python3-pip
RUN mkdir /wfgeneral
ADD . /wfgeneral/
COPY ./requirements.txt /wfgeneral/requirements.txt
RUN pip3 install -r requirements.txt
COPY . /wfgeneral
ENTRYPOINT [ "python3" ]
CMD [ "application.py" ]
Here are the commands I used to build the Docker image on an Ubuntu 18.04 VM, test the image, and push it to Docker Hub:
2. Deploy the Docker image to Azure App Service# install docker sudo apt install docker.io # to copy docker app to local repo git clone https://github.com/gbowerman/wfgdocker cd wfgdocker # build the image sudo docker build --tag myuser/wfgdocker:v1.0.0 # after you make any changes in the master repo run # git pull origin master # to run container on the local VM.. # Note: I also needed to open port 5000 in the VM's network security group so I could connect to it externally sudo docker run -p 5000:5000 myuser/wfgdocker:v1.0.0 # log in to docker hub and upload docker image sudo docker login --username myuser sudo docker push myuser/wfgdocker:v1.0.0
When you have a working Docker image, deploying to the Azure App Service is a 2 step process. Create an app service plan (how powerful your app needs to be), and create a web app. An optional 3rd step can set environment variables and change the default listening port. Since my app listens on port 5000 by default I’ll set the WEBSITES_PORT variable so the App Service knows to convert this to 80. The following commands can be run in an Azure Cloud Shell:
# create an App Service plan
az appservice plan create --name mydockerplan --resource-group myresourcegroup --sku B1 --is-Linux
# create a web app
az webapp create --resource-group myresourcegroup --plan mydockerplan --name webappname --deployment-container-image-name sendmarsh/wfgdocker:v1.0.0
# look at "defaultHostName": "webappname.azurewebsites.net" in the output - this will be the website address
# set port to value specified in container app
az webapp config appsettings set --resource-group myresourcegroup --name webappname --settings WEBSITES_PORT=5000
After running these commands wait a few minutes, then go and find your app at webappname.azurewebsites.net.Making changes to the web app If you update the Docker image, simply restart the web app (in the portal, or use the CLI command ‘az webapp restart’). Further Reading The Azure Documentation has a more detailed tutorial for a Django Docker image here: Use a custom Docker image for Web App for Containers – which covers scenarios like deploying private images, and connecting using SSH.