Build and deploy a simple Flask application on Google Cloud Run - Part 1 - Deploy a Python service

  Рет қаралды 44,591

Federico Tartarini

Federico Tartarini

Күн бұрын

In this tutorial, we'll show you how to deploy a simple Flask application on Google Cloud Run. Google Cloud Run is a serverless platform that allows you to run containerized applications quickly and easily.
We'll start by creating a simple Flask application and testing it locally. We are going to deploy the application using Cloud Shell.
By the end of this tutorial, you'll have a solid understanding of how to deploy a Flask application on Google Cloud Run and take advantage of its scalability, flexibility, and affordability.
So if you're ready to deploy your Flask application on Google Cloud Run, be sure to check out our tutorial. Don't forget to subscribe to our channel for more tutorials on Flask, Google Cloud, and other tools and technologies.
Keywords: Flask, Google Cloud Run, Docker, Containerization, Google Container Registry, Google Cloud SDK, gcloud, Deployment, Serverless, Scalability, Flexibility, Affordability
Hashtags: #Flask #GoogleCloudRun #Docker #Containerization #GoogleContainerRegistry #GoogleCloudSDK #gcloud #Deployment #Serverless #Scalability #Flexibility #Affordability
✅ Subscribe for more videos like this one
🔔 Click on the bell icon to be notified when I release a new video
👍 Please like my video!
⚠️ Join my channel to get access to perks: / @federicotartarini
🙏 Support my channel on Patreon at / federicotartarini
☕ Support my channel by buying me a coffee - www.buymeacoffee.com/FedericoT
✅ Let's connect:
🌍 My website - federicotartarini.github.io/
💻 GitHub - github.com/FedericoTartarini
Twitter - / federicotartar1
LinkedIn - / federico-tartarini
📝 Useful notes:
Google's official guide (contains source code): cloud.google.com/run/docs/qui...
👨‍💻 Source code:
Copy the following code in main.py
```
import os
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
name = os.environ.get("NAME", "World")
return "Hello {}!".format(name)
if _name_ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
```
Copy the following code in Dockerfile
```
Use the official lightweight Python image.
hub.docker.com/_/python
FROM python:3.9-slim
Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
Install production dependencies.
RUN pip install Flask gunicorn
Run the web service on container startup. Here we use the gunicorn
webserver, with one worker process and 8 threads.
For environments with multiple CPU cores, increase the number of workers
to be equal to the cores available.
Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
```
Add the following code in .dockerignore
```
Dockerfile
README.md
*.pyc
*.pyo
*.pyd
_pycache_
.pytest_cache
```
🎥 Content of this video:
00:00 - What you will learn
02:55 - Create a new Project
04:33 - Create a new Flask web application
10:30 - Run the Flask app on the local emulator
14:00 - Deploy to Cloud Run

Пікірлер: 89
@Jojo-lf4cw
@Jojo-lf4cw 2 ай бұрын
Best tutorial on deploying a web app with GCP!
@FedericoTartarini
@FedericoTartarini 2 ай бұрын
Thank you so much
@adrianorcampos
@adrianorcampos 3 жыл бұрын
Amazing tutorial, Frederico!!! Tks for sharing!!
@FedericoTartarini
@FedericoTartarini 3 жыл бұрын
I am glad you found the video useful.
@Alebobb
@Alebobb Жыл бұрын
Complimenti Fede! This video has taught me a lot. I'm now one step closer to being a data engineer thanks to you.
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Glad it helped you.
@revolutionarydefeatism
@revolutionarydefeatism 3 жыл бұрын
You are the best Federico!
@FedericoTartarini
@FedericoTartarini 3 жыл бұрын
Thank you so much and thank you for letting me know that you enjoyed the video.
@user-hu2ic2fj8c
@user-hu2ic2fj8c Жыл бұрын
This is just fire info. Thank you man.
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Glad it was helpful!
@jamirhuaman5860
@jamirhuaman5860 Жыл бұрын
Thank you for your sharing, is the best video
@FedericoTartarini
@FedericoTartarini Жыл бұрын
You are welcome. I am glad you enjoyed it
@wildananugrah
@wildananugrah 2 жыл бұрын
thank you for your sharing, it means a lot for me,
@FedericoTartarini
@FedericoTartarini 2 жыл бұрын
You are welcome. I am very happy that you found it useful. Thank you for writing a comment.
@adnanedriouche6335
@adnanedriouche6335 Жыл бұрын
So interesting - Thank you so much & keep going ^^
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Thanks for watching!
@mohdsarique3389
@mohdsarique3389 3 ай бұрын
This is just lit and helped me a lot to meet a deadline, you are doing great work 🤩🤩
@FedericoTartarini
@FedericoTartarini 3 ай бұрын
Glad I could help!
@457Fede
@457Fede 10 ай бұрын
Thank you,I also learn a lot
@FedericoTartarini
@FedericoTartarini 10 ай бұрын
Glad it was helpful!
@user-gc1wn9lr2d
@user-gc1wn9lr2d Жыл бұрын
If you are getting a deployment failed error. Try to remove "exec" command from your last line in docker file.
@bhavanimuddala7627
@bhavanimuddala7627 Жыл бұрын
thanks u solved my issue
@films2reels
@films2reels 9 ай бұрын
Solved my problem too! Thanks!
@portiseremacunix
@portiseremacunix 9 ай бұрын
cool!
@FedericoTartarini
@FedericoTartarini 9 ай бұрын
Thank you.
@user-to9op4jx2f
@user-to9op4jx2f 7 ай бұрын
Thank you Fede! Do youhave some examples with a flask application using a sql db? That's my issue:(
@nazwaunimal2173
@nazwaunimal2173 7 ай бұрын
i am too lazy to open my journey book to remeber what i have learned because tomorrow is my exam day so i just wacth your video and i hope it's can help me to face that exam tomorrow.
@FedericoTartarini
@FedericoTartarini 6 ай бұрын
Good luck.
@manomancan
@manomancan Жыл бұрын
Hi Federico! Thank you very much for your tutorial. You have a great way of explaining. However, my editor does not load, at the video on 10:17. I looked around the console on Chrome and Firefox and binged Stack Overflow. My WebSocket connection fails. I think it does with a lot of people. Probably an issue with CORS policy. It says, "blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource." Now, I am a bit afraid of giving full access with "Access-Control-Allow-Origin", "*". That would probably be a security risk. Any advice? Thank you, once again for this great tutorial!
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Thank you so much. Unfortunately, I am not sure how to solve the problem. I made the video quite some time ago and things may have slightly changed since then. I do not remember experiencing the same issue when I tried to deploy the application.
@manomancan
@manomancan Жыл бұрын
@@FedericoTartarini Still, thank you for your quick response, Federico! And thank you once again for your great tutorial. Looking forward to your new ones! (I have, of course, subscribed :)). You're not going to believe this, but I think it is a new bug on Google's end. Well, at least, if "the customer is always right," and the customer did absolutely tried everything to bypass CORS issues, then it is a bug on Google's end, haha. I don't seem to be the only once facing the problem. I managed to deploy my app via Heroku, so we have a happy ending. Have a great week!
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Great that you found a workaround and that you were able to deploy your app. Have a nice festive period
@inteligenciamilgrau
@inteligenciamilgrau 4 ай бұрын
Excelent!! Nice shot!! One issue!! The code in the description have a typo.. "if _name_ " is surrounded by simple underlines!! Thankssss
@FedericoTartarini
@FedericoTartarini 4 ай бұрын
Thank you for pointing this out. I am glad you enjoyed the video. Thank you for writing a comment
@warperone
@warperone 2 жыл бұрын
thanks you for your video. Can I ask - does the python flask app automatically get closed by Google Run ? If 100 url request are made at once does Google Run spin up 100 instances of the app then close them all ?
@FedericoTartarini
@FedericoTartarini 2 жыл бұрын
Yes Google Run automatically manages that. That is why it is a great system to deploy cloud applications since it only bills you when the application is being used.
@joncrosby1
@joncrosby1 2 жыл бұрын
I just get this when I get to the Cloud run emulator step? Any advice? (The minikube profile 'cloud-run-dev-internal' has been scheduled to stop automatically after exiting Cloud Code. To disable this on future deployments, set autoStop to false in your launch configuration /home//.vscode/launch.json flag needs an argument: --status-check See 'skaffold dev --help' for usage. Exited with code 127. Cleaning up... Finished clean up.)
@FedericoTartarini
@FedericoTartarini 2 жыл бұрын
Hi, have you followed all the steps as shown in the video?
@fabg05
@fabg05 2 жыл бұрын
Hi, I get the same error. Could you solve this ? best regards
@FedericoTartarini
@FedericoTartarini 2 жыл бұрын
I am currently on vacation so unfortunately 8 cannot check that.
@Ahmed-Hosam-Elrefai
@Ahmed-Hosam-Elrefai 2 жыл бұрын
I get similar results and it's frustrating. Did you figure out how to solve it?
@debjyotibanerjee7750
@debjyotibanerjee7750 3 жыл бұрын
How can I push changes to the GCP? I already have an app deployed, but i have made changes in the local system, and now i want to push it, how can I do that?
@FedericoTartarini
@FedericoTartarini 3 жыл бұрын
Have a look at this video: kzfaq.info/get/bejne/hKmRY7WFuLupdoU.html You can also use the same Google SDK commands as you used to push the app the first time
@adityanjsg99
@adityanjsg99 2 жыл бұрын
Got this content after a long search..! Does this tutorial also address the CI/CD as well?
@FedericoTartarini
@FedericoTartarini 2 жыл бұрын
Please have a look at part 2 and 3 of this video for more info on CI/CD
@tommasoseneca9189
@tommasoseneca9189 Жыл бұрын
Complimenti Federico, ottimo tutorial. Non riesco però a capire i costi di questo servizio Google, potresti spiegarmi brevemente? Grazie! Iscritto e video piaciuto ✌🏻
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Ciao Tommaso, grazie mille. E' un po complicato spiegare come funzionano i cosi in un commento di KZfaq. Ti suggerisco di guardare il loro sito cloud.google.com/run/pricing. Puoi anche il loro tool cloud.google.com/products/calculator. Noi usiamo Google Cloud Run per parecchie applicazioni e anche quelle che sono usate da tanti user ci costano meno di $10 dollari al mese. Pero' il costo finale dipende tanto dalla tua applicazione e da quanti utenti hai.
@user-ru2qh3zx5i
@user-ru2qh3zx5i Жыл бұрын
Hey Federico, I getting error message after I try to run on cloud run emulator "status check failed" Deploy failed Update failed with error code DEPLOY_UNKNOWN 1/1 deployment(s) failed Skaffold exited with code 1. any suggestions?
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Have you followed all the steps as shown in the video?
@aravindsudarshan2705
@aravindsudarshan2705 Жыл бұрын
Hey, I followed the whoel tutorial exactly. But it fails to load and when looking for logs , it says: The connection to the server localhost:8080 was refused - did you specify the right host or port? Can any body please help me out? Stuck on this for too long
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Try to change the port number
@sachinbhagat1483
@sachinbhagat1483 7 ай бұрын
Thank you for great video, but didn't understand docker
@FedericoTartarini
@FedericoTartarini 7 ай бұрын
Just copy the code I wrote.
@mattytripps
@mattytripps Жыл бұрын
when doing the run emulator it just says loading web app forever?
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Hi, have you followed all the steps as shown in the video?
@markpayton3895
@markpayton3895 Жыл бұрын
Thank you for the video! I am interested in using Google Cloud Run. Say I have a container that is running a certain task using Python Flask web framework and at the end of the task, I would like to automatically terminate the container/service that is running. Is there a way to do this using an API from inside the container without manually doing it in the cloud run console? If so, can you please point me in the direction where I can learn to accomplish this? Thank you so much.
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Hi Mark, thank you for reaching out to me. It is not 100% clear to me what you are trying to do so I am not sure how to help. Perhaps you could achieve what you are trying to do with Google Functions. Have a look at them and see if they are what you are looking for.
@markpayton3895
@markpayton3895 Жыл бұрын
@@FedericoTartarini Ok, thank you.
@noeldacosta7621
@noeldacosta7621 10 ай бұрын
Don't worry too mucha about this-a.
@user-to9op4jx2f
@user-to9op4jx2f 7 ай бұрын
Hi Fede
@maazshaikh7905
@maazshaikh7905 5 ай бұрын
Will it work for Flask applications with SQLite database? Please reply ASAP. If yes then please share a tutorial.
@FedericoTartarini
@FedericoTartarini 5 ай бұрын
No it still not work with a local SQLite. You will need to connect it to an external database
@mattytripps
@mattytripps Жыл бұрын
Error: Could not import 'app'
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Have you followed all the steps as shown in the video?
@plsj12
@plsj12 Жыл бұрын
Deployment failed with error message: Failed to deploy the app. Error : unacceptable kind of an object to dump (object Undefined)
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Could you please share more information about the error that you are getting? Have you followed all the steps as shown in the video?
@elderizm
@elderizm 11 ай бұрын
502 bad gateway 😢 how do I fix it
@FedericoTartarini
@FedericoTartarini 11 ай бұрын
Have you followed all the steps as shown in the video?
@rifkiamil
@rifkiamil Жыл бұрын
not sure if had a copy and past error in the youtube comments - "Undefined variable '_name_" pylint(undefined-variable) [15, 4) "
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Please try to check if the code you have copied and pasted is the same as the one I show in the video.
@rifkiamil
@rifkiamil Жыл бұрын
@@FedericoTartarini I might be wrong but could be the code in the comments missing __ and _
@FedericoTartarini
@FedericoTartarini Жыл бұрын
It should be if __name__ == '__main__':
@vijaybudhewar7014
@vijaybudhewar7014 Жыл бұрын
This is not working now
@FedericoTartarini
@FedericoTartarini Жыл бұрын
What is not working? Could you please be a bit more specific? Have you followed all the steps?
@AkshaySharmaa
@AkshaySharmaa Жыл бұрын
typo in the script "if __name__ == "__main__": should be "if ____name____ == "__main__":
@FedericoTartarini
@FedericoTartarini Жыл бұрын
Thank you. I’ll fix that
@osmankabasakal2127
@osmankabasakal2127 11 ай бұрын
I am trying to install pymongo. I added "RUN pip install pymongo==4.5.0" on the docker file but still not able to install it Any ideas?
@FedericoTartarini
@FedericoTartarini 11 ай бұрын
Are you getting any error messages?
@osmankabasakal2127
@osmankabasakal2127 11 ай бұрын
@@FedericoTartarini solved it anyway thanks
@FedericoTartarini
@FedericoTartarini 11 ай бұрын
Great to hear that.
@mustafaalsaegh7536
@mustafaalsaegh7536 10 ай бұрын
google cloud run is a waste of time. you will run through a lot of deployment problems. it is really annoying
@FedericoTartarini
@FedericoTartarini 10 ай бұрын
I don’t think so. It work very well for all my applications
How to easily Continuous Deployment with Cloud Run
34:24
Kevin Simper
Рет қаралды 34 М.
小蚂蚁被感动了!火影忍者 #佐助 #家庭
00:54
火影忍者一家
Рет қаралды 28 МЛН
Doing This Instead Of Studying.. 😳
00:12
Jojo Sim
Рет қаралды 19 МЛН
Slow motion boy #shorts by Tsuriki Show
00:14
Tsuriki Show
Рет қаралды 10 МЛН
Mama vs Son vs Daddy 😭🤣
00:13
DADDYSON SHOW
Рет қаралды 49 МЛН
Cloud Run user auth for internal apps
15:31
Google Cloud Tech
Рет қаралды 16 М.
How to Deploy AI Apps to the Cloud with Flask & Azure
31:15
Dave Ebbelaar
Рет қаралды 23 М.
Cloud Run QuickStart - Docker to Serverless
7:50
Fireship
Рет қаралды 182 М.
Run your React app on Google Cloud
16:28
Google Cloud Tech
Рет қаралды 17 М.
Google Cloud Platform (GCP) - Cloud Run
1:53:07
Cloud Monkey
Рет қаралды 27 М.
How to create a great dev environment with Docker
29:55
Patrick Loeber
Рет қаралды 257 М.
How To Deploy ML Models With Google Cloud Run
20:10
Patrick Loeber
Рет қаралды 46 М.
Deploy your Flask app on Google Cloud Platform
17:51
Leslie Wubbel
Рет қаралды 14 М.
小蚂蚁被感动了!火影忍者 #佐助 #家庭
00:54
火影忍者一家
Рет қаралды 28 МЛН