In the previous installment, we explored the foundational steps to establish a board game recommendation system utilizing FastAPI and PostgreSQL. Now, we transition into the deployment phase, where we will host our project on Render, making it accessible for users.
Our journey will involve several key tasks: configuring our PostgreSQL database on Render, populating it with data, Dockerizing our FastAPI application, and ultimately deploying it as a Render Web Application.
Table of Contents
- Deploying a PostgreSQL database on Render
- Deploying a FastAPI app as a Render Web Application
– Dockerizing our application
– Pushing Docker Image to DockerHub
– Pulling from DockerHub to Render
Tooling Used
- Render
- Docker Desktop
- Docker Hub
Deploying on Render
With our PostgreSQL database and FastAPI application functioning locally, we are poised to deploy them onto a cloud service, enabling access from a front-end application or directly via Swagger. For this endeavor, we will utilize Render, a cloud platform known for its user-friendly setup, particularly for smaller projects compared to giants like AWS and Azure.
To initiate this process, visit Render to create a new account. Once registered, click on the ‘New Project’ button to set up your project, which we will name “fastapi-test.” After the project is established, we can proceed to configure the necessary components.
Each project on Render encapsulates all required elements within a self-contained environment. For our application, we need two primary components: a database and a web server for our FastAPI application. We will begin by creating our database.
To do this, select ‘Create New Service’ and then choose ‘Postgres.’ You will be directed to a setup screen where you can name your database “fastapi-database” and opt for the free tier to commence. While this tier is temporary, it suffices for our current example, and Render’s pricing for long-term use remains reasonable.
After entering the database details and selecting ‘Create,’ the setup will take a moment. Upon completion, you will see a screen displaying the Internal and External Database URLs, which we will save in our .env file for future connection needs. Testing the connection from our local machine using the External Database URL will allow us to create the necessary tables before we configure our FastAPI application.
Loading Database
Having confirmed our ability to connect to the database from our local environment, we can now proceed to establish and populate our database tables. Utilizing our src/load_database.py script, we will connect to the database using the External Database URL as our connection string. The script will execute the test_table function defined in our DatabaseHandler class to verify the number of rows in each table.
Upon running this script, we expect to see output confirming the successful creation of tables, along with a validation that the data matches our input.
Deploying a FastAPI Application on Render
With the database now operational, we turn our attention to deploying the FastAPI application. We will leverage Render’s Web Application hosting service, allowing our FastAPI app to function as a web application accessible by external services. Although we will not be developing a front-end component at this stage, we can interact with our application through the Swagger documentation.
Containerizing our Application with Docker
To facilitate the transfer of our FastAPI project—including all code, dependencies, and environmental variables—into a container on Render, we will employ Docker. This tool simplifies the deployment process by enabling us to package our application as an image, which can then be deployed to Render.
Our workflow can be summarized as follows: the FastAPI app runs locally, a snapshot is captured with Docker, the image is pushed to DockerHub, and Render pulls this image to spin up a container on its server. To begin, ensure that Docker Desktop is installed and running, as it is essential for building our Docker image.
Building a Docker Image
To create a Docker image, first confirm that Docker Desktop is operational. Open the application and check for the green indicator confirming it is running. Next, we will define a Dockerfile, which contains the instructions for Docker to package our application into an image.
- FROM: Specifies the base image, in this case, python:3.13-slim-bullseye.
- WORKDIR: Sets the working directory inside the container to /app.
- RUN: Updates system dependencies.
- COPY: Includes the requirements.txt file, essential for installing project libraries.
- RUN: Installs the libraries listed in requirements.txt.
- COPY: Transfers the entire project directory to /app.
- RUN: Creates a logs directory at /app/logs.
- EXPOSE: Documents that port 8000 will be exposed.
- ENV: Sets the Python path to /app.
- CMD: Executes the FastAPI app using Uvicorn, specifying the app location and port.
With the Dockerfile in place, we can now build our Docker image using a few commands in the terminal. Each command should be executed separately from the top directory of your project.
docker build -t recommendersystem .
docker tag recommendersystem seelucas/fastapi_tutorial:fastapi_on_render
docker push seelucas/fastapi_tutorial:fastapi_on_render
After completing these steps, logging into DockerHub will reveal our newly created image.
Pulling Docker Image to Render
With our Docker image now hosted on DockerHub, we can deploy it on Render. Navigate to the project we created for our database, “fastapi-test,” and select “New” followed by “Web Service.” Since we are deploying from DockerHub, we will indicate that our source code is an existing image, pasting the DockerHub directory path into the ‘Image URL’ field.
As this image is private, we must create a DockerHub Access token to securely pull the image into Render. This process involves navigating to your DockerHub account settings, creating a personal access token with read permissions, and then entering this token in Render.
Once the credentials are established, we will proceed to configure our Render Web application, ensuring to include the environmental variables from our .env file. This is crucial, as the FastAPI application requires the Internal_Database_Url to connect to the database.
After deploying the web service, Render will take a few minutes to finalize the setup. Once completed, a notification will confirm the deployment, providing a link to access the application.
By appending /docs to the link, we can access the Swagger documentation, where we can test the connection to our database by using the Fetch All Users method, confirming that data retrieval is functioning as expected.
To ensure our user recommendation system is dynamically updating, we can add a new game to a user’s liked games and observe the changes in recommendations generated by the recommender pipeline.
Through this project, we have successfully demonstrated the setup and deployment of a recommendation system, utilizing FastAPI and PostgreSQL to deliver intelligent board game recommendations. Future enhancements could include implementing a hybrid recommendation system or enabling user tagging for richer feature capture.
For further exploration, you can find the project repository on GitHub, along with additional resources for FastAPI and Docker.
Figures: All images, unless otherwise noted, are by the author.
Links:
- Github Repository for Project: https://github.com/pinstripezebra/recommender_system
- FastAPI Docs: https://fastapi.tiangolo.com/tutorial/
- Docker Tutorial: https://www.youtube.com/watch?v=b0HMimUb4f0
- Docker Desktop Download: https://www.docker.com/products/docker-desktop/
- Docker Hub: https://hub.docker.com/