Deploy a Python API project on Azure using docker

Problem

Though there are many contents on internet for deploying a Python application, it really tough to find a content which provides end-to-end solution.

Solution

In this comprehensive article, we will deploy our Python REST API application on Azure using Docker container.

Prerequisites:

1- We should have our application ready. So, I have this below Python Rest API application ready which works using aiohttp framework.

To know how to create a comprehensive Python REST API project using aiohttp framework you can follow our article. In this article we are deploying the same application. Complete project can also be found on this Github source as well.

Note:

If you are using Flask add this controller – app.run(host=’0.0.0.0′). If using aiohttp then not required.

2 – We need to have a Container registry on Azure first. Follow below 2 steps to create one.

   a- Browse to the Azure portal and sign in.

    b- Click the menu icon for New, select Containers, and then select Azure Container               Registry. Then create new.

Creating azure container registry

Then click on Review+Create then validation will occour then when its passed we can have our container ready.

Then go to Access keys option under Settings and turn on the Enabled switch behind the Admin user.

Before following the steps:

All commands are run in root directory of project where we have app.py 

We are keeping name of local docker image as py-docker-img 

Our container name is pytest260822

   Now step-by-step:

1 – Create a folder with any name, say “sdk” in the root of project to keep the wheel file if required. 

2- Login to Azure using  az login 

 3- Login to the container registry of Azure using az acr login –name <registry-name> 

so our command is az acr login –name pytest260822

4 – Add (if not already created) docker file using touch Dockerfile

5- Add below content to the docker file:

FROM python:3.9
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "/code/App.py"]

6 – Build the docker file to create docker image using below(include the last            dot)

     docker build -f Dockerfile -t py-docker-img:latest .

azure container login

7 – Run the docker image using docker run -p 5000:5000 –rm py-docker-img

8 – Test the docker image locally on localhost:5000 on your browser.

9 – The terminal may get locked so we may need to open another teminal now.

      Get name of all the docker images using docker images 

docker images

10 –  Get IMAGE ID from the list of the py-docker-img image created from the list. Here its b737e0d7d440

11 –  Tag the docker image to a new image for the container registry using 

docker tag <IMAGE ID> <registry-name>.azurecr.io/your-new-image-name:latest

Your-new-image-name can be py-img-docker1

 So our command:

docker tag b737e0d7d440 pytest260822.azurecr.io/py-img-docker1:latest
 
 12 – Push the docker image to Azure container registry using

docker push  <registry-name>.azurecr.io/your-new-image-name:latest

Remember the your-new-image-name to be used in step 13-c

Our command:

docker push  pytest260822.azurecr.io/py-img-docker1:latest

docker push

13 – Create web app with on Azure portal using below steps

a- Browse to the Azure portal and sign in.

b – Click the menu icon for Create a resource, select Compute, and then select Web App.

 

create Azure web app

c – Select the image name that we had used in step 12th

d – Click on review and create.

 

That’s all now we can browse our app:

Read more from Python:

Jenifer Walker
With references from Eric Hansa
669
Jenifer Walker
With references from Eric Hansa
43

2 thoughts on “Deploy a Python API project on Azure using docker

Leave a Reply

Your email address will not be published. Required fields are marked *