Deploying a Flask App to GCP using Docker and CI/CD
ARYAN KYATHAM
Impact Kid | 8x Hackathon Winner | Software Engineer | Startups & AI | Student @FRCRCE |
Step 1: Create requirements.txt
First, list the dependencies for your Flask application in a requirements.txt file:
Flask==2.0.3
Step 2: Create Dockerfile
Next, create a Dockerfile to containerize your Flask application. Below is an example:
Step 3: Update Flask App to Listen on Port 8080
In your app.py, ensure your Flask app listens on port 8080:
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080) # Hardcoded port 8080
Error Handling: If the port is not configured correctly, you may encounter the following error when deploying:
Revision 'flaskimg-00001-nqv' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout.
Ensure your app is configured to listen on port 8080, as GCP expects it.
Step 4: Create a New GCP Project
Create a new project in GCP, for example, flaskdeploydocker. Obtain the project ID for further use.
Step 5: Enable Cloud Run API and Cloud Build
You need to enable Cloud Run and Cloud Build APIs in your project. You can do this in the GCP Console, and ensure billing is enabled.
Step 6: Install Google Cloud CLI
Install the gcloud CLI tool and initialize it:
gcloud init
gcloud config set project flaskdockerdeploy
Step 7: Create Artifact Repository
Create a Docker repository in Google Artifact Registry:
gcloud artifacts repositories create flaskdocker --repository-format=docker --location=europe-west4 --description="FlaskDockerAPP" --immutable-tags --async
领英推è
Step 8: Authenticate Docker to GCP
Authenticate Docker to GCP's Artifact Registry:
gcloud auth configure-docker europe-west4-docker.pkg.dev
Step 9: Configure IAM Permissions
In IAM, assign the Object Storage Viewer permission to the service account to allow access to the Artifact Registry.
Step 10: Build and Push Docker Image
Build and push the Docker image to Artifact Registry:
gcloud builds submit --tag europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/flaskimg:flasktagnew
Step 11: Deploy to Cloud Run
- Go to the Cloud Run console.
- Select Deploy Container -> Service.
- Choose the container image from Artifact Registry.
- Allow unauthenticated invocations if you want your service to be publicly accessible.
Step 12: Set up CI/CD Pipeline with GitHub
To automate deployments, connect your GitHub repository to GCP and set up a CI/CD pipeline using Cloud Build to push images to Artifact Registry and deploy them to Cloud Run.
Step 13: Run Locally Using Docker
If you want to run the application locally using Docker, build and run it using the following commands:
docker build . --tag europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/otherimg:othertag
docker run -p 8080:8080 europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/otherimg:othertag
Gonna ship my next all apps using docker + gcp - Why? Because i have free credits of gcp (due to hackathon wins)
SIH'24 Winner | 3? @Codechef | Technical Head @IEEE CS SPIT | Undergrad @SPIT |
3 个月another great guide ????
@Atlas Research AI/ML dev | LLM Fine-Tuning | GenAI | Computer Vision | AI Agents & RAG | I build AI Stuff :)
3 个月Crazy ??
Data Scientist | Generative AI | Deep Learning | Large Language Models (LLMs) | NLP | Computer Vision | Finance and AI Fusion | Statistical Analysis
3 个月Very helpful
Student at Vishwakarma Institute of Information Technology
3 个月Interesting
Winner @Google Gen Ai Exchange Hackathon'24 | AI & ML | Science & Tech | Space ?? | SCOE CS'25
3 个月Very helpful