How to deploy a FastAPI application on Patr

Aditi Shah
The Patr-onus Deployment Blog
4 min readJul 1, 2022

--

In this article, we are going to deploy a FastAPI application on Patr.

What is FastAPI?

Back in the days, when Python web frameworks like Flask and Django were popular, Python was quite different from how it is today. Many elements of modern Python, like asynchronous execution and the ASGI (Asynchronous Server Gateway Interface) standard, were either at an infant stage or didn’t exist yet.

FastAPI is a Python web framework that has been built from the ground up to make use of modern Python features. It uses the ASGI standard for asynchronous, concurrent connectivity with clients, and it can work with WSGI if needed. Async functions can be used for routes and endpoints. And FastAPI allows web applications to be written efficiently in clean, modern Python code with type hints.

Now we’ll see how to deploy a FastAPI app on Patr.

Step 1: Create a Patr account

Patr is a code deployment platform that takes care of running and scaling your applications in a hassle-free way. You can sign up for your free Patr account by signing up on app.patr.cloud.

Step 2: Create a Docker repository on Patr

Docker has become the preferred way to containerise modern applications and services. Patr provides first-class support for running docker images. Patr creates a docker registry for your docker images.

To deploy Docker images on Patr, you’ll first need to create a private Docker repository on Patr.

  1. Go to Docker Repository -> Create Repository
  2. Enter the repository name of your choice, for this example let it be “FastAPI”.
  3. Then click Create.

Once you copy you can get the docker repo information like repository URL, size etc.

Step 3: Dockerize your FastAPI app

To deploy your FastAPI application you need to dockerize your FastAPI app. You can dockerize your app using the following steps in the Dockerfile. Now in your project directory create a file Dockerfile with:

Step 4: Build and push your Docker image to Patr

Now that you have your Dockerfile, it’s time to build it and push it to the Docker registry that you have created in Step 3.

Step 5: Create a deployment for your app

Go to Infrastructure -> Deployment -> Create a new deployment on Patr

  1. Enter the name as “fastapi”.
  2. Choose image details as “fastapi”.
  3. Choose tag as “latest”.
  4. Choose a region nearest to your location in the dropdown menu.
  5. Click “Next”.

Based on your app select the port, env variables or secrets if you have created any. In this tutorial, we would go with port only.

Then press next and select the deployment strategy on push or on create, a select the number of replication for the app and select the machine type and then press Create.

Step 6: Copy Deployment ID

Within seconds, patr will make your application live. To access your application from your browser, click on that created deployment and copy the deployment-id in the URL.

Step 7: Using your app

Now you can access your application by using the URL

{port}-{deployment-id}.patr.cloud where — {port} is the port which we allowed during deployment (in our case 8000) — {deployment-id} is the id which we copied in the previous step.

Voila! You have successfully deployed a FastAPI app using Patr within seconds.

--

--