Тёмный

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

Federico Tartarini
Подписаться 8 тыс.
Просмотров 44 тыс.
50% 1

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

Опубликовано:

 

3 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 89   
@adrianorcampos
@adrianorcampos 3 года назад
Amazing tutorial, Frederico!!! Tks for sharing!!
@FedericoTartarini
@FedericoTartarini 3 года назад
I am glad you found the video useful.
@user-hu2ic2fj8c
@user-hu2ic2fj8c Год назад
This is just fire info. Thank you man.
@FedericoTartarini
@FedericoTartarini Год назад
Glad it was helpful!
@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.
@Jojo-lf4cw
@Jojo-lf4cw Месяц назад
Best tutorial on deploying a web app with GCP!
@FedericoTartarini
@FedericoTartarini Месяц назад
Thank you so much
@jamirhuaman5860
@jamirhuaman5860 Год назад
Thank you for your sharing, is the best video
@FedericoTartarini
@FedericoTartarini Год назад
You are welcome. I am glad you enjoyed it
@adnanedriouche6335
@adnanedriouche6335 Год назад
So interesting - Thank you so much & keep going ^^
@FedericoTartarini
@FedericoTartarini Год назад
Thanks for watching!
@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.
@mohdsarique3389
@mohdsarique3389 2 месяца назад
This is just lit and helped me a lot to meet a deadline, you are doing great work 🤩🤩
@FedericoTartarini
@FedericoTartarini 2 месяца назад
Glad I could help!
@457Fede
@457Fede 9 месяцев назад
Thank you,I also learn a lot
@FedericoTartarini
@FedericoTartarini 9 месяцев назад
Glad it was helpful!
@user-gc1wn9lr2d
@user-gc1wn9lr2d 11 месяцев назад
If you are getting a deployment failed error. Try to remove "exec" command from your last line in docker file.
@bhavanimuddala7627
@bhavanimuddala7627 11 месяцев назад
thanks u solved my issue
@films2reels
@films2reels 7 месяцев назад
Solved my problem too! Thanks!
@user-to9op4jx2f
@user-to9op4jx2f 5 месяцев назад
Thank you Fede! Do youhave some examples with a flask application using a sql db? That's my issue:(
@nazwaunimal2173
@nazwaunimal2173 5 месяцев назад
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 5 месяцев назад
Good luck.
@inteligenciamilgrau
@inteligenciamilgrau 3 месяца назад
Excelent!! Nice shot!! One issue!! The code in the description have a typo.. "if _name_ " is surrounded by simple underlines!! Thankssss
@FedericoTartarini
@FedericoTartarini 3 месяца назад
Thank you for pointing this out. I am glad you enjoyed the video. Thank you for writing a comment
@portiseremacunix
@portiseremacunix 8 месяцев назад
cool!
@FedericoTartarini
@FedericoTartarini 8 месяцев назад
Thank you.
@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.
@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
@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 RU-vid. 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.
@sachinbhagat1483
@sachinbhagat1483 6 месяцев назад
Thank you for great video, but didn't understand docker
@FedericoTartarini
@FedericoTartarini 6 месяцев назад
Just copy the code I wrote.
@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
@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: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-NCa0RTSUEFQ.html You can also use the same Google SDK commands as you used to push the app the first time
@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
@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?
@noeldacosta7621
@noeldacosta7621 9 месяцев назад
Don't worry too mucha about this-a.
@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?
@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?
@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.
@maazshaikh7905
@maazshaikh7905 4 месяца назад
Will it work for Flask applications with SQLite database? Please reply ASAP. If yes then please share a tutorial.
@FedericoTartarini
@FedericoTartarini 4 месяца назад
No it still not work with a local SQLite. You will need to connect it to an external database
@user-to9op4jx2f
@user-to9op4jx2f 5 месяцев назад
Hi Fede
@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__':
@plsj12
@plsj12 11 месяцев назад
Deployment failed with error message: Failed to deploy the app. Error : unacceptable kind of an object to dump (object Undefined)
@FedericoTartarini
@FedericoTartarini 11 месяцев назад
Could you please share more information about the error that you are getting? Have you followed all the steps as shown in the video?
@mattytripps
@mattytripps Год назад
Error: Could not import 'app'
@FedericoTartarini
@FedericoTartarini Год назад
Have you followed all the steps as shown in the video?
@elderizm
@elderizm 10 месяцев назад
502 bad gateway 😢 how do I fix it
@FedericoTartarini
@FedericoTartarini 10 месяцев назад
Have you followed all the steps as shown in the video?
@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 9 месяцев назад
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 9 месяцев назад
Are you getting any error messages?
@osmankabasakal2127
@osmankabasakal2127 9 месяцев назад
@@FedericoTartarini solved it anyway thanks
@FedericoTartarini
@FedericoTartarini 9 месяцев назад
Great to hear that.
@mustafaalsaegh7536
@mustafaalsaegh7536 9 месяцев назад
google cloud run is a waste of time. you will run through a lot of deployment problems. it is really annoying
@FedericoTartarini
@FedericoTartarini 9 месяцев назад
I don’t think so. It work very well for all my applications
Далее
Deploy your Flask app on Google Cloud Platform
17:51
Просмотров 12 тыс.
КТО ЭТО БЫЛ?
25:31
Просмотров 427 тыс.
Cloud Run user auth for internal apps
15:31
Просмотров 15 тыс.
Run your React app on Google Cloud
16:28
Просмотров 15 тыс.
How to easily Continuous Deployment with Cloud Run
34:24
Azure Tutorial - Deploying AI Apps (Flask / FastAPI)
31:15
Linux on Windows......Windows on Linux
23:54
Просмотров 109 тыс.
Picking the right serverless platform (Part 1)
14:02
Просмотров 14 тыс.
Cloud Run QuickStart - Docker to Serverless
7:50
Просмотров 179 тыс.