Deploying a Flask App to GCP using Docker and CI/CD

Deploying a Flask App to GCP using Docker and CI/CD

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:

dockerfile

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

  1. Go to the Cloud Run console.
  2. Select Deploy Container -> Service.
  3. Choose the container image from Artifact Registry.
  4. Allow unauthenticated invocations if you want your service to be publicly accessible.


cloud run


deployed container

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)

Omkar Yadav

SIH'24 Winner | 3? @Codechef | Technical Head @IEEE CS SPIT | Undergrad @SPIT |

3 个月

another great guide ????

Sujit Nirmal

@Atlas Research AI/ML dev | LLM Fine-Tuning | GenAI | Computer Vision | AI Agents & RAG | I build AI Stuff :)

3 个月

Crazy ??

Akshay B.

Data Scientist | Generative AI | Deep Learning | Large Language Models (LLMs) | NLP | Computer Vision | Finance and AI Fusion | Statistical Analysis

3 个月

Very helpful

Vinit Wankhade

Student at Vishwakarma Institute of Information Technology

3 个月

Interesting

Vinayak Shinde

Winner @Google Gen Ai Exchange Hackathon'24 | AI & ML | Science & Tech | Space ?? | SCOE CS'25

3 个月

Very helpful

要查看或添加评论,请登录

ARYAN KYATHAM的更多文章

  • Tech toolkit for winning hackathons

    Tech toolkit for winning hackathons

    This toolkit consists of features/tech we integrated which helped me win 8+ hackathons.?? Ranging from AI, openCV…

    23 条评论
  • How to Dockerize Your Django Application

    How to Dockerize Your Django Application

    1. Install Docker Desktop: Download Docker Desktop from docker.

    3 条评论
  • AWS - Networking, Compute, Storage

    AWS - Networking, Compute, Storage

    Full article Here : Link, ? the repo :) Earlier, people used to set up rack servers manually on-premises, which was…

    3 条评论
  • Python fundamentals 1 shot

    Python fundamentals 1 shot

    Full Blog : Click Here ?? Python is a versatile programming language that supports both Object-Oriented Programming…

    7 条评论
  • How to win hackathons ?

    How to win hackathons ?

    Build awesome team Your vibe should match not only the skills. Don’t team up with people with whom you ain’t…

    10 条评论
  • AI Powered Developer Guru

    AI Powered Developer Guru

    In software development, a significant portion of a developer's time—up to 60%—is spent not in writing code, but in…

  • Deploying Django on AWS Elastic Beanstalk

    Deploying Django on AWS Elastic Beanstalk

    Introduction: In this tutorial, we will walk through the process of deploying a Django application on AWS Elastic…

    2 条评论
  • Deploy dJango app on AWS using EC2, RDS, S3 !

    Deploy dJango app on AWS using EC2, RDS, S3 !

    ?? Launch Django Project on AWS with EC2, S3, RDS, Nginx, Gunicorn! ?? What is EC2 Instance ? : EC2 allows us to…

    1 条评论
  • Revolutionizing Billboard Analytics with Artificial Intelligence & Data Science.

    Revolutionizing Billboard Analytics with Artificial Intelligence & Data Science.

    What is Billboard Analytics ? Billboard analytics is the process of analyzing data related to outdoor advertising, such…

    3 条评论

社区洞察

其他会员也浏览了