FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. Render is a cloud platform that simplifies deploying and scaling web applications and services. In this guide, we'll walk through the steps to deploy a FASTAPI application on Render, from creating a Docker image to deploying it on Render's platform.
Prerequisites:
A FASTAPI application with its dependencies specified in a
requirements.txt
file.Docker installed on your local machine.
An account on Docker Hub for storing your Docker images.
An account on Render to deploy and manage your application.
Step 1: Dockerize Your FASTAPI Application
Create a
Dockerfile
in your FASTAPI application directory. Below is a sampleDockerfile
:# Use an official Python runtime as a parent image FROM python:3.9 # Set the working directory in the container WORKDIR /app # Copy the requirements file into the container at /app COPY requirements.txt /app/ # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Copy the rest of the application's code into the container COPY . /app/ # Make port 8000 available to the world outside this container EXPOSE 8000 # Define environment variable ENV PYTHONUNBUFFERED 1 # Run app.py when the container launches CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Build your Docker image locally using the following command:
docker build -t your-docker-username/your-app-name:latest .
Test your Docker image locally by running it in a container:
docker run -p 8080:80 your-docker-username/your-app-name:latest
Step 2: Push Docker Image to Docker Hub
Log in to Docker Hub using the command:
docker login
Push your Docker image to Docker Hub:
docker push your-docker-username/your-app-name:latest
Step 3: Create a New Web Service on Render
Log in to Render and create a new web service.
Choose "Docker" as the environment and specify your Docker image from Docker Hub (
your-docker-username/your-app-name:latest
).Configure the service settings such as the service name, instance type, and environment variables if needed.
Step 4: Configure Environment Variables (Optional)
If your FASTAPI application requires environment variables, you can configure them in Render's dashboard under the environment settings for your service.
Step 5: Deploy Your Application
Click on the "Deploy" button in Render's dashboard to deploy your FASTAPI application.
Monitor the deployment logs to ensure that your application starts successfully.
Once deployed, Render provides you with a unique URL where your FASTAPI application is accessible.
Step 6: Test Your Deployed Application
Visit the URL provided by Render to access your deployed FASTAPI application.
Test the API endpoints and functionalities to ensure everything is working as expected.
Conclusion
By following these steps, you can successfully deploy your FASTAPI application on Render's platform. Dockerizing your application allows for easy deployment and scaling, while Render simplifies the deployment process with its user-friendly interface and robust infrastructure. Deploying on Render provides reliability, scalability, and easy management of your FASTAPI applications in the cloud.
Do share your comments and for communication contact me at manojbhavvan124@gmail.com