On the finish of 2024, I made a decision to study MLOps on Google Cloud. I purchased a course from Udemy about MLOps on Google Cloud and began to study. As a substitute of watching movies and writing my code, I write about what I do know on Medium. Writing about my studying journey will make me extra comfy with MLOps on Google Cloud.
So, to start out my writing journey, I wish to share how one can deploy a Flask App to the cloud utilizing Artifact and Cloud Run. Do you’ve a Flask App and wish to share it with different folks? However your luck with how to try this? Google Cloud may be one of many decisions for internet hosting your apps. Google Cloud has some alternatives, comparable to serverless, steady supply, straightforward integration with Google Service, and many others.
The very first thing to do whenever you wish to deploy Flask App is create a repository on Artifact Registry. In the event you don’t have a venture earlier than, it is best to create one. Go to your Google Cloud account and make a venture. After you make a venture, open your venture and search Artifact Registry within the search discipline. Click on on Artifact Registry, and your display will present the Artifact Registry part. Click on the Create Repository, fill within the required fields comparable to title and area, and click on Create.
Construct the Docker Picture
First, we have to package deal our Flask app right into a Docker container. Run the next command to construct the Docker picture.
docker construct -t demo-flask-app .
-t demo-flask-app
assigns a reputation to the Docker picture. The.
on the finish specifies that the Dockerfile
is within the present listing
Push the Picture to the Artifact Registry
After efficiently constructing the Docker picture, the following step is to push it to the Artifact Registry. Run the next command to replace the picture reference to your Artifact Registry.
docker tag flask-app REGION-docker.pkg.dev/PROJECT-ID/REPOSITORY-NAME/flask-app
REGION
is the area the place your Artifact Registry is hosted (e.g., us-central1
). PROJECT-ID
is your Google Cloud venture ID, and REPOSITORY-NAME
is the title of your Artifact Registry repository. After tagging, you possibly can push the picture with the next command beneath.
docker push REGION-docker.pkg.dev/PROJECT-ID/REPOSITORY-NAME/flask-app
Deploy the App to the Cloud Run
As soon as your container picture is within the Artifact Registry, you possibly can deploy it to Google Cloud Run. Cloud Run is a totally managed platform that lets you run containerized functions with out worrying about infrastructure. The deployment command appears to be like like this.
gcloud run deploy SERVICE-NAME
--image REGION-docker.pkg.dev/PROJECT-ID/REPOSITORY-NAME/flask-app
--region REGION
--allow-unauthenticated
Change SERVICE-NAME
with the title of your Cloud Run service (e.g., flask-service
). REGION
is the area for deployment (e.g., us-central1
). PROJECT-ID
is your Google Cloud venture ID, and REPOSITORY-NAME
is the title of your Artifact Registry repository. The --allow-unauthenticated
flag makes the app publicly accessible. What occurs subsequent? Google Cloud Run will pull the container picture from Artifact Registry, deploy the containerized app in a scalable serverless setting, and supply a safe HTTPS URL to entry your app. Open the URL in a browser to see your Flask app dwell!
Congratulations! You’ve efficiently deployed a Flask app to Google Cloud utilizing Artifact Registry and Cloud Run. This course of ensures your utility is packaged, saved securely, and deployed in a extremely scalable, serverless setting. Be at liberty to go away feedback beneath in case you have questions or points. 🚀