Build and deploy a simple Flask application on Google Cloud Run - Part 1 - Deploy a Python service
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
Amazing tutorial, Frederico!!! Tks for sharing!!
@FedericoTartarini
3 жыл бұрын
I am glad you found the video useful.
Complimenti Fede! This video has taught me a lot. I'm now one step closer to being a data engineer thanks to you.
@FedericoTartarini
Жыл бұрын
Glad it helped you.
Best tutorial on deploying a web app with GCP!
@FedericoTartarini
Ай бұрын
Thank you so much
You are the best Federico!
@FedericoTartarini
3 жыл бұрын
Thank you so much and thank you for letting me know that you enjoyed the video.
This is just fire info. Thank you man.
@FedericoTartarini
Жыл бұрын
Glad it was helpful!
Thank you for your sharing, is the best video
@FedericoTartarini
Жыл бұрын
You are welcome. I am glad you enjoyed it
thank you for your sharing, it means a lot for me,
@FedericoTartarini
2 жыл бұрын
You are welcome. I am very happy that you found it useful. Thank you for writing a comment.
So interesting - Thank you so much & keep going ^^
@FedericoTartarini
Жыл бұрын
Thanks for watching!
This is just lit and helped me a lot to meet a deadline, you are doing great work 🤩🤩
@FedericoTartarini
2 ай бұрын
Glad I could help!
Thank you,I also learn a lot
@FedericoTartarini
9 ай бұрын
Glad it was helpful!
If you are getting a deployment failed error. Try to remove "exec" command from your last line in docker file.
@bhavanimuddala7627
11 ай бұрын
thanks u solved my issue
@films2reels
8 ай бұрын
Solved my problem too! Thanks!
Thank you Fede! Do youhave some examples with a flask application using a sql db? That's my issue:(
cool!
@FedericoTartarini
8 ай бұрын
Thank you.
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
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.
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
Жыл бұрын
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
Жыл бұрын
@@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
Жыл бұрын
Great that you found a workaround and that you were able to deploy your app. Have a nice festive period
Excelent!! Nice shot!! One issue!! The code in the description have a typo.. "if _name_ " is surrounded by simple underlines!! Thankssss
@FedericoTartarini
3 ай бұрын
Thank you for pointing this out. I am glad you enjoyed the video. Thank you for writing a comment
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
6 ай бұрын
Good luck.
Got this content after a long search..! Does this tutorial also address the CI/CD as well?
@FedericoTartarini
2 жыл бұрын
Please have a look at part 2 and 3 of this video for more info on CI/CD
Complimenti Federico, ottimo tutorial. Non riesco però a capire i costi di questo servizio Google, potresti spiegarmi brevemente? Grazie! Iscritto e video piaciuto ✌🏻
@FedericoTartarini
Жыл бұрын
Ciao Tommaso, grazie mille. E' un po complicato spiegare come funzionano i cosi in un commento di KZread. 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.
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
2 жыл бұрын
Hi, have you followed all the steps as shown in the video?
@fabg05
2 жыл бұрын
Hi, I get the same error. Could you solve this ? best regards
@FedericoTartarini
2 жыл бұрын
I am currently on vacation so unfortunately 8 cannot check that.
@Ahmed-Hosam-Elrefai
2 жыл бұрын
I get similar results and it's frustrating. Did you figure out how to solve it?
Thank you for great video, but didn't understand docker
@FedericoTartarini
6 ай бұрын
Just copy the code I wrote.
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
3 жыл бұрын
Have a look at this video: kzread.info/dash/bejne/gHeVkrSNg7irdrQ.html You can also use the same Google SDK commands as you used to push the app the first time
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
Жыл бұрын
Try to change the port number
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
Жыл бұрын
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
Жыл бұрын
@@FedericoTartarini Ok, thank you.
Don't worry too mucha about this-a.
when doing the run emulator it just says loading web app forever?
@FedericoTartarini
Жыл бұрын
Hi, have you followed all the steps as shown in the video?
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
Жыл бұрын
Have you followed all the steps as shown in the video?
Will it work for Flask applications with SQLite database? Please reply ASAP. If yes then please share a tutorial.
@FedericoTartarini
4 ай бұрын
No it still not work with a local SQLite. You will need to connect it to an external database
not sure if had a copy and past error in the youtube comments - "Undefined variable '_name_" pylint(undefined-variable) [15, 4) "
@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
Жыл бұрын
@@FedericoTartarini I might be wrong but could be the code in the comments missing __ and _
@FedericoTartarini
Жыл бұрын
It should be if __name__ == '__main__':
Hi Fede
Deployment failed with error message: Failed to deploy the app. Error : unacceptable kind of an object to dump (object Undefined)
@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?
Error: Could not import 'app'
@FedericoTartarini
Жыл бұрын
Have you followed all the steps as shown in the video?
502 bad gateway 😢 how do I fix it
@FedericoTartarini
10 ай бұрын
Have you followed all the steps as shown in the video?
typo in the script "if __name__ == "__main__": should be "if ____name____ == "__main__":
@FedericoTartarini
Жыл бұрын
Thank you. I’ll fix that
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
10 ай бұрын
Are you getting any error messages?
@osmankabasakal2127
10 ай бұрын
@@FedericoTartarini solved it anyway thanks
@FedericoTartarini
10 ай бұрын
Great to hear that.
This is not working now
@FedericoTartarini
Жыл бұрын
What is not working? Could you please be a bit more specific? Have you followed all the steps?
google cloud run is a waste of time. you will run through a lot of deployment problems. it is really annoying
@FedericoTartarini
9 ай бұрын
I don’t think so. It work very well for all my applications