Deploying a Flask REST API to Google Cloud Run allows you to run your application in a fully managed serverless environment. This guide will walk you through deploying a Flask API from a source directory using the gcloud command-line tool.
Prerequisites
Before deploying, ensure you have the following:
- Google Cloud account with billing enabled
- Google Cloud SDK installed and authenticated
Setting Up the Flask API
You can setup the Flask API however you like, and in the root directory create a Dockerfile and requirements.txt file
api/
│-- app/ # Flask application directory
│-- Dockerfile # Containerization instructions
│-- requirements.txt # Python Dependencies
Understanding the Dockerfile
The Dockerfile defines how the application is containerized:
FROM python:3.9-slim
WORKDIR /app
# *OPTIONAL* Install additional packages required by dependencies listed in requirements.tx
RUN apt-get update && apt-get install -y package-name-here
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 8080
COPY . /app
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "--workers", "2", "--threads", "8", "app:create_app()"]
This sets up a minimal Python environment, installs dependencies, exposes port 8080, and runs the API using Gunicorn.
Notice the apt-get install
command. This is optional and should only be used to install any additional packages required by dependencies listed in requirements.txt
.
Deploying to Google Cloud Run
-
Navigate to the API directory:
cd api
-
Deploy the API using Google Cloud Run:
gcloud run deploy flask-api --source ./ --region us-central1 --platform managed --allow-unauthenticated
This command does the following:
- Deploys the API from the current directory
- Sets the region to us-central1
- Uses the managed Cloud Run platform
- Allows unauthenticated access (optional, depends on your security needs)
-
Wait for the deployment to complete. Google Cloud Run will provide a public URL upon success.
-
Test the API:
bash curl https://YOUR_CLOUD_RUN_URL/hello-world
Simply replaceYOUR_CLOUD_RUN_URL
with the actual URL provided after deployment and substitutehello-world
with the specific API endpoint you are testing.
Troubleshooting Common Pitfalls
Gunicorn Command Issues
One common issue is getting the Gunicorn command to correctly call the Flask application. The CMD in the Dockerfile is defined as:
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "--workers", "2", "--threads", "8", "app:create_app()"]
However, the app:create_app()
command may vary based on your file structure and function name. If your application is organized differently, you might need to modify this command.
For example, if your app instance is directly in app.py, use:
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "app:app"]
If your factory function is named differently, adjust accordingly:
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "myapp:create_app()"]
If you are having issues after deploying, check your Flask application's entry point to ensure Gunicorn can locate and run it properly.
Use gcloud functions logs read
to view logs for debugging, or navigate to the Cloud Run service in the Google Cloud Console to view the logs for your service.
Conclusion
Deploying a Flask API to Google Cloud Run is simple with the gcloud command line tool. For a production-ready setup, consider setting the minimum instance to 1 to avoid cold starts and optimize performance.
For information on pricing, visit the Google Cloud Run Pricing page.