How to deploy your fastAPI application to Azure web service

Kaizin
3 min readMar 29, 2023

--

There are several ways to deploy a fastAPI application.

One of the ways is using Docker image. It is the fastest and stable way since you can contain all dependencies in image.

Below file is a simple example to create docker image.

First line determine python version that your program will run. Set the version as same as your environment.

From second line to fourth line, copy requirements file into working directory and install all packages that mentioned in the requirements file.

If you have to install some package separately, you can add other ‘RUN’ code.

Fifth line copy all files into working directory. the app folder has all files in this case.

Finally, set commend line that run fastAPI app with port 80.

To deploy a Docker image to an Azure Container Registry (ACR), you can follow these general steps:

  1. Log in to your Azure account using the Azure CLI:
az login

2. Create an Azure Container Registry if you haven’t already done so:

az acr create --name myregistry --resource-group myresourcegroup --sku Basic

or If you prefer to Azure portal, follow below link:

3. After creating registry, our docker can login to the registry using below code.

docker login myregistry.azurecr.io

Once you enter the code in terminal, it requires to enter User name and Password for the registry login.

Password didn’t show up when you type it in.

You can find your user name and password from access key tap in container registry page.

Find your password

4. Tag your Docker image with the ACR login server address and repository name:

docker tag myimage myregistry.azurecr.io/myimage:tag

If you use Docker Desktop, you can find your image that you built as below.

5. Push the Docker image to the ACR:

docker push myregistry.azurecr.io/myimage:tag

The pushed image is in repository of the registry and we can check like this.

Tag is ‘latest’ since I didn’t set tag explicitly.

6. Create App service

In the Azure portal, we can decide what we use to deploy while setting app instances. We check Docker container radio button.

After setting Docker container option, Docker tap will show up.

We check image source as Azure Container Registry.
Then, we can see registry that we created and image that we pushed.

--

--

Kaizin

Interested in data science. Recently keen into LLM related stuff.